1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Collection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Collection. |
18
|
|
|
*/ |
19
|
|
|
class Collection implements CollectionInterface |
20
|
|
|
{ |
21
|
|
|
/** @var string Collection's identifier. */ |
22
|
|
|
protected $id; |
23
|
|
|
|
24
|
|
|
/** @var array Collection's items. */ |
25
|
|
|
protected $items = []; |
26
|
|
|
|
27
|
1 |
|
public function __construct(string $id, array $items = []) |
28
|
|
|
{ |
29
|
1 |
|
$this->setId($id); |
30
|
1 |
|
$this->items = $items; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
1 |
|
public function setId(string $id): BaseInterface |
37
|
|
|
{ |
38
|
1 |
|
$this->id = $id; |
39
|
|
|
|
40
|
1 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
1 |
|
public function getId(): string |
47
|
|
|
{ |
48
|
1 |
|
return $this->id; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Search an item by ID. |
53
|
|
|
*/ |
54
|
1 |
|
protected function searchItem(string $id): ?array |
55
|
|
|
{ |
56
|
1 |
|
return array_filter($this->items, function (ItemInterface $item) use ($id) { |
57
|
1 |
|
return $item->getId() == $id; |
58
|
1 |
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function has(string $id): bool |
65
|
|
|
{ |
66
|
1 |
|
$result = $this->searchItem($id); |
67
|
1 |
|
if (is_array($result) && !empty($result)) { |
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
* |
77
|
|
|
* @throws \DomainException |
78
|
|
|
*/ |
79
|
1 |
|
public function add(ItemInterface $item): CollectionInterface |
80
|
|
|
{ |
81
|
1 |
|
if ($this->has($item->getId())) { |
82
|
1 |
|
throw new \DomainException(\sprintf('Failed adding "%s" in "%s" collection: item already exists.', $item->getId(), $this->getId())); |
83
|
|
|
} |
84
|
1 |
|
$this->items[] = $item; |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
* |
92
|
|
|
* @throws \DomainException |
93
|
|
|
*/ |
94
|
1 |
|
public function replace(string $id, ItemInterface $item): CollectionInterface |
95
|
|
|
{ |
96
|
1 |
|
if (!$this->has($id)) { |
97
|
|
|
throw new \DomainException(\sprintf('Failed replacing "%s" in "%s" collection: item does not exist.', $item->getId(), $this->getId())); |
98
|
|
|
} |
99
|
1 |
|
$this->items[$this->getPosition($id)] = $item; |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
* |
107
|
|
|
* @throws \DomainException |
108
|
|
|
*/ |
109
|
1 |
|
public function remove(string $id): CollectionInterface |
110
|
|
|
{ |
111
|
1 |
|
if (!$this->has($id)) { |
112
|
|
|
throw new \DomainException(\sprintf('Failed removing "%s" in "%s" collection: item does not exist.', $id, $this->getId())); |
113
|
|
|
} |
114
|
1 |
|
unset($this->items[$this->getPosition($id)]); |
115
|
|
|
|
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
* |
122
|
|
|
* @throws \DomainException |
123
|
|
|
*/ |
124
|
1 |
|
public function get(string $id): ItemInterface |
125
|
|
|
{ |
126
|
1 |
|
if (!$this->has($id)) { |
127
|
|
|
throw new \DomainException(\sprintf('Failed getting "%s" in "%s" collection: item does not exist.', $id, $this->getId())); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $this->items[$this->getPosition($id)]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
* |
136
|
|
|
* @throws \DomainException |
137
|
|
|
*/ |
138
|
1 |
|
public function getPosition(string $id): int |
139
|
|
|
{ |
140
|
1 |
|
$result = $this->searchItem($id); |
141
|
1 |
|
$position = key($result); |
142
|
1 |
|
if (!is_int($position)) { |
143
|
|
|
throw new \DomainException(\sprintf('Failed getting position of "%s" in "%s" collection: item does not exist.', $id, $this->getId())); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
return $position; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function keys(): array |
153
|
|
|
{ |
154
|
|
|
return array_keys($this->items); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
1 |
|
public function first(): ?ItemInterface |
161
|
|
|
{ |
162
|
1 |
|
if (count($this->items) < 1) { |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
1 |
|
$items = $this->items; |
166
|
|
|
|
167
|
1 |
|
return array_shift($items); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function last(): ?ItemInterface |
174
|
|
|
{ |
175
|
|
|
if (count($this->items) < 1) { |
176
|
|
|
return null; |
177
|
|
|
} |
178
|
|
|
$items = $this->items; |
179
|
|
|
|
180
|
|
|
return array_pop($items); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
1 |
|
public function count(): int |
187
|
|
|
{ |
188
|
1 |
|
return count($this->items); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
1 |
|
public function toArray(): array |
195
|
|
|
{ |
196
|
1 |
|
return $this->items; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function toJson(): string |
203
|
|
|
{ |
204
|
|
|
return \sprintf("%s\n", json_encode($this->items)); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
1 |
|
public function getIterator(): \ArrayIterator |
211
|
|
|
{ |
212
|
1 |
|
return new \ArrayIterator($this->items); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
1 |
|
public function usort(\Closure $callback = null): CollectionInterface |
219
|
|
|
{ |
220
|
1 |
|
$callback ? usort($this->items, $callback) : usort($this->items, function ($a, $b) { |
221
|
|
|
if ($a == $b) { |
222
|
|
|
return 0; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return ($a < $b) ? -1 : 1; |
226
|
1 |
|
}); |
227
|
|
|
|
228
|
1 |
|
return new static($this->getId(), $this->items); /** @phpstan-ignore-line */ |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritdoc} |
233
|
|
|
*/ |
234
|
1 |
|
public function reverse(): CollectionInterface |
235
|
|
|
{ |
236
|
1 |
|
return new static($this->getId(), array_reverse($this->items)); /** @phpstan-ignore-line */ |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* {@inheritdoc} |
241
|
|
|
*/ |
242
|
1 |
|
public function filter(\Closure $callback): CollectionInterface |
243
|
|
|
{ |
244
|
1 |
|
return new static($this->getId(), array_filter($this->items, $callback)); /** @phpstan-ignore-line */ |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* {@inheritdoc} |
249
|
|
|
*/ |
250
|
|
|
public function map(\Closure $callback): CollectionInterface |
251
|
|
|
{ |
252
|
|
|
return new static($this->getId(), array_map($callback, $this->items)); /** @phpstan-ignore-line */ |
253
|
|
|
} |
254
|
|
|
|
255
|
1 |
|
/** |
256
|
|
|
* Implements \ArrayAccess. |
257
|
1 |
|
* |
258
|
|
|
* @param string $offset |
259
|
|
|
* |
260
|
|
|
* @return bool |
261
|
|
|
*/ |
262
|
|
|
#[\ReturnTypeWillChange] |
263
|
|
|
public function offsetExists($offset): bool |
264
|
|
|
{ |
265
|
|
|
return $this->has((string) $offset); |
266
|
|
|
} |
267
|
|
|
|
268
|
1 |
|
/** |
269
|
|
|
* Implements \ArrayAccess. |
270
|
1 |
|
* |
271
|
|
|
* @param string $offset |
272
|
|
|
* |
273
|
|
|
* @return CollectionInterface|ItemInterface|null |
274
|
|
|
*/ |
275
|
|
|
#[\ReturnTypeWillChange] |
276
|
|
|
public function offsetGet($offset) |
277
|
|
|
{ |
278
|
|
|
return $this->get((string) $offset); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Implements \ArrayAccess. |
283
|
|
|
* |
284
|
|
|
* @param mixed $offset |
285
|
|
|
* @param ItemInterface $value |
286
|
|
|
* |
287
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
288
|
|
|
*/ |
289
|
|
|
#[\ReturnTypeWillChange] |
290
|
|
|
public function offsetSet($offset, $value): void |
291
|
|
|
{ |
292
|
|
|
$this->add($value); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Implements \ArrayAccess. |
297
|
|
|
* |
298
|
|
|
* @param string $offset |
299
|
|
|
* |
300
|
|
|
* @return void |
301
|
|
|
*/ |
302
|
|
|
#[\ReturnTypeWillChange] |
303
|
|
|
public function offsetUnset($offset): void |
304
|
|
|
{ |
305
|
1 |
|
$this->remove($offset); |
306
|
|
|
} |
307
|
1 |
|
|
308
|
|
|
/** |
309
|
|
|
* Returns the collection ID. |
310
|
|
|
* |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
|
|
public function __toString() |
314
|
|
|
{ |
315
|
|
|
return $this->getId(); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|