1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Collection; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Collection. |
13
|
|
|
*/ |
14
|
|
|
class Collection implements CollectionInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Collection's identifier. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $id = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Collection's items. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $items = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Collection constructor. |
32
|
|
|
* |
33
|
|
|
* @param string|null $id |
34
|
|
|
* @param array $items |
35
|
|
|
*/ |
36
|
|
|
public function __construct($id = null, $items = []) |
37
|
|
|
{ |
38
|
|
|
$this->setId($id); |
39
|
|
|
$this->items = $items; |
40
|
|
|
/* |
41
|
|
|
if ($items) { |
42
|
|
|
foreach ($items as $item) { |
43
|
|
|
$this->add($item); |
44
|
|
|
} |
45
|
|
|
}*/ |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* If parameter is empty uses the object's hash. |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function setId(string $id = null) |
53
|
|
|
{ |
54
|
|
|
$this->id = $id; |
55
|
|
|
if (empty($this->id)) { |
56
|
|
|
$this->id = spl_object_hash($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getId(): string |
66
|
|
|
{ |
67
|
|
|
return $this->id; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function has(string $id): bool |
74
|
|
|
{ |
75
|
|
|
return array_key_exists($id, $this->items); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function add(ItemInterface $item): ?CollectionInterface |
82
|
|
|
{ |
83
|
|
|
if ($this->has($item->getId())) { |
84
|
|
|
throw new \DomainException(sprintf( |
85
|
|
|
'Failed adding "%s" in "%s" collection: item already exists.', |
86
|
|
|
$item->getId(), |
87
|
|
|
$this->getId() |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
$this->items[$item->getId()] = $item; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function replace(string $id, ItemInterface $item): ?CollectionInterface |
99
|
|
|
{ |
100
|
|
|
if (!$this->has($id)) { |
101
|
|
|
throw new \DomainException(sprintf( |
102
|
|
|
'Failed replacing "%s" in "%s" collection: item does not exist.', |
103
|
|
|
$item->getId(), |
104
|
|
|
$this->getId() |
105
|
|
|
)); |
106
|
|
|
} |
107
|
|
|
$this->items[$id] = $item; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function remove(string $id): ?CollectionInterface |
116
|
|
|
{ |
117
|
|
|
if (!$this->has($id)) { |
118
|
|
|
throw new \DomainException(sprintf( |
119
|
|
|
'Failed removing "%s" in "%s" collection: item does not exist.', |
120
|
|
|
$id, |
121
|
|
|
$this->getId() |
122
|
|
|
)); |
123
|
|
|
} |
124
|
|
|
unset($this->items[$id]); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function get(string $id): ?ItemInterface |
133
|
|
|
{ |
134
|
|
|
if (!$this->has($id)) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->items[$id]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function keys(): array |
145
|
|
|
{ |
146
|
|
|
return array_keys($this->items); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function first(): ?ItemInterface |
153
|
|
|
{ |
154
|
|
|
if (count($this->items) < 1) { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return array_shift($this->items); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function count(): int |
165
|
|
|
{ |
166
|
|
|
return count($this->items); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function toArray(): array |
173
|
|
|
{ |
174
|
|
|
return $this->items; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function getIterator(): \ArrayIterator |
181
|
|
|
{ |
182
|
|
|
return new \ArrayIterator($this->items); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function usort(\Closure $callback = null): CollectionInterface |
189
|
|
|
{ |
190
|
|
|
$callback ? uasort($this->items, $callback) : uasort($this->items, function ($a, $b) { |
191
|
|
|
if ($a == $b) { |
192
|
|
|
return 0; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return ($a < $b) ? -1 : 1; |
196
|
|
|
}); |
197
|
|
|
|
198
|
|
|
return new static(self::getId(), $this->items); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function filter(\Closure $callback): CollectionInterface |
205
|
|
|
{ |
206
|
|
|
return new static(self::getId(), array_filter($this->items, $callback)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
|
|
public function map(\Closure $callback): CollectionInterface |
213
|
|
|
{ |
214
|
|
|
return new static(self::getId(), array_map($callback, $this->items)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Implement ArrayAccess. |
219
|
|
|
* |
220
|
|
|
* @param string $offset |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
public function offsetExists($offset) |
225
|
|
|
{ |
226
|
|
|
return $this->has($offset); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Implement ArrayAccess. |
231
|
|
|
* |
232
|
|
|
* @param string $offset |
233
|
|
|
* |
234
|
|
|
* @return CollectionInterface|bool |
|
|
|
|
235
|
|
|
*/ |
236
|
|
|
public function offsetGet($offset) |
237
|
|
|
{ |
238
|
|
|
return $this->get($offset); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Implement ArrayAccess. |
243
|
|
|
* |
244
|
|
|
* @param mixed $offset |
245
|
|
|
* @param ItemInterface $value |
246
|
|
|
* |
247
|
|
|
* @return CollectionInterface|null |
248
|
|
|
*/ |
249
|
|
|
public function offsetSet($offset, $value) |
250
|
|
|
{ |
251
|
|
|
return $this->add($value); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Implement ArrayAccess. |
256
|
|
|
* |
257
|
|
|
* @param string $offset |
258
|
|
|
* |
259
|
|
|
* @return CollectionInterface|null |
260
|
|
|
*/ |
261
|
|
|
public function offsetUnset($offset) |
262
|
|
|
{ |
263
|
|
|
return $this->remove($offset); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Returns a string representation of this object. |
268
|
|
|
* |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
|
|
public function __toString() |
272
|
|
|
{ |
273
|
|
|
return sprintf("%s\n", json_encode($this->items)); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.