1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Collection; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use ArrayIterator; |
7
|
|
|
use Countable; |
8
|
|
|
use Isswp101\Persimmon\Contracts\Jsonable; |
9
|
|
|
use Isswp101\Persimmon\Contracts\Stringable; |
10
|
|
|
use IteratorAggregate; |
11
|
|
|
use JsonSerializable; |
12
|
|
|
|
13
|
|
|
class Collection implements ArrayAccess, Countable, IteratorAggregate, ICollection, Jsonable, Stringable, JsonSerializable |
14
|
|
|
{ |
15
|
|
|
protected $items = []; |
16
|
|
|
|
17
|
|
|
public function __construct(array $items = []) |
18
|
|
|
{ |
19
|
|
|
$this->items = $items; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function put($key, $value) |
23
|
|
|
{ |
24
|
|
|
$this->offsetSet($key, $value); |
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function get($key, $default = null) |
29
|
|
|
{ |
30
|
|
|
return $this->offsetExists($key) ? $this->items[$key] : $default; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function has($key) |
34
|
|
|
{ |
35
|
|
|
return $this->offsetExists($key); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function forget($key) |
39
|
|
|
{ |
40
|
|
|
$this->offsetUnset($key); |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function all(): array |
45
|
|
|
{ |
46
|
|
|
return $this->items; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isEmpty(): bool |
50
|
|
|
{ |
51
|
|
|
return empty($this->items); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function isNotEmpty(): bool |
55
|
|
|
{ |
56
|
|
|
return !$this->isEmpty(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function first() |
60
|
|
|
{ |
61
|
|
|
return array_values($this->items)[0] ?? null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function last() |
65
|
|
|
{ |
66
|
|
|
return end($this->items); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function jsonSerialize() |
70
|
|
|
{ |
71
|
|
|
return $this->all(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function toJson(int $options = 0): string |
75
|
|
|
{ |
76
|
|
|
return json_encode($this->jsonSerialize(), $options); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getIterator(): ArrayIterator |
80
|
|
|
{ |
81
|
|
|
return new ArrayIterator($this->items); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function count(): int |
85
|
|
|
{ |
86
|
|
|
return count($this->items); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function offsetExists($key) |
90
|
|
|
{ |
91
|
|
|
return array_key_exists($key, $this->items); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function offsetGet($key) |
95
|
|
|
{ |
96
|
|
|
return $this->items[$key]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function offsetSet($key, $value) |
100
|
|
|
{ |
101
|
|
|
if (is_null($key)) { |
102
|
|
|
$this->items[] = $value; |
103
|
|
|
} else { |
104
|
|
|
$this->items[$key] = $value; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function offsetUnset($key) |
109
|
|
|
{ |
110
|
|
|
unset($this->items[$key]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function __toString(): string |
114
|
|
|
{ |
115
|
|
|
return $this->toJson(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function each(callable $callback) |
119
|
|
|
{ |
120
|
|
|
foreach ($this->items as $key => $item) { |
121
|
|
|
if ($callback($item, $key) === false) { |
122
|
|
|
break; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |