| 1 | <?php |
||
| 10 | class IteratorTools |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Ensures that the given items are \Traversable. |
||
| 14 | * |
||
| 15 | * Usage: |
||
| 16 | * |
||
| 17 | * ``` |
||
| 18 | * IteratorTools::ensureTraversable([1, 2, 3]); |
||
| 19 | * IteratorTools::ensureTraversable(new \ArrayIterator([1, 2, 3])); |
||
| 20 | * ``` |
||
| 21 | * |
||
| 22 | * @param $items |
||
| 23 | * |
||
| 24 | * @return \Traversable |
||
| 25 | * |
||
| 26 | * @throws \InvalidArgumentException |
||
| 27 | */ |
||
| 28 | public static function ensureTraversable($items) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Creates an iterator from an array. |
||
| 43 | * |
||
| 44 | * Usage: |
||
| 45 | * |
||
| 46 | * ``` |
||
| 47 | * IteratorTools::fromArray([1, 2, 3]); |
||
| 48 | * ``` |
||
| 49 | * |
||
| 50 | * @param array $results |
||
| 51 | * |
||
| 52 | * @return \ArrayIterator |
||
| 53 | */ |
||
| 54 | public static function fromArray(array $results) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Creates an iterator from a generator. |
||
| 61 | * |
||
| 62 | * Example of valid usage: |
||
| 63 | * |
||
| 64 | * ``` |
||
| 65 | * IteratorTools::fromGenerator(function() { |
||
| 66 | * yield 1; |
||
| 67 | * yield 2; |
||
| 68 | * yield 3; |
||
| 69 | * }); |
||
| 70 | * ``` |
||
| 71 | * |
||
| 72 | * @param callable $generatorCallable A callable, which will return a generator once called. |
||
| 73 | * |
||
| 74 | * @return \Iterator |
||
| 75 | */ |
||
| 76 | public static function fromGenerator(callable $generatorCallable) |
||
| 80 | } |
||
| 81 |