run:R W Run
4.79 KB
2026-03-11 16:18:52
R W Run
error_log
📄SplFixedArray.php
1<?php
2
3if (class_exists('SplFixedArray')) {
4 return;
5}
6
7/**
8 * The SplFixedArray class provides the main functionalities of array. The
9 * main differences between a SplFixedArray and a normal PHP array is that
10 * the SplFixedArray is of fixed length and allows only integers within
11 * the range as indexes. The advantage is that it allows a faster array
12 * implementation.
13 */
14class SplFixedArray implements Iterator, ArrayAccess, Countable
15{
16 /** @var array<int, mixed> */
17 private $internalArray = array();
18
19 /** @var int $size */
20 private $size = 0;
21
22 /**
23 * SplFixedArray constructor.
24 * @param int $size
25 */
26 public function __construct($size = 0)
27 {
28 $this->size = $size;
29 $this->internalArray = array();
30 }
31
32 /**
33 * @return int
34 */
35 #[\ReturnTypeWillChange]
36 public function count()
37 {
38 return count($this->internalArray);
39 }
40
41 /**
42 * @return array
43 */
44 public function toArray()
45 {
46 ksort($this->internalArray);
47 return (array) $this->internalArray;
48 }
49
50 /**
51 * @param array $array
52 * @param bool $save_indexes
53 * @return SplFixedArray
54 * @psalm-suppress MixedAssignment
55 */
56 public static function fromArray(array $array, $save_indexes = true)
57 {
58 $self = new SplFixedArray(count($array));
59 if($save_indexes) {
60 foreach($array as $key => $value) {
61 $self[(int) $key] = $value;
62 }
63 } else {
64 $i = 0;
65 foreach (array_values($array) as $value) {
66 $self[$i] = $value;
67 $i++;
68 }
69 }
70 return $self;
71 }
72
73 /**
74 * @return int
75 */
76 #[\ReturnTypeWillChange]
77 public function getSize()
78 {
79 return $this->size;
80 }
81
82 /**
83 * @param int $size
84 * @return bool
85 */
86 #[\ReturnTypeWillChange]
87 public function setSize($size)
88 {
89 $this->size = $size;
90 return true;
91 }
92
93 /**
94 * @param string|int $index
95 * @return bool
96 */
97 #[\ReturnTypeWillChange]
98 public function offsetExists($index)
99 {
100 return array_key_exists((int) $index, $this->internalArray);
101 }
102
103 /**
104 * @param string|int $index
105 * @return mixed
106 */
107 #[\ReturnTypeWillChange]
108 public function offsetGet($index)
109 {
110 /** @psalm-suppress MixedReturnStatement */
111 return $this->internalArray[(int) $index];
112 }
113
114 /**
115 * @param string|int $index
116 * @param mixed $newval
117 * @psalm-suppress MixedAssignment
118 */
119 #[\ReturnTypeWillChange]
120 public function offsetSet($index, $newval)
121 {
122 $this->internalArray[(int) $index] = $newval;
123 }
124
125 /**
126 * @param string|int $index
127 */
128 #[\ReturnTypeWillChange]
129 public function offsetUnset($index)
130 {
131 unset($this->internalArray[(int) $index]);
132 }
133
134 /**
135 * Rewind iterator back to the start
136 * @link https://php.net/manual/en/splfixedarray.rewind.php
137 * @return void
138 * @since 5.3.0
139 */
140 #[\ReturnTypeWillChange]
141 public function rewind()
142 {
143 reset($this->internalArray);
144 }
145
146 /**
147 * Return current array entry
148 * @link https://php.net/manual/en/splfixedarray.current.php
149 * @return mixed The current element value.
150 * @since 5.3.0
151 */
152 #[\ReturnTypeWillChange]
153 public function current()
154 {
155 /** @psalm-suppress MixedReturnStatement */
156 return current($this->internalArray);
157 }
158
159 /**
160 * Return current array index
161 * @return int The current array index.
162 */
163 #[\ReturnTypeWillChange]
164 public function key()
165 {
166 return key($this->internalArray);
167 }
168
169 /**
170 * @return void
171 */
172 #[\ReturnTypeWillChange]
173 public function next()
174 {
175 next($this->internalArray);
176 }
177
178 /**
179 * Check whether the array contains more elements
180 * @link https://php.net/manual/en/splfixedarray.valid.php
181 * @return bool true if the array contains any more elements, false otherwise.
182 */
183 #[\ReturnTypeWillChange]
184 public function valid()
185 {
186 if (empty($this->internalArray)) {
187 return false;
188 }
189 $result = next($this->internalArray) !== false;
190 prev($this->internalArray);
191 return $result;
192 }
193
194 public function __sleep()
195 {
196 return $this->internalArray;
197 }
198
199 /**
200 * Do nothing.
201 */
202 public function __wakeup()
203 {
204 // NOP
205 }
206
207 public function __serialize()
208 {
209 return array_values($this->internalArray);
210 }
211
212 public function __unserialize(array $data)
213 {
214 $length = count($data);
215 $values = array_values($data);
216 for ($i = 0; $i < $length; ++$i) {
217 $this->internalArray[$i] = $values[$i];
218 }
219 $this->size = $length;
220 }
221}
222