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
|
37 |
|
public function __construct(int $size = 0) |
36
|
|
|
{ |
37
|
37 |
|
$this->data = new SplFixedArray($size); |
|
|
|
|
38
|
37 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
34 |
|
public function add($item) |
44
|
|
|
{ |
45
|
34 |
|
$this->data[$this->pointer++] = $item; |
46
|
34 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
10 |
|
public function get(int $index) |
52
|
|
|
{ |
53
|
10 |
|
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
|
17 |
|
public function getIterator() |
94
|
|
|
{ |
95
|
17 |
|
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
|
31 |
|
public function resize(int $size) |
112
|
|
|
{ |
113
|
31 |
|
$this->data->setSize($size); |
|
|
|
|
114
|
31 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the current pointer position |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
30 |
|
public function getCurrentPosition() : int |
122
|
|
|
{ |
123
|
30 |
|
return $this->pointer; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Resizes to the current pointer |
128
|
|
|
*/ |
129
|
30 |
|
public function resizeToFull() |
130
|
|
|
{ |
131
|
30 |
|
$this->resize($this->getCurrentPosition()); |
132
|
30 |
|
} |
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..