1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[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 League\CommonMark\Util; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Array collection |
18
|
|
|
* |
19
|
|
|
* Provides a wrapper around a standard PHP array. |
20
|
|
|
* |
21
|
|
|
* @internal |
22
|
|
|
* |
23
|
|
|
* @phpstan-template T |
24
|
|
|
* @phpstan-implements \IteratorAggregate<int, T> |
25
|
|
|
* @phpstan-implements \ArrayAccess<int, T> |
26
|
|
|
*/ |
27
|
|
|
final class ArrayCollection implements \IteratorAggregate, \Countable, \ArrayAccess |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array<int, mixed> |
31
|
|
|
* @phpstan-var array<int, T> |
32
|
|
|
*/ |
33
|
|
|
private $elements; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor |
37
|
|
|
* |
38
|
|
|
* @param array<int|string, mixed> $elements |
39
|
|
|
* |
40
|
|
|
* @phpstan-param array<int, T> $elements |
41
|
|
|
*/ |
42
|
375 |
|
public function __construct(array $elements = []) |
43
|
|
|
{ |
44
|
375 |
|
$this->elements = $elements; |
45
|
375 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return mixed|false |
49
|
|
|
* |
50
|
|
|
* @phpstan-return T|false |
51
|
|
|
*/ |
52
|
114 |
|
public function first() |
53
|
|
|
{ |
54
|
114 |
|
return \reset($this->elements); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return mixed|false |
59
|
|
|
* |
60
|
|
|
* @phpstan-return T|false |
61
|
|
|
*/ |
62
|
3 |
|
public function last() |
63
|
|
|
{ |
64
|
3 |
|
return \end($this->elements); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Retrieve an external iterator |
69
|
|
|
* |
70
|
|
|
* @return \ArrayIterator<int, mixed> |
71
|
|
|
* |
72
|
|
|
* @phpstan-return \ArrayIterator<int, T> |
73
|
|
|
*/ |
74
|
81 |
|
public function getIterator(): \ArrayIterator |
75
|
|
|
{ |
76
|
81 |
|
return new \ArrayIterator($this->elements); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Count elements of an object |
81
|
|
|
* |
82
|
|
|
* @return int The count as an integer. |
83
|
|
|
*/ |
84
|
111 |
|
public function count(): int |
85
|
|
|
{ |
86
|
111 |
|
return \count($this->elements); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Whether an offset exists |
91
|
|
|
* |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
* |
94
|
|
|
* @phpstan-param int $offset |
95
|
|
|
*/ |
96
|
3 |
|
public function offsetExists($offset): bool |
97
|
|
|
{ |
98
|
3 |
|
return \array_key_exists($offset, $this->elements); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Offset to retrieve |
103
|
|
|
* |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
* |
106
|
|
|
* @phpstan-param int $offset |
107
|
|
|
* |
108
|
|
|
* @phpstan-return T|null |
109
|
|
|
*/ |
110
|
3 |
|
public function offsetGet($offset) |
111
|
|
|
{ |
112
|
3 |
|
return $this->elements[$offset] ?? null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Offset to set |
117
|
|
|
* |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
* |
120
|
|
|
* @phpstan-param int|null $offset |
121
|
|
|
* @phpstan-param T $value |
122
|
|
|
*/ |
123
|
342 |
|
public function offsetSet($offset, $value): void |
124
|
|
|
{ |
125
|
342 |
|
if ($offset === null) { |
126
|
342 |
|
$this->elements[] = $value; |
127
|
|
|
} else { |
128
|
3 |
|
$this->elements[$offset] = $value; |
129
|
|
|
} |
130
|
342 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Offset to unset |
134
|
|
|
* |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
* |
137
|
|
|
* @phpstan-param int $offset |
138
|
|
|
*/ |
139
|
6 |
|
public function offsetUnset($offset): void |
140
|
|
|
{ |
141
|
6 |
|
if (! \array_key_exists($offset, $this->elements)) { |
142
|
6 |
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
6 |
|
unset($this->elements[$offset]); |
146
|
6 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns a subset of the array |
150
|
|
|
* |
151
|
|
|
* @return array<int, mixed> |
152
|
|
|
* |
153
|
|
|
* @phpstan-return array<int, T> |
154
|
|
|
*/ |
155
|
99 |
|
public function slice(int $offset, ?int $length = null): array |
156
|
|
|
{ |
157
|
99 |
|
return \array_slice($this->elements, $offset, $length, true); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array<int, mixed> |
162
|
|
|
* |
163
|
|
|
* @phpstan-return array<int, T> |
164
|
|
|
*/ |
165
|
171 |
|
public function toArray(): array |
166
|
|
|
{ |
167
|
171 |
|
return $this->elements; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|