18
18
19
19
use Adbar\Dot;
20
20
21
+
use function PHPUnit\Framework\isNull;
22
+
23
+
/**
24
+
* Asterisk is provide asterisk notation access to arrays
25
+
*
26
+
* @package Setnemo
27
+
*/
21
28
class Asterisk extends Dot
22
29
{
23
30
/**
@@ -27,7 +34,7 @@ class Asterisk extends Dot
27
34
* @param array|int|string $keys
28
35
* @param mixed $value
29
36
*/
30
-
public function add($keys, $value = null)
37
+
public function add($keys, $value = null): void
31
38
{
32
39
if (is_array($keys)) {
33
40
foreach ($keys as $key => $value) {
@@ -42,7 +49,7 @@ public function add($keys, $value = null)
42
49
* Delete the contents of a given key or keys
43
50
*
44
51
* @param array|int|string|null $keys
45
-
* @param bool $asterisk
52
+
* @param bool $asterisk
46
53
*/
47
54
public function clear($keys = null, bool $asterisk = true): void
48
55
{
@@ -62,7 +69,7 @@ public function clear($keys = null, bool $asterisk = true): void
62
69
*
63
70
* @param array|int|string $keys
64
71
*/
65
-
public function delete($keys)
72
+
public function delete($keys): void
66
73
{
67
74
$keys = (array) $keys;
68
75
$affectedKeys = $this->getAffectedAndFilterKeys($keys);
@@ -72,29 +79,29 @@ public function delete($keys)
72
79
73
80
/**
74
81
* Return the value of a given key
75
-
* with star - all keys as array ['key.with.dot' => 'value']
82
+
* with asterisk - all keys as array ['key.with.dot' => 'value']
76
83
*
77
84
* @param int|string|null $key
78
-
* @param mixed $default
79
-
* @param bool $asterisk
85
+
* @param mixed $default
86
+
* @param bool $asterisk
80
87
* @return mixed
81
88
*/
82
89
public function get($key = null, $default = null, bool $asterisk = true)
83
90
{
84
-
if (false === $asterisk || is_null($key) || !$this->keyHasStars($key)) {
91
+
if (false === $asterisk || is_null($key) || !$this->keyHasAsterisk($key)) {
85
92
return parent::get($key, $default);
86
93
}
87
94
88
-
return $this->getStarsAffectedKeys($key);
95
+
return $this->getAffectedKeys((string) $key);
89
96
}
90
97
91
98
/**
92
99
* Check if a given key or keys exists
93
100
* Also compare values with strict mode (default)
94
101
*
95
102
* @param array|int|string $keys
96
-
* @param null $value
97
-
* @param bool $strict
103
+
* @param null $value
104
+
* @param bool $strict
98
105
* @return bool
99
106
*/
100
107
public function has($keys, $value = null, bool $strict = true): bool
@@ -106,29 +113,29 @@ public function has($keys, $value = null, bool $strict = true): bool
106
113
107
114
$affectedKeys = $this->getAffectedAndFilterKeys($keys);
108
115
109
-
if (null !== $value) {
116
+
if (!is_null($value)) {
110
117
return $this->hasValueAndStrict(array_merge($affectedKeys, $keys), $value, $strict);
111
118
}
112
119
113
-
return (bool) (!empty($affectedKeys) | parent::has($keys));
120
+
return (bool) ((int) !empty($affectedKeys) | (int) parent::has($keys));
114
121
}
115
122
116
123
/**
117
124
* Set a given key / value pair or pairs
118
-
*
119
125
* @param array|int|string $keys
120
126
* @param mixed $value
127
+
* @param bool $asterisk
121
128
*/
122
-
public function set($keys, $value = null, bool $asterisk = true)
129
+
public function set($keys, $value = null, bool $asterisk = true): void
123
130
{
124
131
if (is_array($keys)) {
125
132
foreach ($keys as $key => $value) {
126
133
$this->set($key, $value, $asterisk);
127
134
}
128
135
return;
129
136
}
130
-
if ($asterisk && $this->keyHasStars($keys)) {
131
-
$affectedKeys = $this->prepareStarsAffectedKeys($keys, $value);
137
+
if ($asterisk && $this->keyHasAsterisk($keys)) {
138
+
$affectedKeys = $this->prepareAffectedKeys((string) $keys, $value);
132
139
$this->set($affectedKeys, [], false);
133
140
return;
134
141
}
@@ -143,6 +150,35 @@ public function set($keys, $value = null, bool $asterisk = true)
143
150
$items = $value;
144
151
}
145
152
153
+
/**
154
+
* @param array $keys
155
+
* @param $value
156
+
* @param bool $strict
157
+
* @return bool
158
+
*/
159
+
protected function hasValueAndStrict(
160
+
array $keys,
161
+
$value,
162
+
bool $strict
163
+
): bool {
164
+
$nonStrictResult = 0;
165
+
166
+
foreach ($keys as $key) {
167
+
if ($value !== $this->get($key, null, false)) {
168
+
if (false === $strict) {
169
+
$nonStrictResult++;
170
+
continue;
171
+
}
172
+
return false;
173
+
}
174
+
}
175
+
176
+
if (!$strict) {
177
+
return $nonStrictResult !== count($keys);
178
+
}
179
+
180
+
return true;
181
+
}
146
182
147
183
/**
148
184
* Push a given value to the end of the array
@@ -151,56 +187,31 @@ public function set($keys, $value = null, bool $asterisk = true)
151
187
* @param mixed $key
152
188
* @param mixed $value
153
189
*/
154
-
public function push($key, $value = null, bool $asterisk = true)
190
+
public function push($key, $value = null, bool $asterisk = true): void
155
191
{
156
-
if (is_null($value)) {
157
-
$this->items[] = $key;
158
-
159
-
return;
160
-
}
161
-
162
192
$items = $this->get($key);
163
-
if ($asterisk && $this->keyHasStars($key)) {
164
-
$items = $this->prepareStarsAffectedKeys($key, $value);
193
+
if ($asterisk && $this->keyHasAsterisk($key)) {
194
+
$items = $this->prepareAffectedKeys($key, $value);
165
195
foreach ($items as $key => $item) {
166
196
$changedItems = $this->get($key);
167
197
$this->set($key, array_merge((array)$changedItems, (array)$value), false);
168
198
}
169
199
} elseif (is_array($items) || is_null($items)) {
170
-
$items[] = $value;
200
+
$items[] = $value ?? [];
171
201
$this->set($key, $items, $asterisk);
172
202
}
173
203
}
174
204
175
-
/**
176
-
* @param string $asteriskKey
177
-
* @param $value
178
-
* @return array
179
-
*/
180
-
protected function prepareStarsAffectedKeys(string $asteriskKey, $value): array
181
-
{
182
-
$keys = $this->getStarsAffectedKeys($asteriskKey);
183
-
if ($this->isEmpty() && empty($keys)) {
184
-
return [$asteriskKey => $value];
185
-
}
186
-
187
-
foreach ($keys as $key => $v) {
188
-
$keys[$key] = $value;
189
-
}
190
-
191
-
return $keys;
192
-
}
193
-
194
205
/**
195
206
* @param array $keys
196
-
* @return array
207
+
* @return array
197
208
*/
198
209
protected function getAffectedAndFilterKeys(array &$keys): array
199
210
{
200
211
$affectedKeys = [];
201
212
foreach ($keys as $it => $key) {
202
-
if ($this->keyHasStars($key)) {
203
-
$affectedKeys = array_merge($affectedKeys, $this->getStarsAffectedKeys($key, false) ?? []);
213
+
if ($this->keyHasAsterisk($key)) {
214
+
$affectedKeys = array_merge($affectedKeys, $this->getAffectedKeys($key, false) ?? []);
204
215
205
216
unset($keys[$it]);
206
217
}
@@ -211,10 +222,10 @@ protected function getAffectedAndFilterKeys(array &$keys): array
211
222
212
223
/**
213
224
* @param string $asteriskKey
214
-
* @param bool $withValues
225
+
* @param bool $withValues
215
226
* @return array|null
216
227
*/
217
-
protected function getStarsAffectedKeys(string $asteriskKey, bool $withValues = true): ?array
228
+
protected function getAffectedKeys(string $asteriskKey, bool $withValues = true): ?array
218
229
{
219
230
$keys = $this->flatten();
220
231
@@ -227,8 +238,8 @@ protected function getStarsAffectedKeys(string $asteriskKey, bool $withValues =
227
238
foreach ($keys as $key => $v) {
228
239
$matches = [];
229
240
preg_match_all($pattern, "$key", $matches);
230
-
$matchResult = $matches[0][0] ?? [];
231
-
if (!empty($matchResult) || $matchResult === "0") {
241
+
$matchResult = $matches[0][0] ?? '';
242
+
if (!empty($matchResult) || $matchResult === '0') {
232
243
$result = array_merge($result, $withValues ? [$matchResult => $v] : [$matchResult]);
233
244
}
234
245
}
@@ -237,43 +248,34 @@ protected function getStarsAffectedKeys(string $asteriskKey, bool $withValues =
237
248
}
238
249
239
250
/**
240
-
* @param array $keys
241
-
* @param $value
242
-
* @param bool $strict
251
+
* @param $key
243
252
* @return bool
244
253
*/
245
-
protected function hasValueAndStrict(
246
-
array $keys,
247
-
$value,
248
-
bool $strict
249
-
): bool {
250
-
$nonStrictResult = 0;
251
-
252
-
foreach ($keys as $key) {
253
-
if ($value !== $this->get($key, null, false)) {
254
-
if (false === $strict) {
255
-
$nonStrictResult++;
256
-
continue;
257
-
}
258
-
return false;
259
-
}
260
-
}
261
-
262
-
if (!$strict) {
263
-
return $nonStrictResult !== count($keys);
264
-
}
254
+
protected function keyHasAsterisk($key): bool
255
+
{
256
+
$counter = array_count_values(explode('.', (string) $key) ?? []);
265
257
266
-
return true;
258
+
return (bool) ($counter['*'] ?? 0);
267
259
}
268
260
269
261
/**
270
-
* @param $key
271
-
* @return bool
262
+
* @param string $asteriskKey
263
+
* @param $value
264
+
* @return array
272
265
*/
273
-
protected function keyHasStars($key): bool
266
+
protected function prepareAffectedKeys(string $asteriskKey, $value): array
274
267
{
275
-
$counter = array_count_values(explode('.', (string) $key) ?? []);
268
+
$keys = $this->getAffectedKeys($asteriskKey);
269
+
if ($this->isEmpty() && empty($keys)) {
270
+
return [$asteriskKey => $value ?? []];
271
+
}
276
272
277
-
return (bool) ($counter['*'] ?? 0);
273
+
if (is_array($keys)) {
274
+
foreach ($keys as $key => $v) {
275
+
$keys[$key] = $value;
276
+
}
277
+
}
278
+
279
+
return $keys;
278
280
}
279
281
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4