1 | <?php |
||
11 | class Collection implements IteratorAggregate, \Serializable |
||
12 | { |
||
13 | use CollectionTrait; |
||
14 | |||
15 | /** |
||
16 | * @var Traversable |
||
17 | */ |
||
18 | protected $input; |
||
19 | |||
20 | /** |
||
21 | * @var callable |
||
22 | */ |
||
23 | private $inputFactory; |
||
24 | |||
25 | /** |
||
26 | * @param callable|array|Traversable $input If callable is passed, it must return an array|Traversable. |
||
27 | */ |
||
28 | 106 | public function __construct($input) |
|
29 | { |
||
30 | 106 | if (is_callable($input)) { |
|
31 | 81 | $this->inputFactory = $input; |
|
32 | 81 | $input = $input(); |
|
33 | } |
||
34 | |||
35 | 106 | if (is_array($input)) { |
|
36 | 91 | $this->input = new ArrayIterator($input); |
|
37 | 83 | } elseif ($input instanceof Traversable) { |
|
38 | 81 | $this->input = $input; |
|
39 | } else { |
||
40 | 2 | throw $this->inputFactory ? new InvalidReturnValue : new InvalidArgument; |
|
41 | } |
||
42 | 104 | } |
|
43 | |||
44 | /** |
||
45 | * Static alias of normal constructor. |
||
46 | * |
||
47 | * @param callable|array|Traversable $input |
||
48 | * @return Collection |
||
49 | */ |
||
50 | 8 | public static function from($input) |
|
54 | |||
55 | /** |
||
56 | * Returns lazy collection of values, where first value is $input and all subsequent values are computed by applying |
||
57 | * $function to the last value in the collection. By default this produces an infinite collection. However you can |
||
58 | * end the collection by throwing a NoMoreItems exception. |
||
59 | * |
||
60 | * @param mixed $input |
||
61 | * @param callable $function |
||
62 | * @return Collection |
||
63 | */ |
||
64 | 2 | public static function iterate($input, callable $function) |
|
68 | |||
69 | /** |
||
70 | * Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite. |
||
71 | * |
||
72 | * @param mixed $value |
||
73 | * @param int $times |
||
74 | * @return Collection |
||
75 | */ |
||
76 | 3 | public static function repeat($value, $times = -1) |
|
80 | |||
81 | /** |
||
82 | * Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached. |
||
83 | * |
||
84 | * @param int $start |
||
85 | * @param int|null $end |
||
86 | * @param int $step |
||
87 | * @return Collection |
||
88 | */ |
||
89 | 2 | public static function range($start = 0, $end = null, $step = 1) |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | * @throws InvalidReturnValue |
||
97 | */ |
||
98 | 103 | public function getIterator() |
|
99 | { |
||
100 | 103 | if ($this->inputFactory) { |
|
101 | 80 | $input = call_user_func($this->inputFactory); |
|
102 | |||
103 | 80 | if (is_array($input)) { |
|
104 | 1 | $input = new ArrayIterator($input); |
|
105 | } |
||
106 | |||
107 | 80 | if (!($input instanceof Traversable)) { |
|
108 | throw new InvalidReturnValue; |
||
109 | } |
||
110 | |||
111 | 80 | $this->input = $input; |
|
112 | } |
||
113 | |||
114 | 103 | return $this->input; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 1 | public function serialize() |
|
121 | { |
||
122 | 1 | return serialize( |
|
123 | 1 | toArray( |
|
124 | 1 | map( |
|
125 | 1 | $this->input, |
|
126 | 1 | function ($value, $key) { |
|
127 | 1 | return [$key, $value]; |
|
128 | 1 | } |
|
129 | ) |
||
130 | ) |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | 1 | public function unserialize($serialized) |
|
141 | } |
||
142 |