map() public method

Applies a callback to a copy of all data in the collection and returns the result.
public map ( callback $filter, array $options = [] ) : mixed
$filter callback The filter to apply.
$options array The available options are: - `'collect'`: If `true`, the results will be returned wrapped in a new `Collection` object or subclass.
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.
Beispiel #1
0
 /**
  * Applies a callback to a copy of all data in the collection
  * and returns the result.
  *
  * Overriden to load any data that has not yet been loaded.
  *
  * @param callback $filter The filter to apply.
  * @param array $options The available options are:
  *              - `'collect'`: If `true`, the results will be returned wrapped
  *              in a new `Collection` object or subclass.
  * @return object The filtered data.
  */
 public function map($filter, array $options = array())
 {
     $defaults = array('collect' => true);
     $options += $defaults;
     if (!$this->closed()) {
         while ($this->next()) {
         }
     }
     $data = parent::map($filter, $options);
     if ($options['collect']) {
         foreach (array('_model', '_schema', '_pathKey') as $key) {
             $data->{$key} = $this->{$key};
         }
     }
     return $data;
 }
Beispiel #2
0
 /**
  * Applies a callback to a copy of all data in the collection
  * and returns the result.
  *
  * Overriden to load any data that has not yet been loaded.
  *
  * @param callback $filter The filter to apply.
  * @param array $options The available options are:
  *              - `'collect'`: If `true`, the results will be returned wrapped
  *              in a new Collection object or subclass.
  * @return array|object The filtered data.
  */
 public function map($filter, array $options = array())
 {
     if (!$this->closed()) {
         while ($this->next()) {
         }
     }
     return parent::map($filter, $options);
 }
Beispiel #3
0
 public function testCollectionMapFilter()
 {
     $collection = new Collection(array('data' => array(1, 2, 3, 4, 5)));
     $filter = function ($item) {
         return ++$item;
     };
     $result = $collection->map($filter);
     $this->assertNotEqual($collection, $result);
     $this->assertEqual(array(1, 2, 3, 4, 5), $collection->to('array'));
     $this->assertEqual(array(2, 3, 4, 5, 6), $result->to('array'));
     $result = $collection->map($filter, array('collect' => false));
     $this->assertEqual(array(2, 3, 4, 5, 6), $result);
 }
Beispiel #4
0
 /**
  * Applies a callback to a copy of all data in the collection
  * and returns the result.
  *
  * Overriden to load any data that has not yet been loaded.
  *
  * @param callback $filter The filter to apply.
  * @param array $options The available options are:
  *        - `'collect'`: If `true`, the results will be returned wrapped
  *        in a new `Collection` object or subclass.
  * @return object The filtered data.
  */
 public function map($filter, array $options = array())
 {
     $defaults = array('collect' => true);
     $options += $defaults;
     $this->offsetGet(null);
     $data = parent::map($filter, $options);
     if ($options['collect']) {
         foreach (array('_model', '_schema', '_pathKey') as $key) {
             $data->{$key} = $this->{$key};
         }
     }
     return $data;
 }