You are here

public function DirectoryDestination::queryFiles in Backup and Migrate 5.0.x

Run a basic query with sort on the list of files.

Parameters

array $filters: An array of metadata fields to filter by.

string $sort: A metadata field to sort by, defaults to 'datestamp'.

int $sort_direction: The direction to sort by, either SORT_ASC or SORT_DESC.

int $count: The number of records to obtain.

int $start: The first item to start the result set from.

Return value

mixed

Overrides ListableDestinationInterface::queryFiles

1 method overrides DirectoryDestination::queryFiles()
DrupalDirectoryDestination::queryFiles in src/Drupal/Destination/DrupalDirectoryDestination.php
Run a basic query with sort on the list of files.

File

src/Core/Destination/DirectoryDestination.php, line 151

Class

DirectoryDestination
@package Drupal\backup_migrate\Core\Destination

Namespace

Drupal\backup_migrate\Core\Destination

Code

public function queryFiles(array $filters = [], $sort = 'datestamp', $sort_direction = SORT_DESC, $count = 100, $start = 0) {

  // Get the full list of files.
  $out = $this
    ->listFiles($count + $start);
  foreach ($out as $key => $file) {
    $out[$key] = $this
      ->loadFileMetadata($file);
  }

  // Filter the output.
  if ($filters) {
    $out = array_filter($out, function ($file) use ($filters) {
      foreach ($filters as $key => $value) {
        if ($file
          ->getMeta($key) !== $value) {
          return FALSE;
        }
      }
      return TRUE;
    });
  }

  // Sort the files.
  if ($sort && $sort_direction) {
    uasort($out, function ($a, $b) use ($sort, $sort_direction) {
      if ($sort_direction == SORT_DESC) {
        return $b
          ->getMeta($sort) < $b
          ->getMeta($sort);
      }
      else {
        return $b
          ->getMeta($sort) > $b
          ->getMeta($sort);
      }
    });
  }

  // Slice the return array.
  if ($count || $start) {
    $out = array_slice($out, $start, $count);
  }
  return $out;
}