|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the sauls/collections package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
|
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
|
7
|
|
|
* @copyright 2018 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Sauls\Component\Collection; |
|
14
|
|
|
|
|
15
|
|
|
use function Sauls\Component\Helper\array_deep_search; |
|
16
|
|
|
use function Sauls\Component\Helper\array_get_value; |
|
17
|
|
|
use function Sauls\Component\Helper\array_remove_key; |
|
18
|
|
|
use function Sauls\Component\Helper\array_remove_value; |
|
19
|
|
|
use function Sauls\Component\Helper\array_merge; |
|
20
|
|
|
use function \Sauls\Component\Helper\array_key_exists; |
|
21
|
|
|
use function Sauls\Component\Helper\array_set_value; |
|
22
|
|
|
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException; |
|
23
|
|
|
|
|
24
|
|
|
class ArrayCollection implements ArrayCollectionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $elements; |
|
30
|
|
|
|
|
31
|
20 |
|
public function __construct(array $elements = []) |
|
32
|
|
|
{ |
|
33
|
20 |
|
$this->elements = $elements; |
|
34
|
20 |
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function create(array $elements): ArrayCollectionInterface |
|
37
|
|
|
{ |
|
38
|
1 |
|
return new static($elements); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
public function set($key, $value): void |
|
42
|
|
|
{ |
|
43
|
4 |
|
array_set_value($this->elements, $key, $value); |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function add(array $elements): void |
|
47
|
|
|
{ |
|
48
|
2 |
|
foreach($elements as $key => $element) { |
|
49
|
2 |
|
$this->set($key, $element); |
|
50
|
|
|
} |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function merge(array $elements): void |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->elements = array_merge($this->elements, $elements); |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @throws PropertyNotAccessibleException |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function get($key, $default = null) |
|
62
|
|
|
{ |
|
63
|
6 |
|
return array_get_value($this->elements, $key, $default); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function replace(array $elements): void |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->elements = $elements; |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function removeKey($key) |
|
72
|
|
|
{ |
|
73
|
2 |
|
return array_remove_key($this->elements, $key, false); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function removeElement($element) |
|
77
|
|
|
{ |
|
78
|
1 |
|
return array_remove_value($this->elements, $element); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function slice($offset, $length = null): array |
|
82
|
|
|
{ |
|
83
|
1 |
|
return \array_slice($this->elements, $offset, $length, true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function clear(): void |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->elements = []; |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
5 |
|
public function all(): array |
|
92
|
|
|
{ |
|
93
|
5 |
|
return $this->elements; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
public function filter(\Closure $function) |
|
97
|
|
|
{ |
|
98
|
1 |
|
return \array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
public function map(\Closure $function) |
|
102
|
|
|
{ |
|
103
|
1 |
|
return \array_map($function, $this->elements); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
5 |
|
public function hasKey($key): bool |
|
107
|
|
|
{ |
|
108
|
5 |
|
return array_key_exists($this->elements, $key); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
public function hasValue($value): bool |
|
112
|
|
|
{ |
|
113
|
2 |
|
return empty(array_deep_search($this->elements, $value)) ? false : true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function isEmpty(): bool |
|
117
|
|
|
{ |
|
118
|
1 |
|
return empty($this->elements); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
public function __toString(): string |
|
122
|
|
|
{ |
|
123
|
1 |
|
return __CLASS__ . '@' . spl_object_hash($this); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
public function getIterator() |
|
127
|
|
|
{ |
|
128
|
1 |
|
return new \ArrayIterator($this->elements); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
public function offsetExists($offset) |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->hasKey($offset); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @throws PropertyNotAccessibleException |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function offsetGet($offset) |
|
140
|
|
|
{ |
|
141
|
2 |
|
return $this->get($offset); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
public function offsetSet($offset, $value) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$this->set($offset, $value); |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
public function offsetUnset($offset) |
|
150
|
|
|
{ |
|
151
|
1 |
|
$this->removeKey($offset); |
|
152
|
1 |
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
public function count(): int |
|
155
|
|
|
{ |
|
156
|
1 |
|
return \count($this->elements); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
1 |
|
public function has($keyOrValue): bool |
|
160
|
|
|
{ |
|
161
|
1 |
|
if ($this->hasKey($keyOrValue)) { |
|
162
|
1 |
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
1 |
|
return $this->hasValue($keyOrValue); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|