|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arrayzy; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Some methods could change the array instance itself |
|
7
|
|
|
* and some could return a new instance of ObjectOrientedArray. |
|
8
|
|
|
* This class repeats the PHP built-in functions behavior. |
|
9
|
|
|
* |
|
10
|
|
|
* @method Extend\Nested nested() |
|
11
|
|
|
* @author Victor Bocharsky <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class ArrayImitator extends AbstractArray |
|
14
|
|
|
{ |
|
15
|
|
|
// The public method list ordered by ASC |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Default value to return if no required key doesn't exist. |
|
19
|
|
|
* |
|
20
|
|
|
* @var mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $defaultValue = null; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Add a new element to the current array. |
|
27
|
|
|
* |
|
28
|
|
|
* @param mixed $element |
|
29
|
|
|
* |
|
30
|
|
|
* @return ArrayImitator The current array with added value |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function add($element) |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->elements[] = $element; |
|
35
|
|
|
|
|
36
|
1 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a chunked version of current array. |
|
41
|
|
|
* |
|
42
|
|
|
* @param int $size Size of each chunk |
|
43
|
|
|
* @param bool $preserveKeys Whether array keys are preserved or no |
|
44
|
|
|
* |
|
45
|
|
|
* @return ArrayImitator A new array of chunks from the current array |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function chunk($size, $preserveKeys = false) |
|
48
|
|
|
{ |
|
49
|
4 |
|
return new static(array_chunk($this->elements, $size, $preserveKeys)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Clear the current array. |
|
54
|
|
|
* |
|
55
|
|
|
* @return ArrayImitator The current empty array |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function clear() |
|
58
|
|
|
{ |
|
59
|
4 |
|
$this->elements = []; |
|
60
|
|
|
|
|
61
|
4 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create an array using the current array as keys and the other array as values. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $array Values array |
|
68
|
|
|
* |
|
69
|
|
|
* @return ArrayImitator A new array with values from the other array |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function combine(array $array) |
|
72
|
|
|
{ |
|
73
|
1 |
|
return new static(array_combine($this->elements, $array)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Compute the current array values which not present in the given one. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $array Array for diff |
|
80
|
|
|
* |
|
81
|
|
|
* @return ArrayImitator A new array containing all the entries from this array |
|
82
|
|
|
* that are not present in $array |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function diff(array $array) |
|
85
|
|
|
{ |
|
86
|
4 |
|
return new static(array_diff($this->elements, $array)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Filter the current array for elements satisfying the predicate $func. |
|
91
|
|
|
* |
|
92
|
|
|
* @param callable $func |
|
93
|
|
|
* |
|
94
|
|
|
* @return ArrayImitator A new array with only element satisfying $func |
|
95
|
|
|
*/ |
|
96
|
4 |
|
public function filter(callable $func) |
|
97
|
|
|
{ |
|
98
|
4 |
|
return new static(array_filter($this->elements, $func)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Exchanges all keys of current array with their associated values. |
|
103
|
|
|
* |
|
104
|
|
|
* @return ArrayImitator A new array with flipped elements |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function flip() |
|
107
|
|
|
{ |
|
108
|
4 |
|
return new static(array_flip($this->elements)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Compute the current array values which present in the given one. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $array Array for intersect |
|
115
|
|
|
* |
|
116
|
|
|
* @return ArrayImitator An array with containing all the entries from this array |
|
117
|
|
|
* that are present in $array |
|
118
|
|
|
*/ |
|
119
|
4 |
|
public function intersect(array $array) |
|
120
|
|
|
{ |
|
121
|
4 |
|
return new static(array_intersect($this->elements, $array)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Compute the current array values with additional index check |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $array Array for intersect |
|
128
|
|
|
* |
|
129
|
|
|
* @return ArrayImitator An array with containing all the entries from this array |
|
130
|
|
|
* that are present in $array. Note that the keys are also used in the comparison |
|
131
|
|
|
* unlike in intersect(). |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function intersectAssoc(array $array) |
|
134
|
|
|
{ |
|
135
|
4 |
|
return new static(array_intersect_assoc($this->elements, $array)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Compute the current array using keys for comparison which present in the given one. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $array Array for intersect |
|
142
|
|
|
* |
|
143
|
|
|
* @return ArrayImitator An array with containing all the entries from this array |
|
144
|
|
|
* which have keys that are present in $array. |
|
145
|
|
|
*/ |
|
146
|
4 |
|
public function intersectKey(array $array) |
|
147
|
|
|
{ |
|
148
|
4 |
|
return new static(array_intersect_key($this->elements, $array)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Apply the given function to the every element of the current array, |
|
153
|
|
|
* collecting the results. |
|
154
|
|
|
* |
|
155
|
|
|
* @param callable $func |
|
156
|
|
|
* |
|
157
|
|
|
* @return ArrayImitator A new array with modified elements |
|
158
|
|
|
*/ |
|
159
|
4 |
|
public function map(callable $func) |
|
160
|
|
|
{ |
|
161
|
4 |
|
return new static(array_map($func, $this->elements)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Merge the current array with the provided one. The latter array is overwriting. |
|
166
|
|
|
* |
|
167
|
|
|
* @param array $array Array to merge with (overwrites) |
|
168
|
|
|
* @param bool $recursively Whether array will be merged recursively or no |
|
169
|
|
|
* |
|
170
|
|
|
* @return ArrayImitator A new array with the keys/values from $array added |
|
171
|
|
|
*/ |
|
172
|
8 |
View Code Duplication |
public function merge(array $array, $recursively = false) |
|
173
|
|
|
{ |
|
174
|
8 |
|
if (true === $recursively) { |
|
175
|
4 |
|
return new static(array_merge_recursive($this->elements, $array)); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
4 |
|
return new static(array_merge($this->elements, $array)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Pad the current array to the specified size with a given value. |
|
183
|
|
|
* |
|
184
|
|
|
* @param int $size Size of the result array |
|
185
|
|
|
* @param mixed $value Empty value by default |
|
186
|
|
|
* |
|
187
|
|
|
* @return ArrayImitator A new array padded to $size with $value |
|
188
|
|
|
*/ |
|
189
|
4 |
|
public function pad($size, $value) |
|
190
|
|
|
{ |
|
191
|
4 |
|
return new static(array_pad($this->elements, $size, $value)); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Create a numerically re-indexed array based on the current array. |
|
196
|
|
|
* |
|
197
|
|
|
* @return ArrayImitator A new array with re-indexed elements |
|
198
|
|
|
*/ |
|
199
|
4 |
|
public function reindex() |
|
200
|
|
|
{ |
|
201
|
4 |
|
return new static(array_values($this->elements)); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Replace values in the current array with values in the given one |
|
206
|
|
|
* that have the same key. |
|
207
|
|
|
* |
|
208
|
|
|
* @param array $array Array of replacing values |
|
209
|
|
|
* @param bool $recursively Whether array will be replaced recursively or no |
|
210
|
|
|
* |
|
211
|
|
|
* @return ArrayImitator A new array with the same keys but new values |
|
212
|
|
|
*/ |
|
213
|
8 |
View Code Duplication |
public function replace(array $array, $recursively = false) |
|
214
|
|
|
{ |
|
215
|
8 |
|
if (true === $recursively) { |
|
216
|
4 |
|
return new static(array_replace_recursive($this->elements, $array)); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
4 |
|
return new static(array_replace($this->elements, $array)); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Reverse the values order of the current array. |
|
224
|
|
|
* |
|
225
|
|
|
* @param bool $preserveKeys Whether array keys are preserved or no |
|
226
|
|
|
* |
|
227
|
|
|
* @return ArrayImitator A new array with the order of the elements reversed |
|
228
|
|
|
*/ |
|
229
|
4 |
|
public function reverse($preserveKeys = false) |
|
230
|
|
|
{ |
|
231
|
4 |
|
return new static(array_reverse($this->elements, $preserveKeys)); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Randomize elements order of the current array. |
|
236
|
|
|
* |
|
237
|
|
|
* @return ArrayImitator The current array with the shuffled elements order |
|
238
|
|
|
*/ |
|
239
|
4 |
|
public function shuffle() |
|
240
|
|
|
{ |
|
241
|
4 |
|
shuffle($this->elements); |
|
242
|
|
|
|
|
243
|
4 |
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Extract a slice of the current array. |
|
248
|
|
|
* |
|
249
|
|
|
* @param int $offset Slice begin index |
|
250
|
|
|
* @param int|null $length Length of the slice |
|
251
|
|
|
* @param bool $preserveKeys Whether array keys are preserved or no |
|
252
|
|
|
* |
|
253
|
|
|
* @return ArrayImitator A new array, which is slice of the current array |
|
254
|
|
|
* with specified $length |
|
255
|
|
|
*/ |
|
256
|
4 |
|
public function slice($offset, $length = null, $preserveKeys = false) |
|
257
|
|
|
{ |
|
258
|
4 |
|
return new static(array_slice($this->elements, $offset, $length, $preserveKeys)); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Remove duplicate values from the current array. |
|
263
|
|
|
* |
|
264
|
|
|
* @param int|null $sortFlags |
|
265
|
|
|
* |
|
266
|
|
|
* @return ArrayImitator A new array with only unique elements |
|
267
|
|
|
*/ |
|
268
|
4 |
|
public function unique($sortFlags = null) |
|
269
|
|
|
{ |
|
270
|
4 |
|
return new static(array_unique($this->elements, $sortFlags)); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Apply the given function to the every element of the current array, |
|
275
|
|
|
* discarding the results. |
|
276
|
|
|
* |
|
277
|
|
|
* @param callable $func |
|
278
|
|
|
* @param bool $recursively Whether array will be walked recursively or no |
|
279
|
|
|
* |
|
280
|
|
|
* @return ArrayImitator The current array with modified elements |
|
281
|
|
|
*/ |
|
282
|
8 |
|
public function walk(callable $func, $recursively = false) |
|
283
|
|
|
{ |
|
284
|
8 |
|
if (true === $recursively) { |
|
285
|
4 |
|
array_walk_recursive($this->elements, $func); |
|
286
|
4 |
|
} else { |
|
287
|
4 |
|
array_walk($this->elements, $func); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
8 |
|
return $this; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Allow use classes described with namespace Arrayzy\Extend\<ucfirst name called function> |
|
295
|
|
|
* |
|
296
|
|
|
* @param $name |
|
297
|
|
|
* @param $arguments |
|
298
|
|
|
* @return mixed |
|
299
|
|
|
*/ |
|
300
|
4 |
|
public function __call($name, $arguments) |
|
301
|
|
|
{ |
|
302
|
4 |
|
$class = __NAMESPACE__ . '\\Extend\\' . ucfirst($name); |
|
303
|
4 |
|
if (!class_exists($class)) { |
|
304
|
|
|
throw new Exception('Class ' . $class . ' does not exist'); |
|
305
|
|
|
} |
|
306
|
4 |
|
$object = new $class($this); |
|
307
|
4 |
|
return $object; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Set default value. |
|
312
|
|
|
* |
|
313
|
|
|
* @param mixed $value |
|
314
|
|
|
*/ |
|
315
|
4 |
|
public function setDefaultValue($value) |
|
316
|
|
|
{ |
|
317
|
4 |
|
$this->defaultValue = $value; |
|
318
|
4 |
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* @return mixed |
|
322
|
|
|
*/ |
|
323
|
6 |
|
public function getDefaultValue() |
|
324
|
|
|
{ |
|
325
|
6 |
|
return $this->defaultValue; |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
|