/** * Constructs a new collection that will dynamically add properties to it out of * the values found in $values. * * @param array|\Traversable $into The target collection to which the values will * be inserted at the specified path. * @param string $path A dot separated list of properties that need to be traversed * to insert the value into the target collection. * @param array|\Traversable $values The source collection from which the values will * be inserted at the specified path. */ public function __construct($into, $path, $values) { parent::__construct($into); if (!$values instanceof Collection) { $values = new Collection($values); } $path = explode('.', $path); $target = array_pop($path); $this->_path = $path; $this->_target = $target; $this->_values = $values; }
/** * Wraps this iterator around the passed items so when iterated they are returned * in order. * * The callback will receive as first argument each of the elements in $items, * the value returned in the callback will be used as the value for sorting such * element. Please not that the callback function could be called more than once * per element. * * @param array|\Traversable $items The values to sort * @param callable|string $callback A function used to return the actual value to * be compared. It can also be a string representing the path to use to fetch a * column or property in each element * @param int $dir either SORT_DESC or SORT_ASC * @param int $type the type of comparison to perform, either SORT_STRING * SORT_NUMERIC or SORT_NATURAL */ public function __construct($items, $callback, $dir = SORT_DESC, $type = SORT_NUMERIC) { if (is_array($items)) { $items = new Collection($items); } $items = iterator_to_array($items, false); $callback = $this->_propertyExtractor($callback); $results = []; foreach ($items as $key => $value) { $results[$key] = $callback($value); } $dir === SORT_DESC ? arsort($results, $type) : asort($results, $type); foreach (array_keys($results) as $key) { $results[$key] = $items[$key]; } parent::__construct($results); }
/** * Maintains an in-memory cache of the results yielded by the internal * iterator. * * @param array|\Traversable $items The items to be filtered. */ public function __construct($items) { $this->_buffer = new SplDoublyLinkedList(); parent::__construct($items); }
/** * Creates an iterator from another iterator that will modify each of the values * by converting them using a callback function. * * Each time the callback is executed it will receive the value of the element * in the current iteration, the key of the element and the passed $items iterator * as arguments, in that order. * * @param array|\Traversable $items The items to be filtered. * @param callable $callback Callback. */ public function __construct($items, callable $callback) { $this->_callback = $callback; parent::__construct($items); $this->_innerIterator = $this->getInnerIterator(); }
/** * Creates the iterator that will return the requested property for each value * in the collection expressed in $path * * ### Example: * * Extract the user name for all comments in the array: * * {{{ * $items = [ * ['comment' => ['body' => 'cool', 'user' => ['name' => 'Mark']], * ['comment' => ['body' => 'very cool', 'user' => ['name' => 'Renan']] * ]; * $extractor = new ExtractIterator($items, 'comment.user.name''); * }}} * * @param array|\Traversable $items The list of values to iterate * @param string $path a dot separated string symbolizing the path to follow * inside the hierarchy of each value so that the column can be extracted. */ public function __construct($items, $path) { $this->_path = explode('.', $path); parent::__construct($items); }
/** * Constructor * * @param array|\Traversable $items Collection items. * @param string|callable $nestKey the property that contains the nested items * If a callable is passed, it should return the childrens for the passed item */ public function __construct($items, $nestKey) { parent::__construct($items); $this->_nestKey = $nestKey; }
/** * Creates a filtered iterator using the callback to determine which items are * accepted or rejected. * * Each time the callback is executed it will receive the value of the element * in the current iteration, the key of the element and the passed $items iterator * as arguments, in that order. * * @param Iterator $items The items to be filtered. * @param callable $callback Callback. */ public function __construct(Iterator $items, callable $callback) { $wrapper = new CallbackFilterIterator($items, $callback); parent::__construct($wrapper); }
/** * Unserialize a resultset. * * Part of Serializable interface. * * @param string $serialized Serialized object * @return void */ public function unserialize($serialized) { parent::__construct(unserialize($serialized)); }
/** * Creates an iterator from another iterator that will modify each of the values * by converting them using a callback function. * * Each time the callback is executed it will receive the value of the element * in the current iteration, the key of the element and the passed $items iterator * as arguments, in that order. * * @param array|\Traversable $items the items to be filtered * @param callable $callback */ public function __construct($items, callable $callback) { $this->_callback = $callback; parent::__construct($items); }
/** * Creates the iterator that will return the requested property for each value * in the collection expressed in $path * * ### Example: * * Extract the user name for all comments in the array: * * ``` * $items = [ * ['comment' => ['body' => 'cool', 'user' => ['name' => 'Mark']], * ['comment' => ['body' => 'very cool', 'user' => ['name' => 'Renan']] * ]; * $extractor = new ExtractIterator($items, 'comment.user.name''); * ``` * * @param array|\Traversable $items The list of values to iterate * @param string $path a dot separated string symbolizing the path to follow * inside the hierarchy of each value so that the column can be extracted. */ public function __construct($items, $path) { $this->_extractor = $this->_propertyExtractor($path); parent::__construct($items); }
/** * Creates an iterator that can be stopped based on a condition provided by a callback. * * Each time the condition callback is executed it will receive the value of the element * in the current iteration, the key of the element and the passed $items iterator * as arguments, in that order. * * @param array|\Traversable $items The list of values to iterate * @param callable $condition A function that will be called for each item in * the collection, if the result evaluates to false, no more items will be * yielded from this iterator. */ public function __construct($items, callable $condition) { $this->_condition = $condition; parent::__construct($items); $this->_innnerIterator = $this->getInnerIterator(); }