find() public method

Filters a copy of the items in the collection.
public find ( callback $filter, array $options = [] ) : mixed
$filter callback Callback to use for filtering.
$options array The available options are: - `'collect'`: If `true`, the results will be returned wrapped in a new `Collection` object or subclass. Defaults to `true`.
return mixed The filtered items. Will be an array unless `'collect'` is defined in the `$options` argument, then an instance of this class will be returned.
Example #1
0
 /**
  * Tests that the `find()` method properly filters items out of the resulting collection.
  *
  * @return void
  */
 public function testCollectionFindFilter()
 {
     $collection = new Collection(array('data' => array_merge(array_fill(0, 10, 1), array_fill(0, 10, 2))));
     $this->assertEqual(20, count($collection->to('array')));
     $filter = function ($item) {
         return $item == 1;
     };
     $result = $collection->find($filter);
     $this->assertTrue($result instanceof Collection);
     $this->assertEqual(array_fill(0, 10, 1), $result->to('array'));
     $result = $collection->find($filter, array('collect' => false));
     $this->assertEqual(array_fill(0, 10, 1), $result);
 }
Example #2
0
 /**
  * Tests that the `find()` method properly filters items out of the resulting collection.
  */
 public function testCollectionFindFilter()
 {
     $collection = new Collection(array('data' => array_merge(array_fill(0, 10, 1), array_fill(0, 10, 2))));
     $this->assertCount(20, $collection->to('array'));
     $filter = function ($item) {
         return $item === 1;
     };
     $result = $collection->find($filter);
     $this->assertInstanceOf('lithium\\util\\Collection', $result);
     $this->assertEqual(array_fill(0, 10, 1), $result->to('array'));
     $result = $collection->find($filter, array('collect' => false));
     $this->assertEqual(array_fill(0, 10, 1), $result);
 }
Example #3
0
 /**
  * Overrides parent `find()` implementation to enable key/value-based filtering of entity
  * objects contained in this collection.
  *
  * @param mixed $filter Callback to use for filtering, or array of key/value pairs which entity
  *        properties will be matched against.
  * @param array $options Options to modify the behavior of this method. See the documentation
  *        for the `$options` parameter of `lithium\util\Collection::find()`.
  * @return mixed The filtered items. Will be an array unless `'collect'` is defined in the
  *         `$options` argument, then an instance of this class will be returned.
  */
 public function find($filter, array $options = array())
 {
     $this->offsetGet(null);
     if (is_array($filter)) {
         $filter = $this->_filterFromArray($filter);
     }
     return parent::find($filter, $options);
 }
 /**
  * Gets an array of classes and their corresponding definition files, or examines a file and
  * returns the classes it defines.
  *
  * @param array $options
  * @return array Associative of classes and their corresponding definition files
  * @todo Document valid options
  */
 public static function classes($options = array())
 {
     $defaults = array('group' => 'classes', 'file' => null);
     $options += $defaults;
     $list = get_declared_classes();
     $classes = array();
     if (!empty($options['file'])) {
         $loaded = new Collection(array('items' => array_map(function ($class) {
             return new ReflectionClass($class);
         }, $list)));
         if (!in_array($options['file'], $loaded->getFileName())) {
             include $options['file'];
             $list = array_diff(get_declared_classes(), $list);
         } else {
             $file = $options['file'];
             $filter = function ($class) use($file) {
                 return $class->getFileName() == $file;
             };
             $list = $loaded->find($filter)->getName();
         }
     }
     foreach ($list as $class) {
         $inspector = new ReflectionClass($class);
         if ($options['group'] == 'classes') {
             $inspector->getFileName() ? $classes[$class] = $inspector->getFileName() : null;
         } elseif ($options['group'] == 'files') {
             $classes[$inspector->getFileName()][] = $inspector;
         }
     }
     return $classes;
 }