|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace League\CommonMark\Util; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Array collection |
|
16
|
|
|
* |
|
17
|
|
|
* Provides a wrapper around a standard PHP array. |
|
18
|
|
|
* |
|
19
|
|
|
* @internal |
|
20
|
|
|
* |
|
21
|
|
|
* @phpstan-template TKey |
|
22
|
|
|
* @phpstan-template TValue |
|
23
|
|
|
* @phpstan-implements \IteratorAggregate<TKey, TValue> |
|
24
|
|
|
* @phpstan-implements \ArrayAccess<TKey, TValue> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ArrayCollection implements \IteratorAggregate, \Countable, \ArrayAccess |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array<int|string, mixed> |
|
30
|
|
|
* @phpstan-var array<TKey, TValue> |
|
31
|
|
|
*/ |
|
32
|
|
|
private $elements; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor |
|
36
|
|
|
* |
|
37
|
|
|
* @param array<int|string, mixed> $elements |
|
38
|
|
|
* |
|
39
|
|
|
* @phpstan-param array<TKey, TValue> $elements |
|
40
|
|
|
*/ |
|
41
|
3123 |
|
public function __construct(array $elements = []) |
|
42
|
|
|
{ |
|
43
|
3123 |
|
$this->elements = $elements; |
|
44
|
|
|
|
|
45
|
3123 |
|
if (self::class !== static::class) { |
|
46
|
|
|
@\trigger_error('Extending the ArrayCollection class is deprecated in league/commonmark 1.6 and will not be allowed in 2.0', \E_USER_DEPRECATED); |
|
47
|
|
|
} |
|
48
|
3123 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return mixed|false |
|
52
|
|
|
* |
|
53
|
|
|
* @phpstan-return TValue|false |
|
54
|
|
|
*/ |
|
55
|
120 |
|
public function first() |
|
56
|
|
|
{ |
|
57
|
120 |
|
return \reset($this->elements); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return mixed|false |
|
62
|
|
|
* |
|
63
|
|
|
* @phpstan-return TValue|false |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function last() |
|
66
|
|
|
{ |
|
67
|
3 |
|
return \end($this->elements); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Retrieve an external iterator |
|
72
|
|
|
* |
|
73
|
|
|
* @return \ArrayIterator<int|string, mixed> |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @phpstan-return \ArrayIterator<TKey, TValue> |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function getIterator() |
|
78
|
|
|
{ |
|
79
|
3 |
|
return new \ArrayIterator($this->elements); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param mixed $element |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
* |
|
87
|
|
|
* @phpstan-param TValue $element |
|
88
|
|
|
* |
|
89
|
|
|
* @deprecated |
|
90
|
|
|
*/ |
|
91
|
6 |
|
public function add($element): bool |
|
92
|
|
|
{ |
|
93
|
6 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'add()', '$collection[] = $value'), E_USER_DEPRECATED); |
|
94
|
|
|
|
|
95
|
6 |
|
$this->elements[] = $element; |
|
96
|
|
|
|
|
97
|
6 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param int|string $key |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
* |
|
106
|
|
|
* @phpstan-param TKey $key |
|
107
|
|
|
* @phpstan-param TValue $value |
|
108
|
|
|
* |
|
109
|
|
|
* @deprecated |
|
110
|
|
|
*/ |
|
111
|
3 |
|
public function set($key, $value) |
|
112
|
|
|
{ |
|
113
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'set()', '$collection[$key] = $value'), E_USER_DEPRECATED); |
|
114
|
|
|
|
|
115
|
3 |
|
$this->offsetSet($key, $value); |
|
116
|
3 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param int|string $key |
|
120
|
|
|
* |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
* |
|
123
|
|
|
* @phpstan-param TKey $key |
|
124
|
|
|
* |
|
125
|
|
|
* @phpstan-return TValue|null |
|
126
|
|
|
* |
|
127
|
|
|
* @deprecated |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function get($key) |
|
130
|
|
|
{ |
|
131
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'get()', '$collection[$key]'), E_USER_DEPRECATED); |
|
132
|
|
|
|
|
133
|
3 |
|
return $this->offsetGet($key); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param int|string $key |
|
138
|
|
|
* |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
* |
|
141
|
|
|
* @phpstan-param TKey $key |
|
142
|
|
|
* |
|
143
|
|
|
* @phpstan-return TValue|null |
|
144
|
|
|
* |
|
145
|
|
|
* @deprecated |
|
146
|
|
|
*/ |
|
147
|
6 |
|
public function remove($key) |
|
148
|
|
|
{ |
|
149
|
6 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'remove()', 'unset($collection[$key])'), E_USER_DEPRECATED); |
|
150
|
|
|
|
|
151
|
6 |
|
if (!\array_key_exists($key, $this->elements)) { |
|
152
|
6 |
|
return; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
6 |
|
$removed = $this->elements[$key]; |
|
156
|
6 |
|
unset($this->elements[$key]); |
|
157
|
|
|
|
|
158
|
6 |
|
return $removed; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return bool |
|
163
|
|
|
* |
|
164
|
|
|
* @deprecated |
|
165
|
|
|
*/ |
|
166
|
3 |
|
public function isEmpty(): bool |
|
167
|
|
|
{ |
|
168
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'isEmpty()', 'count($collection) === 0'), E_USER_DEPRECATED); |
|
169
|
|
|
|
|
170
|
3 |
|
return empty($this->elements); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param mixed $element |
|
175
|
|
|
* |
|
176
|
|
|
* @return bool |
|
177
|
|
|
* |
|
178
|
|
|
* @phpstan-param TValue $element |
|
179
|
|
|
* |
|
180
|
|
|
* @deprecated |
|
181
|
|
|
*/ |
|
182
|
3 |
|
public function contains($element): bool |
|
183
|
|
|
{ |
|
184
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'contains()', 'in_array($value, $collection->toArray(), true)'), E_USER_DEPRECATED); |
|
185
|
|
|
|
|
186
|
3 |
|
return \in_array($element, $this->elements, true); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param mixed $element |
|
191
|
|
|
* |
|
192
|
|
|
* @return mixed|false |
|
193
|
|
|
* |
|
194
|
|
|
* @phpstan-param TValue $element |
|
195
|
|
|
* |
|
196
|
|
|
* @deprecated |
|
197
|
|
|
*/ |
|
198
|
3 |
|
public function indexOf($element) |
|
199
|
|
|
{ |
|
200
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'indexOf()', 'array_search($value, $collection->toArray(), true)'), E_USER_DEPRECATED); |
|
201
|
|
|
|
|
202
|
3 |
|
return \array_search($element, $this->elements, true); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param int|string $key |
|
207
|
|
|
* |
|
208
|
|
|
* @return bool |
|
209
|
|
|
* |
|
210
|
|
|
* @phpstan-param TKey $key |
|
211
|
|
|
* |
|
212
|
|
|
* @deprecated |
|
213
|
|
|
*/ |
|
214
|
3 |
|
public function containsKey($key): bool |
|
215
|
|
|
{ |
|
216
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4, use "%s" instead.', self::class, 'containsKey()', 'isset($collection[$key])'), E_USER_DEPRECATED); |
|
217
|
|
|
|
|
218
|
3 |
|
return \array_key_exists($key, $this->elements); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Count elements of an object |
|
223
|
|
|
* |
|
224
|
|
|
* @return int The count as an integer. |
|
225
|
|
|
*/ |
|
226
|
120 |
|
public function count(): int |
|
227
|
|
|
{ |
|
228
|
120 |
|
return \count($this->elements); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Whether an offset exists |
|
233
|
|
|
* |
|
234
|
|
|
* @param int|string $offset An offset to check for. |
|
235
|
|
|
* |
|
236
|
|
|
* @return bool true on success or false on failure. |
|
237
|
|
|
* |
|
238
|
|
|
* @phpstan-param TKey $offset |
|
239
|
|
|
*/ |
|
240
|
3 |
|
public function offsetExists($offset): bool |
|
241
|
|
|
{ |
|
242
|
3 |
|
return \array_key_exists($offset, $this->elements); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Offset to retrieve |
|
247
|
|
|
* |
|
248
|
|
|
* @param int|string $offset |
|
249
|
|
|
* |
|
250
|
|
|
* @return mixed|null |
|
251
|
|
|
* |
|
252
|
|
|
* @phpstan-param TKey $offset |
|
253
|
|
|
* |
|
254
|
|
|
* @phpstan-return TValue|null |
|
255
|
|
|
*/ |
|
256
|
6 |
|
public function offsetGet($offset) |
|
257
|
|
|
{ |
|
258
|
6 |
|
return $this->elements[$offset] ?? null; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Offset to set |
|
263
|
|
|
* |
|
264
|
|
|
* @param int|string|null $offset The offset to assign the value to. |
|
265
|
|
|
* @param mixed $value The value to set. |
|
266
|
|
|
* |
|
267
|
|
|
* @return void |
|
268
|
|
|
* |
|
269
|
|
|
* @phpstan-param TKey|null $offset |
|
270
|
|
|
* @phpstan-param TValue $value |
|
271
|
|
|
*/ |
|
272
|
2958 |
|
public function offsetSet($offset, $value) |
|
273
|
|
|
{ |
|
274
|
2958 |
|
if ($offset === null) { |
|
275
|
2955 |
|
$this->elements[] = $value; |
|
276
|
|
|
} else { |
|
277
|
6 |
|
$this->elements[$offset] = $value; |
|
278
|
|
|
} |
|
279
|
2958 |
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Offset to unset |
|
283
|
|
|
* |
|
284
|
|
|
* @param int|string $offset The offset to unset. |
|
285
|
|
|
* |
|
286
|
|
|
* @return void |
|
287
|
|
|
* |
|
288
|
|
|
* @phpstan-param TKey $offset |
|
289
|
|
|
*/ |
|
290
|
6 |
|
public function offsetUnset($offset) |
|
291
|
|
|
{ |
|
292
|
6 |
|
if (!\array_key_exists($offset, $this->elements)) { |
|
293
|
3 |
|
return; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
6 |
|
unset($this->elements[$offset]); |
|
297
|
6 |
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Returns a subset of the array |
|
301
|
|
|
* |
|
302
|
|
|
* @param int $offset |
|
303
|
|
|
* @param int|null $length |
|
304
|
|
|
* |
|
305
|
|
|
* @return array<int|string, mixed> |
|
|
|
|
|
|
306
|
|
|
* |
|
307
|
|
|
* @phpstan-return array<TKey, TValue> |
|
308
|
|
|
*/ |
|
309
|
108 |
|
public function slice(int $offset, ?int $length = null): array |
|
310
|
|
|
{ |
|
311
|
108 |
|
return \array_slice($this->elements, $offset, $length, true); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @return array<int|string, mixed> |
|
|
|
|
|
|
316
|
|
|
* |
|
317
|
|
|
* @phpstan-return array<TKey, TValue> |
|
318
|
|
|
*/ |
|
319
|
2820 |
|
public function toArray(): array |
|
320
|
|
|
{ |
|
321
|
2820 |
|
return $this->elements; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param array<int|string, mixed> $elements |
|
326
|
|
|
* |
|
327
|
|
|
* @return $this |
|
328
|
|
|
* |
|
329
|
|
|
* @phpstan-param array<TKey, TValue> $elements |
|
330
|
|
|
* |
|
331
|
|
|
* @deprecated |
|
332
|
|
|
*/ |
|
333
|
3 |
|
public function replaceWith(array $elements) |
|
334
|
|
|
{ |
|
335
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4.', self::class, 'replaceWith()'), E_USER_DEPRECATED); |
|
336
|
|
|
|
|
337
|
3 |
|
$this->elements = $elements; |
|
338
|
|
|
|
|
339
|
3 |
|
return $this; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @deprecated |
|
344
|
|
|
* |
|
345
|
|
|
* @return void |
|
346
|
|
|
*/ |
|
347
|
3 |
|
public function removeGaps() |
|
348
|
|
|
{ |
|
349
|
3 |
|
@trigger_error(sprintf('The "%s:%s" method is deprecated since league/commonmark 1.4.', self::class, 'removeGaps()'), E_USER_DEPRECATED); |
|
350
|
|
|
|
|
351
|
3 |
|
$this->elements = \array_filter($this->elements); |
|
352
|
3 |
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.