1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Incoming |
4
|
|
|
* |
5
|
|
|
* @author Trevor Suarez (Rican7) |
6
|
|
|
* @copyright (c) Trevor Suarez |
7
|
|
|
* @link https://github.com/Rican7/incoming |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Incoming\Structure; |
14
|
|
|
|
15
|
|
|
use Incoming\Structure\Exception\ReadOnlyException; |
16
|
|
|
use Incoming\Structure\Iterator\ReadOnlyIterator; |
17
|
|
|
use Iterator; |
18
|
|
|
use SplFixedArray; |
19
|
|
|
use Traversable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A fixed-size, read-only data-structure. |
23
|
|
|
*/ |
24
|
|
|
class FixedList implements Structure |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Properties |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The underlying decorated data structure. |
33
|
|
|
* |
34
|
|
|
* @var SplFixedArray |
35
|
|
|
*/ |
36
|
|
|
private $decorated; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Methods |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param int $size The size (length) of the list. |
47
|
|
|
*/ |
48
|
57 |
|
public function __construct(int $size = 0) |
49
|
|
|
{ |
50
|
57 |
|
$this->decorated = new SplFixedArray($size); |
51
|
57 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create from data in an array. |
55
|
|
|
* |
56
|
|
|
* @param array $data The data to create from. |
57
|
|
|
* @return static The resulting data-structure. |
58
|
|
|
*/ |
59
|
48 |
|
public static function fromArray(array $data): self |
60
|
|
|
{ |
61
|
48 |
|
$fixed_list = new static(); |
62
|
|
|
|
63
|
48 |
|
$fixed_list->decorated = SplFixedArray::fromArray($data, false); |
64
|
|
|
|
65
|
48 |
|
return $fixed_list; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create from data in a Traversable instance. |
70
|
|
|
* |
71
|
|
|
* @param Traversable $data The data to create from. |
72
|
|
|
* @return static The resulting data-structure. |
73
|
|
|
*/ |
74
|
24 |
|
public static function fromTraversable(Traversable $data): self |
75
|
|
|
{ |
76
|
24 |
|
return static::fromArray( |
77
|
24 |
|
iterator_to_array($data) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if a given index exists in the list. |
83
|
|
|
* |
84
|
|
|
* @param int $index The index to check for existence. |
85
|
|
|
* @return bool True if the index exists, false otherwise. |
86
|
|
|
*/ |
87
|
3 |
|
public function exists(int $index): bool |
88
|
|
|
{ |
89
|
3 |
|
return $this->offsetExists($index); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get a value in the list by index. |
94
|
|
|
* |
95
|
|
|
* @param int $index The index to get the value for. |
96
|
|
|
* @param mixed $default_val The default value to return if the index does |
97
|
|
|
* not exist. |
98
|
|
|
* @return mixed The resulting value. |
99
|
|
|
*/ |
100
|
3 |
|
public function get(int $index, $default_val = null) |
101
|
|
|
{ |
102
|
3 |
|
if ($this->offsetExists($index)) { |
103
|
3 |
|
return $this->offsetGet($index); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
return $default_val; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check if the list is empty. |
111
|
|
|
* |
112
|
|
|
* @return bool True if the list is empty, false otherwise. |
113
|
|
|
*/ |
114
|
3 |
|
public function isEmpty(): bool |
115
|
|
|
{ |
116
|
3 |
|
return ($this->count() === 0); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get a representation of the list as an array. |
121
|
|
|
* |
122
|
|
|
* @return array The array representation of the list. |
123
|
|
|
*/ |
124
|
3 |
|
public function toArray(): array |
125
|
|
|
{ |
126
|
3 |
|
return $this->decorated->toArray(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the number of entries in the list. |
131
|
|
|
* |
132
|
|
|
* @return int The number of entries in the list. |
133
|
|
|
*/ |
134
|
6 |
|
public function count(): int |
135
|
|
|
{ |
136
|
6 |
|
return count($this->decorated); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get an iterator instance over the underlying data. |
141
|
|
|
* |
142
|
|
|
* @return Iterator An iterator scoped to the list's data. |
143
|
|
|
*/ |
144
|
6 |
|
public function getIterator(): Iterator |
145
|
|
|
{ |
146
|
6 |
|
return new ReadOnlyIterator( |
147
|
6 |
|
$this->decorated |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check whether an offset exists. |
153
|
|
|
* |
154
|
|
|
* @param mixed $offset The offset to check for. |
155
|
|
|
* @return bool True if the offset exists, false otherwise. |
156
|
|
|
*/ |
157
|
9 |
|
public function offsetExists($offset): bool |
158
|
|
|
{ |
159
|
9 |
|
return $this->decorated->offsetExists($offset); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the value at the given offset. |
164
|
|
|
* |
165
|
|
|
* @param mixed $offset The offset to get the value for. |
166
|
|
|
* @return mixed The resulting value. |
167
|
|
|
*/ |
168
|
9 |
|
public function offsetGet($offset) |
169
|
|
|
{ |
170
|
9 |
|
return $this->decorated->offsetGet($offset); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set a value at the given offset. |
175
|
|
|
* |
176
|
|
|
* @internal |
177
|
|
|
* |
178
|
|
|
* @param mixed $offset The offset to set the value for. |
179
|
|
|
* @param mixed $value The value to set. |
180
|
|
|
* @throws ReadOnlyException External modification is not allowed. |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
3 |
|
public function offsetSet($offset, $value) |
184
|
|
|
{ |
185
|
3 |
|
throw ReadOnlyException::forAttribute($offset, $value); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Remove the item at the given offset. |
190
|
|
|
* |
191
|
|
|
* @internal |
192
|
|
|
* |
193
|
|
|
* @param mixed $offset The offset to unset. |
194
|
|
|
* @throws ReadOnlyException External modification is not allowed. |
195
|
|
|
* @return void |
196
|
|
|
*/ |
197
|
3 |
|
public function offsetUnset($offset) |
198
|
|
|
{ |
199
|
3 |
|
throw ReadOnlyException::forAttribute($offset); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|