|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Dot - PHP dot notation access to arrays |
|
5
|
|
|
* |
|
6
|
|
|
* @author Riku Särkinen <[email protected]> |
|
7
|
|
|
* @link https://github.com/adbario/php-dot-notation |
|
8
|
|
|
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ballybran\Core\Collections\Collection; |
|
12
|
|
|
|
|
13
|
|
|
use Countable; |
|
14
|
|
|
use ArrayIterator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Dot |
|
18
|
|
|
* |
|
19
|
|
|
* This class provides a dot notation access and helper functions for |
|
20
|
|
|
* working with arrays of data. Inspired by Laravel Collection. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ValidateDot implements Countable |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Replace all items with a given array as a reference |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $items |
|
29
|
|
|
*/ |
|
30
|
|
|
public function setReference(array &$items) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->elements = &$items; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return all the stored items |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function all() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->elements; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Checks if the given key exists in the provided array. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $array Array to validate |
|
50
|
|
|
* @param int|string $key The key to look for |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
private function exists($array, $key) |
|
55
|
|
|
{ |
|
56
|
|
|
return array_key_exists($key, $array); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/* |
|
61
|
|
|
* -------------------------------------------------------------- |
|
62
|
|
|
* ArrayAccess interface |
|
63
|
|
|
* -------------------------------------------------------------- |
|
64
|
|
|
*/ |
|
65
|
|
|
/** |
|
66
|
|
|
* Check if a given key exists |
|
67
|
|
|
* |
|
68
|
|
|
* @param int|string $key |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function offsetExists($key) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->has($key); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Replace all items with a given array |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $items |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setArray($items) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->elements = $this->getArrayItems($items); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/* |
|
87
|
|
|
* -------------------------------------------------------------- |
|
88
|
|
|
* Countable interface |
|
89
|
|
|
* -------------------------------------------------------------- |
|
90
|
|
|
*/ |
|
91
|
|
|
/** |
|
92
|
|
|
* Return the number of items in a given key |
|
93
|
|
|
* |
|
94
|
|
|
* @param int|string|null $key |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
|
|
public function count($key = null) |
|
98
|
|
|
{ |
|
99
|
|
|
return count($this->get($key)); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check if a given key or keys exists |
|
105
|
|
|
* |
|
106
|
|
|
* @param array|int|string $keys |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function has($keys) |
|
110
|
|
|
{ |
|
111
|
|
|
$keys = (array)$keys; |
|
112
|
|
|
if ( empty($this->elements) || $keys === []) { |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
foreach ($keys as $key) { |
|
116
|
|
|
$items = $this->elements; |
|
117
|
|
|
if ($this->exists($items, $key)) { |
|
118
|
|
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
foreach (explode('.', $key) as $segment) { |
|
121
|
|
|
if (!is_array($items) || !$this->exists($items, $segment)) { |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
$items = $items[$segment]; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Set a given key / value pair or pairs |
|
132
|
|
|
* |
|
133
|
|
|
* @param array|int|string $keys |
|
134
|
|
|
* @param mixed $value |
|
135
|
|
|
*/ |
|
136
|
|
|
public function set($keys, $value = null) |
|
137
|
|
|
{ |
|
138
|
|
|
if (is_array($keys)) { |
|
139
|
|
|
foreach ($keys as $key => $value) { |
|
140
|
|
|
$this->set($key, $value); |
|
141
|
|
|
} |
|
142
|
|
|
return; |
|
143
|
|
|
} |
|
144
|
|
|
$items = &$this->elements; |
|
145
|
|
|
foreach (explode('.', $keys) as $key) { |
|
146
|
|
|
if (!isset($items[$key]) || !is_array($items[$key])) { |
|
147
|
|
|
$items[$key] = []; |
|
148
|
|
|
} |
|
149
|
|
|
$items = &$items[$key]; |
|
150
|
|
|
} |
|
151
|
|
|
$items = $value; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Check if a given key or keys are empty |
|
156
|
|
|
* |
|
157
|
|
|
* @param array|int|string|null $keys |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
|
|
public function isEmpty($keys = null) |
|
161
|
|
|
{ |
|
162
|
|
|
if (is_null($keys)) { |
|
163
|
|
|
return empty($this->elements); |
|
164
|
|
|
} |
|
165
|
|
|
$keys = (array)$keys; |
|
166
|
|
|
foreach ($keys as $key) { |
|
167
|
|
|
if (!empty($this->get($key))) { |
|
168
|
|
|
return false; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
return true; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Merge a given array or a Dot object with the given key |
|
176
|
|
|
* or with the whole Dot object |
|
177
|
|
|
* |
|
178
|
|
|
* @param array|string|self $key |
|
179
|
|
|
* @param array $value |
|
180
|
|
|
*/ |
|
181
|
|
|
public function merge($key, $value = null) |
|
182
|
|
|
{ |
|
183
|
|
|
if (is_array($key)) { |
|
184
|
|
|
$this->elements = array_merge($this->elements, $key); |
|
|
|
|
|
|
185
|
|
|
} elseif (is_string($key)) { |
|
186
|
|
|
$items = (array)$this->get($key); |
|
187
|
|
|
$value = array_merge($items, $this->getArrayItems($value)); |
|
188
|
|
|
$this->set($key, $value); |
|
189
|
|
|
} |
|
190
|
|
|
$this->elements = array_merge($this->elements, $key->all()); |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
} |