1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\Iterators;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Spl\DataStructures\Traits\ArrayConversionTrait;
|
19
|
|
|
use O2System\Spl\DataStructures\Traits\ArrayFunctionsTrait;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* O2System Standard PHP Libraries ArrayIterator
|
23
|
|
|
*
|
24
|
|
|
* @package O2System\Core\SPL
|
25
|
|
|
*/
|
26
|
|
|
class ArrayIterator extends \ArrayIterator implements \JsonSerializable
|
27
|
|
|
{
|
28
|
|
|
use ArrayConversionTrait;
|
29
|
|
|
use ArrayFunctionsTrait;
|
30
|
|
|
|
31
|
|
|
// ------------------------------------------------------------------------
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* ArrayIterator::first
|
35
|
|
|
*
|
36
|
|
|
* Rewind the iterator to the first offset
|
37
|
|
|
*
|
38
|
|
|
* @return mixed
|
39
|
|
|
*/
|
40
|
|
|
public function first()
|
41
|
|
|
{
|
42
|
|
|
$this->rewind();
|
43
|
|
|
|
44
|
|
|
return $this->current();
|
45
|
|
|
}
|
46
|
|
|
|
47
|
|
|
// ------------------------------------------------------------------------
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* ArrayIterator::last
|
51
|
|
|
*
|
52
|
|
|
* Forward the iterator to the first offset
|
53
|
|
|
*
|
54
|
|
|
* @return mixed
|
55
|
|
|
*/
|
56
|
|
|
public function last()
|
57
|
|
|
{
|
58
|
|
|
$this->seek(($this->count()) - 1);
|
59
|
|
|
|
60
|
|
|
return $this->current();
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* ArrayIterator::push
|
65
|
|
|
*
|
66
|
|
|
* Push a value for an offset
|
67
|
|
|
*
|
68
|
|
|
* @param mixed $value The new value to store at the index.
|
69
|
|
|
*/
|
70
|
|
|
public function push($value)
|
71
|
|
|
{
|
72
|
|
|
$this->offsetSet($this->count(), $value);
|
73
|
|
|
$this->seek($this->count() - 1);
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
// ------------------------------------------------------------------------
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* ArrayIterator::unshift
|
80
|
|
|
*
|
81
|
|
|
* Prepend one or more value to the beginning of the storage
|
82
|
|
|
*
|
83
|
|
|
* @param mixed $value The new value to store at the index.
|
84
|
|
|
*/
|
85
|
|
|
public function unshift($value)
|
86
|
|
|
{
|
87
|
|
|
$storage = $this->getArrayCopy();
|
88
|
|
|
array_unshift($storage, $value);
|
89
|
|
|
|
90
|
|
|
parent::__construct($storage);
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
// ------------------------------------------------------------------------
|
94
|
|
|
|
95
|
|
|
public function shift()
|
96
|
|
|
{
|
97
|
|
|
$storage = $this->getArrayCopy();
|
98
|
|
|
array_shift($storage);
|
99
|
|
|
|
100
|
|
|
parent::__construct($storage);
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
public function pop()
|
104
|
|
|
{
|
105
|
|
|
$storage = $this->getArrayCopy();
|
106
|
|
|
array_pop($storage);
|
107
|
|
|
|
108
|
|
|
parent::__construct($storage);
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
/**
|
112
|
|
|
* ArrayIterator::has
|
113
|
|
|
*
|
114
|
|
|
* Checks if a value exists in the storage.
|
115
|
|
|
*
|
116
|
|
|
* @param mixed $needle The searched value.
|
117
|
|
|
* @param bool $strict If the third parameter strict is set to TRUE then the in_array() function will also check
|
118
|
|
|
* the types of the needle in the haystack.
|
119
|
|
|
*
|
120
|
|
|
* @return bool
|
121
|
|
|
*/
|
122
|
|
|
public function has($needle, $strict = false)
|
123
|
|
|
{
|
124
|
|
|
return (bool)in_array($needle, $this->getArrayCopy(), $strict);
|
125
|
|
|
}
|
126
|
|
|
|
127
|
|
|
// ------------------------------------------------------------------------
|
128
|
|
|
|
129
|
|
|
/**
|
130
|
|
|
* ArrayIterator::search
|
131
|
|
|
*
|
132
|
|
|
* Searches the storage for a given value and returns the first corresponding key if successful.
|
133
|
|
|
*
|
134
|
|
|
* @param mixed $needle The searched value.
|
135
|
|
|
* @param bool $seek Perform the iterator seek method
|
136
|
|
|
*
|
137
|
|
|
* @return mixed Returns the key for needle if it is found in the array, FALSE otherwise.
|
138
|
|
|
*/
|
139
|
|
|
public function search($needle, $seek = false)
|
140
|
|
|
{
|
141
|
|
|
if (false !== ($position = array_search($needle, $this->getArrayCopy()))) {
|
142
|
|
|
if ($seek === true) {
|
143
|
|
|
$this->seek($position);
|
|
|
|
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
return $position;
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
return false;
|
150
|
|
|
}
|
151
|
|
|
|
152
|
|
|
// ------------------------------------------------------------------------
|
153
|
|
|
|
154
|
|
|
/**
|
155
|
|
|
* ArrayIterator::unique
|
156
|
|
|
*
|
157
|
|
|
* Removes duplicate values from the storage
|
158
|
|
|
*
|
159
|
|
|
* @see http://php.net/manual/en/function.array-unique.php
|
160
|
|
|
*
|
161
|
|
|
* @param int $sortFlags The optional second parameter sort_flags may be used to modify the sorting behavior
|
162
|
|
|
* @param bool $exchangeArray Exchange the array into the filtered array.
|
163
|
|
|
*
|
164
|
|
|
* @return array Returns the filtered array.
|
165
|
|
|
*/
|
166
|
|
|
public function unique($sortFlags = SORT_STRING, $exchangeArray = false)
|
167
|
|
|
{
|
168
|
|
|
$unique = array_unique($this->getArrayCopy(), $sortFlags);
|
169
|
|
|
|
170
|
|
|
if ($exchangeArray) {
|
171
|
|
|
$this->exchangeArray($unique);
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
return $unique;
|
175
|
|
|
}
|
176
|
|
|
|
177
|
|
|
// ------------------------------------------------------------------------
|
178
|
|
|
|
179
|
|
|
/**
|
180
|
|
|
* ArrayIterator::exchangeArray
|
181
|
|
|
*
|
182
|
|
|
* Exchange the array for another one.
|
183
|
|
|
*
|
184
|
|
|
* @param array $values The new array or object to exchange with the current array.
|
185
|
|
|
*
|
186
|
|
|
*
|
187
|
|
|
* @return array of the old storage.
|
188
|
|
|
* @since 5.1.0
|
189
|
|
|
*/
|
190
|
|
|
public function exchangeArray(array $values)
|
191
|
|
|
{
|
192
|
|
|
$oldStorage = $this->getArrayCopy();
|
193
|
|
|
parent::__construct($values);
|
194
|
|
|
|
195
|
|
|
return $oldStorage;
|
196
|
|
|
}
|
197
|
|
|
|
198
|
|
|
// ------------------------------------------------------------------------
|
199
|
|
|
|
200
|
|
|
/**
|
201
|
|
|
* ArrayIterator::merge
|
202
|
|
|
*
|
203
|
|
|
* Merge array of values into the storage
|
204
|
|
|
*
|
205
|
|
|
* @param array $values Variable list of arrays to merge.
|
206
|
|
|
*
|
207
|
|
|
* @return array The array merged copy of the resulting array
|
208
|
|
|
*/
|
209
|
|
|
public function merge(array $values)
|
210
|
|
|
{
|
211
|
|
|
$storage = $this->getArrayCopy();
|
212
|
|
|
$storage = array_merge($storage, $values);
|
213
|
|
|
|
214
|
|
|
$this->exchangeArray($storage);
|
215
|
|
|
|
216
|
|
|
return $storage;
|
217
|
|
|
}
|
218
|
|
|
|
219
|
|
|
// ------------------------------------------------------------------------
|
220
|
|
|
|
221
|
|
|
/**
|
222
|
|
|
* ArrayIterator::remove
|
223
|
|
|
*
|
224
|
|
|
* Remove a given needle from the storage.
|
225
|
|
|
*
|
226
|
|
|
* @param mixed $needle The searched value.
|
227
|
|
|
*
|
228
|
|
|
* @return void
|
229
|
|
|
*/
|
230
|
|
|
public function remove($needle)
|
231
|
|
|
{
|
232
|
|
|
if (false !== ($position = array_search($needle, $this->getArrayCopy()))) {
|
233
|
|
|
$firstStorage = array_splice($this->getArrayCopy(), 0, $position);
|
|
|
|
|
234
|
|
|
$endStorage = array_splice($this->getArrayCopy(), $position + 1);
|
235
|
|
|
|
236
|
|
|
parent::__construct(array_merge($firstStorage, $endStorage));
|
237
|
|
|
}
|
238
|
|
|
}
|
239
|
|
|
|
240
|
|
|
// ------------------------------------------------------------------------
|
241
|
|
|
|
242
|
|
|
/**
|
243
|
|
|
* ArrayIterator::jsonSerialize
|
244
|
|
|
*
|
245
|
|
|
* Specify data which should be serialized to JSON
|
246
|
|
|
*
|
247
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
|
248
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>,
|
249
|
|
|
* which is a value of any type other than a resource.
|
250
|
|
|
* @since 5.4.0
|
251
|
|
|
*/
|
252
|
|
|
public function jsonSerialize()
|
253
|
|
|
{
|
254
|
|
|
return $this->getArrayCopy();
|
255
|
|
|
}
|
256
|
|
|
} |