1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MaxGoryunov\SavingIterator\Src; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Countable; |
7
|
|
|
use Iterator; |
8
|
|
|
use Serializable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Safe array iterator. Copies array when cloned. |
12
|
|
|
* @template TKey |
13
|
|
|
* @template TValue |
14
|
|
|
* @implements ArrayAccess<TKey, TValue> |
15
|
|
|
* @implements Iterator<TKey, TValue> |
16
|
|
|
* |
17
|
|
|
* @since 0.3 |
18
|
|
|
*/ |
19
|
|
|
final class SafeArrayIterator implements ArrayAccess, Countable, Iterator |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Ctor. |
24
|
|
|
* |
25
|
|
|
* @phpstan-param array<TKey, TValue> $stored |
26
|
|
|
* @param array<mixed, mixed> $stored array for stored values. |
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
/** |
30
|
|
|
* Array for stored values. |
31
|
|
|
* |
32
|
|
|
* @phpstan-var array<TKey, TValue> |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private array $stored = [] |
36
|
|
|
) { |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
* @phpstan-param TKey $offset |
42
|
|
|
* @phpstan-param TValue $value |
43
|
|
|
*/ |
44
|
|
|
public function offsetSet(mixed $offset, mixed $value): void |
45
|
|
|
{ |
46
|
|
|
$this->stored[$offset] = $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
* @phpstan-param TKey $offset |
52
|
|
|
*/ |
53
|
|
|
public function offsetGet(mixed $offset): mixed |
54
|
|
|
{ |
55
|
|
|
return $this->stored[$offset]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
* @phpstan-param TKey $offset |
61
|
|
|
*/ |
62
|
|
|
public function offsetExists(mixed $offset): bool |
63
|
|
|
{ |
64
|
|
|
return isset($this->stored[$offset]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
* @phpstan-param TKey $offset |
70
|
|
|
*/ |
71
|
|
|
public function offsetUnset(mixed $offset): void |
72
|
|
|
{ |
73
|
|
|
unset($this->stored); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
public function count(): int |
80
|
|
|
{ |
81
|
|
|
return count($this->stored); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
* @phpstan-return TValue|false |
87
|
|
|
*/ |
88
|
|
|
public function current(): mixed |
89
|
|
|
{ |
90
|
|
|
return current($this->stored); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
* @phpstan-return TKey|null |
96
|
|
|
*/ |
97
|
|
|
public function key(): mixed |
98
|
|
|
{ |
99
|
|
|
return key($this->stored); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
public function next(): void |
106
|
|
|
{ |
107
|
|
|
next($this->stored); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
|
|
public function valid(): bool |
114
|
|
|
{ |
115
|
|
|
return key($this->stored) !== null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
*/ |
121
|
|
|
public function rewind(): void |
122
|
|
|
{ |
123
|
|
|
reset($this->stored); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
* @phpstan-return array<TKey, TValue> |
129
|
|
|
*/ |
130
|
|
|
public function __serialize(): array |
131
|
|
|
{ |
132
|
|
|
return $this->stored; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
* @phpstan-param array<TKey, TValue> $data data for deserialization |
138
|
|
|
*/ |
139
|
|
|
public function __unserialize(array $data): void |
140
|
|
|
{ |
141
|
|
|
$this->stored = $data; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|