|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composite Utils package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @package spaark/composite-utils |
|
11
|
|
|
* @author Emily Shepherd <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Model\Collection\ListCollection; |
|
16
|
|
|
|
|
17
|
|
|
use IteratorIterator; |
|
18
|
|
|
use SplFixedArray; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Represents an List stored in a PHP array |
|
22
|
|
|
*/ |
|
23
|
|
|
class FixedList extends AbstractList |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ValueType[] |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $data; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $pointer = 0; |
|
34
|
|
|
|
|
35
|
30 |
|
public function __construct(int $size = 0) |
|
36
|
|
|
{ |
|
37
|
30 |
|
$this->data = new SplFixedArray($size); |
|
|
|
|
|
|
38
|
30 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
27 |
|
public function add($item) |
|
44
|
|
|
{ |
|
45
|
27 |
|
$this->data[$this->pointer++] = $item; |
|
46
|
27 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
5 |
|
public function get(int $index) |
|
52
|
|
|
{ |
|
53
|
5 |
|
return $this->data[$index]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function splice |
|
60
|
|
|
( |
|
61
|
|
|
int $offset, |
|
62
|
|
|
?int $length = null, |
|
63
|
|
|
array $replacement = [] |
|
64
|
|
|
) |
|
65
|
|
|
{ |
|
66
|
|
|
$end = $offset + $length; |
|
67
|
|
|
|
|
68
|
|
|
for ($i = $offset, $j = 0; $i < $end; $i++, $j++) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->data[$i] = $replacement[$j] ?? null; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function set(int $index, $value) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$this->data[$index] = $value; |
|
80
|
2 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function remove(int $item) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$this->set($item, null); |
|
|
|
|
|
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
11 |
|
public function getIterator() |
|
94
|
|
|
{ |
|
95
|
11 |
|
return new IteratorIterator($this->data); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
6 |
|
public function size() : int |
|
102
|
|
|
{ |
|
103
|
6 |
|
return count($this->data); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Resizes the FixedList, throwing away any unused elements |
|
108
|
|
|
* |
|
109
|
|
|
* @param int $size The new size |
|
110
|
|
|
*/ |
|
111
|
23 |
|
public function resize(int $size) |
|
112
|
|
|
{ |
|
113
|
23 |
|
$this->data->setSize($size); |
|
|
|
|
|
|
114
|
23 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the current pointer position |
|
118
|
|
|
* |
|
119
|
|
|
* @return int |
|
120
|
|
|
*/ |
|
121
|
22 |
|
public function getCurrentPosition() : int |
|
122
|
|
|
{ |
|
123
|
22 |
|
return $this->pointer; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Resizes to the current pointer |
|
128
|
|
|
*/ |
|
129
|
22 |
|
public function resizeToFull() |
|
130
|
|
|
{ |
|
131
|
22 |
|
$this->resize($this->getCurrentPosition()); |
|
132
|
22 |
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..