1
|
|
|
|
2
|
|
|
<?php |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the overtrue/http. |
6
|
|
|
* |
7
|
|
|
* (c) overtrue <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This source file is subject to the MIT license that is bundled |
10
|
|
|
* with this source code in the file LICENSE. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Overtrue\Http\Support; |
14
|
|
|
|
15
|
|
|
use ArrayAccess; |
16
|
|
|
use ArrayIterator; |
17
|
|
|
use Countable; |
18
|
|
|
use IteratorAggregate; |
19
|
|
|
use JsonSerializable; |
20
|
|
|
use Serializable; |
21
|
|
|
use Traversable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Collection. |
25
|
|
|
*/ |
26
|
|
|
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The collection data. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $items = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* set data. |
37
|
|
|
* |
38
|
|
|
* @param mixed $items |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $items = []) |
41
|
|
|
{ |
42
|
|
|
foreach ($items as $key => $value) { |
43
|
|
|
$this->set($key, $value); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return all items. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function all(): array |
53
|
|
|
{ |
54
|
|
|
return $this->items; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return specific items. |
59
|
|
|
* |
60
|
|
|
* @param array $keys |
61
|
|
|
* |
62
|
|
|
* @return \Overtrue\Http\Support\Collection |
63
|
|
|
*/ |
64
|
|
|
public function only(array $keys): self |
65
|
|
|
{ |
66
|
|
|
$return = []; |
67
|
|
|
|
68
|
|
|
foreach ($keys as $key) { |
69
|
|
|
$value = $this->get($key); |
70
|
|
|
|
71
|
|
|
if (!is_null($value)) { |
72
|
|
|
$return[$key] = $value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new static($return); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get all items except for those with the specified keys. |
81
|
|
|
* |
82
|
|
|
* @param mixed $keys |
83
|
|
|
* |
84
|
|
|
* @return \Overtrue\Http\Support\Collection |
85
|
|
|
*/ |
86
|
|
|
public function except($keys): self |
87
|
|
|
{ |
88
|
|
|
$keys = is_array($keys) ? $keys : func_get_args(); |
89
|
|
|
|
90
|
|
|
return new static(array_diff($this->items, array_combine($keys, array_pad([], count($keys), null)))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Merge data. |
95
|
|
|
* |
96
|
|
|
* @param \Overtrue\Http\Support\Collection|array $items |
97
|
|
|
* |
98
|
|
|
* @return \Overtrue\Http\Support\Collection |
99
|
|
|
*/ |
100
|
|
|
public function merge($items): self |
101
|
|
|
{ |
102
|
|
|
foreach ($items as $key => $value) { |
103
|
|
|
$this->set($key, $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return new static($this->all()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* To determine Whether the specified element exists. |
111
|
|
|
* |
112
|
|
|
* @param string $key |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function has($key): bool |
117
|
|
|
{ |
118
|
|
|
return !is_null($this->dotGet($this->items, $key)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieve the first item. |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function first() |
127
|
|
|
{ |
128
|
|
|
return reset($this->items); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieve the last item. |
133
|
|
|
* |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
public function last() |
137
|
|
|
{ |
138
|
|
|
$end = end($this->items); |
139
|
|
|
|
140
|
|
|
reset($this->items); |
141
|
|
|
|
142
|
|
|
return $end; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* add the item value. |
147
|
|
|
* |
148
|
|
|
* @param string $key |
149
|
|
|
* @param mixed $value |
150
|
|
|
*/ |
151
|
|
|
public function add($key, $value) |
152
|
|
|
{ |
153
|
|
|
$this->dotSet($this->items, $key, $value); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set the item value. |
158
|
|
|
* |
159
|
|
|
* @param string $key |
160
|
|
|
* @param mixed $value |
161
|
|
|
*/ |
162
|
|
|
public function set($key, $value) |
163
|
|
|
{ |
164
|
|
|
$this->dotSet($this->items, $key, $value); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Remove item form Collection. |
169
|
|
|
* |
170
|
|
|
* @param string $key |
171
|
|
|
*/ |
172
|
|
|
public function forget($key) |
173
|
|
|
{ |
174
|
|
|
$this->dotRemove($this->items, $key); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Retrieve item from Collection. |
179
|
|
|
* |
180
|
|
|
* @param string $key |
181
|
|
|
* @param mixed $default |
182
|
|
|
* |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
public function get($key, $default = null) |
186
|
|
|
{ |
187
|
|
|
return $this->dotGet($this->items, $key, $default); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param array $array |
192
|
|
|
* @param string $key |
193
|
|
|
* @param mixed|null $default |
194
|
|
|
* |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function dotGet($array, $key, $default = null) |
198
|
|
|
{ |
199
|
|
|
if (is_null($key)) { |
200
|
|
|
return $array; |
201
|
|
|
} |
202
|
|
|
if (array_key_exists($key, $array)) { |
203
|
|
|
return $array[$key]; |
204
|
|
|
} |
205
|
|
|
foreach (explode('.', $key) as $segment) { |
206
|
|
|
if (array_key_exists($segment, $array)) { |
207
|
|
|
$array = $array[$segment]; |
208
|
|
|
} else { |
209
|
|
|
return $default; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param array $array |
216
|
|
|
* @param string $key |
217
|
|
|
* @param mixed $value |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function dotSet(array &$array, string $key, $value): array |
222
|
|
|
{ |
223
|
|
|
$keys = explode('.', $key); |
224
|
|
|
while (count($keys) > 1) { |
225
|
|
|
$key = array_shift($keys); |
226
|
|
|
if (!isset($array[$key]) || !is_array($array[$key])) { |
227
|
|
|
$array[$key] = []; |
228
|
|
|
} |
229
|
|
|
$array = &$array[$key]; |
230
|
|
|
} |
231
|
|
|
$array[array_shift($keys)] = $value; |
232
|
|
|
|
233
|
|
|
return $array; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param array $array |
238
|
|
|
* @param array|string $keys |
239
|
|
|
*/ |
240
|
|
|
public function dotRemove(array &$array, $keys) |
241
|
|
|
{ |
242
|
|
|
$original = &$array; |
243
|
|
|
$keys = (array) $keys; |
244
|
|
|
if (0 === count($keys)) { |
245
|
|
|
return; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
foreach ($keys as $key) { |
249
|
|
|
if (array_key_exists($key, $array)) { |
250
|
|
|
unset($array[$key]); |
251
|
|
|
continue; |
252
|
|
|
} |
253
|
|
|
$parts = explode('.', $key); |
254
|
|
|
|
255
|
|
|
$array = &$original; |
256
|
|
|
|
257
|
|
|
while (count($parts) > 1) { |
258
|
|
|
$part = array_shift($parts); |
259
|
|
|
if (isset($array[$part]) && is_array($array[$part])) { |
260
|
|
|
$array = &$array[$part]; |
261
|
|
|
} else { |
262
|
|
|
continue 2; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
unset($array[array_shift($parts)]); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Build to array. |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
|
|
public function toArray(): array |
275
|
|
|
{ |
276
|
|
|
return $this->all(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Build to json. |
281
|
|
|
* |
282
|
|
|
* @param int $option |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public function toJson($option = JSON_UNESCAPED_UNICODE): string |
287
|
|
|
{ |
288
|
|
|
return json_encode($this->all(), $option); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* To string. |
293
|
|
|
* |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
|
|
public function __toString(): string |
297
|
|
|
{ |
298
|
|
|
return $this->toJson(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
303
|
|
|
* Specify data which should be serialized to JSON. |
304
|
|
|
* |
305
|
|
|
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php |
306
|
|
|
* |
307
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
308
|
|
|
* which is a value of any type other than a resource |
309
|
|
|
*/ |
310
|
|
|
public function jsonSerialize(): array |
311
|
|
|
{ |
312
|
|
|
return $this->items; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
317
|
|
|
* String representation of object. |
318
|
|
|
* |
319
|
|
|
* @see http://php.net/manual/en/serializable.serialize.php |
320
|
|
|
* |
321
|
|
|
* @return string the string representation of the object or null |
322
|
|
|
*/ |
323
|
|
|
public function serialize(): string |
324
|
|
|
{ |
325
|
|
|
return serialize($this->items); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
330
|
|
|
* Retrieve an external iterator. |
331
|
|
|
* |
332
|
|
|
* @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
333
|
|
|
* |
334
|
|
|
* @return \ArrayIterator An instance of an object implementing <b>Iterator</b> or |
335
|
|
|
* <b>Traversable</b> |
336
|
|
|
*/ |
337
|
|
|
public function getIterator(): ArrayIterator |
338
|
|
|
{ |
339
|
|
|
return new ArrayIterator($this->items); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
344
|
|
|
* Count elements of an object. |
345
|
|
|
* |
346
|
|
|
* @see http://php.net/manual/en/countable.count.php |
347
|
|
|
* |
348
|
|
|
* @return int the custom count as an integer. |
349
|
|
|
* </p> |
350
|
|
|
* <p> |
351
|
|
|
* The return value is cast to an integer |
352
|
|
|
*/ |
353
|
|
|
public function count(): int |
354
|
|
|
{ |
355
|
|
|
return count($this->items); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
360
|
|
|
* Constructs the object. |
361
|
|
|
* |
362
|
|
|
* @see http://php.net/manual/en/serializable.unserialize.php |
363
|
|
|
* |
364
|
|
|
* @param string $serialized <p> |
365
|
|
|
* The string representation of the object. |
366
|
|
|
* </p> |
367
|
|
|
* |
368
|
|
|
* @return mixed|void |
369
|
|
|
*/ |
370
|
|
|
public function unserialize($serialized) |
371
|
|
|
{ |
372
|
|
|
return $this->items = unserialize($serialized); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Get a data by key. |
377
|
|
|
* |
378
|
|
|
* @param string $key |
379
|
|
|
* |
380
|
|
|
* @return mixed |
381
|
|
|
*/ |
382
|
|
|
public function __get($key) |
383
|
|
|
{ |
384
|
|
|
return $this->get($key); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Assigns a value to the specified data. |
389
|
|
|
* |
390
|
|
|
* @param string $key |
391
|
|
|
* @param mixed $value |
392
|
|
|
*/ |
393
|
|
|
public function __set($key, $value) |
394
|
|
|
{ |
395
|
|
|
$this->set($key, $value); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Whether or not an data exists by key. |
400
|
|
|
* |
401
|
|
|
* @param string $key |
402
|
|
|
* |
403
|
|
|
* @return bool |
404
|
|
|
*/ |
405
|
|
|
public function __isset($key): bool |
406
|
|
|
{ |
407
|
|
|
return $this->has($key); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Unset an data by key. |
412
|
|
|
* |
413
|
|
|
* @param string $key |
414
|
|
|
*/ |
415
|
|
|
public function __unset($key) |
416
|
|
|
{ |
417
|
|
|
$this->forget($key); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* var_export. |
422
|
|
|
* |
423
|
|
|
* @param array properties |
424
|
|
|
* |
425
|
|
|
* @return object |
426
|
|
|
*/ |
427
|
|
|
public static function __set_state($array): object |
428
|
|
|
{ |
429
|
|
|
return new self($array); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
434
|
|
|
* Whether a offset exists. |
435
|
|
|
* |
436
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetexists.php |
437
|
|
|
* |
438
|
|
|
* @param mixed $offset <p> |
439
|
|
|
* An offset to check for. |
440
|
|
|
* </p> |
441
|
|
|
* |
442
|
|
|
* @return bool true on success or false on failure. |
443
|
|
|
* The return value will be casted to boolean if non-boolean was returned |
444
|
|
|
*/ |
445
|
|
|
public function offsetExists($offset): bool |
446
|
|
|
{ |
447
|
|
|
return $this->has($offset); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
452
|
|
|
* Offset to unset. |
453
|
|
|
* |
454
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetunset.php |
455
|
|
|
* |
456
|
|
|
* @param mixed $offset <p> |
457
|
|
|
* The offset to unset. |
458
|
|
|
* </p> |
459
|
|
|
*/ |
460
|
|
|
public function offsetUnset($offset) |
461
|
|
|
{ |
462
|
|
|
if ($this->offsetExists($offset)) { |
463
|
|
|
$this->forget($offset); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
469
|
|
|
* Offset to retrieve. |
470
|
|
|
* |
471
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetget.php |
472
|
|
|
* |
473
|
|
|
* @param mixed $offset <p> |
474
|
|
|
* The offset to retrieve. |
475
|
|
|
* </p> |
476
|
|
|
* |
477
|
|
|
* @return mixed Can return all value types |
478
|
|
|
*/ |
479
|
|
|
public function offsetGet($offset) |
480
|
|
|
{ |
481
|
|
|
return $this->offsetExists($offset) ? $this->get($offset) : null; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
486
|
|
|
* Offset to set. |
487
|
|
|
* |
488
|
|
|
* @see http://php.net/manual/en/arrayaccess.offsetset.php |
489
|
|
|
* |
490
|
|
|
* @param mixed $offset <p> |
491
|
|
|
* The offset to assign the value to. |
492
|
|
|
* </p> |
493
|
|
|
* @param mixed $value <p> |
494
|
|
|
* The value to set. |
495
|
|
|
* </p> |
496
|
|
|
*/ |
497
|
|
|
public function offsetSet($offset, $value) |
498
|
|
|
{ |
499
|
|
|
$this->set($offset, $value); |
500
|
|
|
} |
501
|
|
|
} |
|
|
|
|
502
|
|
|
|