/**
  * Returns an iterator that iterates over the values of applying a JMESPath
  * search to each result yielded by the iterator as a flat sequence.
  *
  * @param string   $expression JMESPath expression to apply to each result.
  * @param int|null $limit      Total number of items that should be returned
  *                             or null for no limit.
  *
  * @return \Iterator
  */
 public function search($expression, $limit = null)
 {
     // Apply JMESPath expression on each result, but as a flat sequence.
     $xf = t\mapcat(function (Result $result) use($expression) {
         return (array) $result->search($expression);
     });
     // Apply a limit to the total items returned.
     if ($limit) {
         $xf = t\comp($xf, t\take($limit));
     }
     // Return items as an iterator.
     return t\to_iter($this, $xf);
 }
Example #2
0
 /**
  * Support for opendir().
  *
  * The opendir() method of the Amazon S3 stream wrapper supports a stream
  * context option of "listFilter". listFilter must be a callable that
  * accepts an associative array of object data and returns true if the
  * object should be yielded when iterating the keys in a bucket.
  *
  * @param string $path    The path to the directory
  *                        (e.g. "s3://dir[</prefix>]")
  * @param string $options Unused option variable
  *
  * @return bool true on success
  * @see http://www.php.net/manual/en/function.opendir.php
  */
 public function dir_opendir($path, $options)
 {
     $this->openedPath = $path;
     $params = $this->withPath($path);
     $delimiter = $this->getOption('delimiter');
     /** @var callable $filterFn */
     $filterFn = $this->getOption('listFilter');
     $op = ['Bucket' => $params['Bucket']];
     $this->openedBucket = $params['Bucket'];
     if ($delimiter === null) {
         $delimiter = '/';
     }
     if ($delimiter) {
         $op['Delimiter'] = $delimiter;
     }
     if ($params['Key']) {
         $params['Key'] = rtrim($params['Key'], $delimiter) . $delimiter;
         $op['Prefix'] = $params['Key'];
     }
     $this->openedBucketPrefix = $params['Key'];
     // Filter our "/" keys added by the console as directories, and ensure
     // that if a filter function is provided that it passes the filter.
     $this->objectIterator = t\to_iter($this->getClient()->getPaginator('ListObjects', $op), t\mapcat(function (Result $result) use($filterFn) {
         $contentsAndPrefixes = $result->search('[Contents[], CommonPrefixes[]][]');
         // Filter out dir place holder keys and use the filter fn.
         return array_filter($contentsAndPrefixes, function ($key) use($filterFn) {
             return (!$filterFn || call_user_func($filterFn, $key)) && (!isset($key['Key']) || substr($key['Key'], -1, 1) !== '/');
         });
     }));
     return true;
 }