|
|
@@ -730,4 +730,74 @@ interface CollectionInterface extends Iterator, JsonSerializable {
|
|
|
*/
|
|
|
public function listNested($dir = 'desc', $nestingKey = 'children');
|
|
|
|
|
|
+/**
|
|
|
+ * Creates a new collection that when iterated will stop yielding results if
|
|
|
+ * the provided condition evaluates to false.
|
|
|
+ *
|
|
|
+ * This is handy for dealing with infinite iterators or any generator that
|
|
|
+ * could start returning invalid elements at a certain point. For example,
|
|
|
+ * when reading lines from a file stream you may want to stop the iteration
|
|
|
+ * after a certain value is reached.
|
|
|
+ *
|
|
|
+ * ### Example:
|
|
|
+ *
|
|
|
+ * Get an array of lines in a CSV file until the timestamp column is less than a date
|
|
|
+ *
|
|
|
+ * {{{
|
|
|
+ * $lines = (new Collection($fileLines))->stopWhen(function ($value, $key) {
|
|
|
+ * return (new DateTime($value))->format('Y') < 2012;
|
|
|
+ * })
|
|
|
+ * ->toArray();
|
|
|
+ * }}}
|
|
|
+ *
|
|
|
+ * Get elements until the first unapproved message is found:
|
|
|
+ *
|
|
|
+ * {{{
|
|
|
+ * $comments = (new Collection($comments))->stopWhen(['is_approved' => false]);
|
|
|
+ * }}}
|
|
|
+ *
|
|
|
+ * @param callable|array $condition the method that will receive each of the elements and
|
|
|
+ * returns false when the iteration should be stopped.
|
|
|
+ * If an array, it will be interpreted as a key-value list of conditions where
|
|
|
+ * the key is a property path as accepted by `Collection::extract`,
|
|
|
+ * and the value the condition against with each element will be matched.
|
|
|
+ * @return \Cake\Collection\CollectionInterface
|
|
|
+ */
|
|
|
+ public function stopWhen($condition);
|
|
|
+
|
|
|
+/**
|
|
|
+ * Creates a new collection where the items that it will contain are the
|
|
|
+ * concatenation of the lists of items generated by the transformer function
|
|
|
+ * after passing each of the items form the original collection.
|
|
|
+ *
|
|
|
+ * The transformer function will receive the value and the key for each of the
|
|
|
+ * items in the collection, in that order, and it must return an array or a
|
|
|
+ * Traversable object so that it can be concatenated to the final result.
|
|
|
+ *
|
|
|
+ * If no transformer function is passed, an "identity" function will be used.
|
|
|
+ * This is useful when each of the elements in the source collection are
|
|
|
+ * lists of items to be appended one after another.
|
|
|
+ *
|
|
|
+ * ### Example:
|
|
|
+ *
|
|
|
+ * {{{
|
|
|
+ * $items [[1, 2, 3], [4, 5]];
|
|
|
+ * $unfold = (new Collection($items))->unfold(); // Returns [1, 2, 3, 4, 5]
|
|
|
+ * }}}
|
|
|
+ *
|
|
|
+ * Using a transformer
|
|
|
+ *
|
|
|
+ * {{{
|
|
|
+ * $items [1, 2, 3];
|
|
|
+ * $allItems = (new Collection($items))->unfold(function ($page) {
|
|
|
+ * return $service->fetchPage($page)->toArray();
|
|
|
+ * });
|
|
|
+ * }}}
|
|
|
+ *
|
|
|
+ * @param callable|array $transformer A callable function that will receive each of
|
|
|
+ * the items in the collection and should return an array or Traversable object
|
|
|
+ * @return \Cake\Collection\CollectionInterface
|
|
|
+ */
|
|
|
+ public function unfold(callable $transformer = null);
|
|
|
+
|
|
|
}
|