1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace PublishingKit\Utilities\Collections; |
||
6 | |||
7 | use ArrayIterator; |
||
8 | use Generator; |
||
9 | use JsonSerializable; |
||
10 | use Traversable; |
||
11 | use PublishingKit\Utilities\Contracts\Collectable; |
||
12 | use PublishingKit\Utilities\Traits\Macroable; |
||
13 | |||
14 | /** |
||
15 | * Lazy collection class |
||
16 | * |
||
17 | * @psalm-consistent-constructor |
||
18 | */ |
||
19 | 1 | class LazyCollection implements Collectable |
|
20 | { |
||
21 | use Macroable; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
22 | |||
23 | /** |
||
24 | * @var callable|static |
||
25 | */ |
||
26 | private $source; |
||
27 | |||
28 | /** |
||
29 | * Create a new lazy collection instance. |
||
30 | * |
||
31 | * @param mixed $source |
||
32 | 78 | * @return void |
|
33 | */ |
||
34 | 78 | public function __construct($source = null) |
|
35 | 78 | { |
|
36 | 27 | if (is_callable($source) || $source instanceof self) { |
|
37 | 3 | $this->source = $source; |
|
38 | } elseif (is_null($source)) { |
||
39 | 27 | $this->source = static::empty(); |
|
40 | } else { |
||
41 | 78 | $this->source = $this->getArrayableItems($source); |
|
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | * |
||
48 | 3 | * @return self |
|
49 | */ |
||
50 | 3 | public static function make(callable $callback): self |
|
51 | { |
||
52 | return new static($callback); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Create a new instance with no items. |
||
57 | * |
||
58 | 3 | * @return static |
|
59 | */ |
||
60 | 3 | public static function empty() |
|
61 | { |
||
62 | return new static([]); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Results array of items from Collection or Arrayable. |
||
67 | * |
||
68 | * @param mixed $items |
||
69 | * |
||
70 | 27 | * @return iterable |
|
71 | */ |
||
72 | 27 | protected function getArrayableItems($items): iterable |
|
73 | 15 | { |
|
74 | 12 | if (is_array($items)) { |
|
75 | 3 | return $items; |
|
76 | 9 | } elseif ($items instanceof Collectable) { |
|
77 | 3 | return $items->toArray(); |
|
78 | 6 | } elseif ($items instanceof JsonSerializable) { |
|
79 | 3 | return (array) $items->jsonSerialize(); |
|
80 | } elseif ($items instanceof Traversable) { |
||
81 | 3 | return iterator_to_array($items); |
|
82 | } |
||
83 | return (array) $items; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | 3 | * {@inheritDoc} |
|
88 | */ |
||
89 | 3 | public function toJson() |
|
90 | { |
||
91 | return json_encode($this->toArray()); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get the collection of items as a plain array. |
||
96 | * |
||
97 | 27 | * @return iterable |
|
98 | */ |
||
99 | 18 | public function toArray(): iterable |
|
100 | 27 | { |
|
101 | 27 | return $this->map(function ($value) /* @return mixed */ { |
|
102 | return $value instanceof Collectable ? $value->toArray() : $value; |
||
103 | })->all(); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | * |
||
109 | 39 | * @return iterable |
|
110 | */ |
||
111 | 39 | public function all(): iterable |
|
112 | 6 | { |
|
113 | if (is_array($this->source)) { |
||
114 | 33 | return $this->source; |
|
115 | } |
||
116 | return iterator_to_array($this->getIterator()); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | 27 | * {@inheritDoc} |
|
121 | */ |
||
122 | 18 | public function map(callable $callback) |
|
123 | 27 | { |
|
124 | 27 | return new static(function () use ($callback) { |
|
125 | foreach ($this as $key => $value) { |
||
126 | 27 | yield $key => $callback($value, $key); |
|
127 | } |
||
128 | }); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | 9 | * {@inheritDoc} |
|
133 | */ |
||
134 | 9 | public function filter(callable $callback = null) |
|
135 | 2 | { |
|
136 | 3 | if (is_null($callback)) { |
|
137 | 3 | $callback = function ($value): bool { |
|
138 | return (bool) $value; |
||
139 | 6 | }; |
|
140 | 9 | } |
|
141 | 9 | return new static(function () use ($callback) { |
|
142 | 9 | foreach ($this as $key => $value) { |
|
143 | if ($callback($value, $key)) { |
||
144 | yield $key => $value; |
||
145 | 9 | } |
|
146 | } |
||
147 | }); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | 3 | * {@inheritDoc} |
|
152 | */ |
||
153 | 2 | public function reject(callable $callback) |
|
154 | 3 | { |
|
155 | 3 | return $this->filter(function ($item) use ($callback) { |
|
156 | return !$callback($item); |
||
157 | }); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | 3 | * {@inheritDoc} |
|
162 | */ |
||
163 | 3 | public function reduce(callable $callback, $initial = 0) |
|
164 | 3 | { |
|
165 | 3 | $result = $initial; |
|
166 | foreach ($this as $value) { |
||
167 | 3 | $result = $callback($result, $value); |
|
168 | } |
||
169 | return $result; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | 3 | * {@inheritDoc} |
|
174 | */ |
||
175 | 2 | public function pluck($name) |
|
176 | 3 | { |
|
177 | 3 | return $this->map(function ($item) use ($name) { |
|
178 | return $item[$name]; |
||
179 | }); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | 3 | * {@inheritDoc} |
|
184 | */ |
||
185 | 3 | public function each(callable $callback) |
|
186 | 3 | { |
|
187 | foreach ($this->source as $item) { |
||
188 | 3 | $callback($item); |
|
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | 3 | * {@inheritDoc} |
|
194 | */ |
||
195 | 3 | public function count() |
|
196 | { |
||
197 | return iterator_count($this->getIterator()); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | 42 | * {@inheritDoc} |
|
202 | */ |
||
203 | 42 | public function getIterator() |
|
204 | { |
||
205 | return $this->makeIterator($this->source); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | 3 | * {@inheritDoc} |
|
210 | */ |
||
211 | 3 | public function jsonSerialize() |
|
212 | { |
||
213 | return json_encode($this->toArray()); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Make an iterator from the given source. |
||
218 | * |
||
219 | * @param mixed $source |
||
220 | 42 | * @return \Traversable |
|
221 | */ |
||
222 | 42 | protected function makeIterator($source) |
|
223 | 6 | { |
|
224 | if (is_array($source)) { |
||
225 | 42 | return new ArrayIterator($source); |
|
226 | } |
||
227 | return $source(); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | 3 | * {@inheritDoc} |
|
232 | */ |
||
233 | 3 | public function serialize() |
|
234 | { |
||
235 | return serialize($this->toArray()); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | 3 | * {@inheritDoc} |
|
240 | */ |
||
241 | 3 | public function unserialize($serialized) |
|
242 | 3 | { |
|
243 | $this->source = unserialize($serialized); |
||
244 | 3 | } |
|
245 | |||
246 | 3 | public function __debugInfo() |
|
247 | { |
||
248 | return $this->toArray(); |
||
249 | } |
||
250 | } |
||
251 |