-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayExtra.php
220 lines (186 loc) · 5.63 KB
/
ArrayExtra.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Qubus\NoSql
*
* @link https://github.com/QubusPHP/nosql
* @copyright 2020 Joshua Parker <joshua@joshuaparker.dev>
* @copyright 2017 Muhammad Syifa
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\NoSql;
use ArrayAccess;
use InvalidArgumentException;
use function array_key_exists;
use function array_shift;
use function count;
use function explode;
use function is_array;
class ArrayExtra implements ArrayAccess
{
/** @var array $items */
protected array $items = [];
/**
* Constructor
*
* @param array $items
* @return void
*/
public function __construct(array $items)
{
$this->items = $this->getArrayValue(value: $items, message: 'Items must be array or ArrayExtra object');
}
/**
* Check if an item or items exist in an array using "dot" notation.
*
* @param array $array
* @param string|int $key
* @return bool
*/
public static function arrayHas(array $array, mixed $key): bool
{
if (array_key_exists(key: $key, array: $array)) {
return true;
}
foreach (explode(separator: '.', string: $key) as $segment) {
if (is_array(value: $array) && array_key_exists(key: $segment, array: $array)) {
$array = $array[$segment];
} else {
return false;
}
}
return true;
}
/**
* Get an item from an array using "dot" notation.
*
* @param array $array
* @param string $key
* @return mixed
*/
public static function arrayGet(array $array, mixed $key = null): mixed
{
if (null === $key) {
return $array;
}
if (array_key_exists(key: $key, array: $array)) {
return $array[$key];
}
foreach (explode(separator: '.', string: $key) as $segment) {
if (is_array(value: $array) && array_key_exists(key: $segment, array: $array)) {
$array = $array[$segment];
} else {
return null;
}
}
return $array;
}
/**
* Set an item on an array or object using dot notation.
*/
public static function arraySet(mixed &$target, array|string $key, mixed $value, bool $overwrite = true): mixed
{
$segments = is_array(value: $key) ? $key : explode(separator: '.', string: $key);
if (($segment = array_shift($segments)) === '*') {
if (! is_array(value: $target)) {
$target = [];
}
if ($segments) {
foreach ($target as &$inner) {
static::arraySet($inner, $segments, $value, $overwrite);
}
} elseif ($overwrite) {
foreach ($target as &$inner) {
$inner = $value;
}
}
} elseif (is_array(value: $target)) {
if ($segments) {
if (! array_key_exists(key: $segment, array: $target)) {
$target[$segment] = [];
}
static::arraySet($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite || ! array_key_exists(key: $segment, array: $target)) {
$target[$segment] = $value;
}
} else {
$target = [];
if ($segments) {
static::arraySet($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite) {
$target[$segment] = $value;
}
}
return $target;
}
/**
* Remove item in array.
*
* @param array $array
* @param string $key
*/
public static function arrayRemove(array &$array, string $key): void
{
$keys = explode(separator: '.', string: $key);
while (count($keys) > 1) {
$key = array_shift($keys);
if (! isset($array[$key]) || ! is_array(value: $array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
unset($array[array_shift($keys)]);
}
/**
* Merge array.
*
* @param array $value
* @return void
*/
public function merge(array $value): void
{
$array = $this->getArrayValue(value: $value, message: 'Value is not mergeable.');
foreach ($value as $key => $val) {
$this->items = static::arraySet($this->items, $key, $val, true);
}
}
/**
* Returns array value.
*
* @param array|ArrayExtra $value
* @param string $message
* @return array
*/
protected function getArrayValue(ArrayExtra|array $value, string $message): array
{
if (! is_array(value: $value) && false === $value instanceof ArrayExtra) {
throw new InvalidArgumentException(message: $message);
}
return is_array(value: $value) ? $value : $value->toArray();
}
/**
* Array items.
*
* @return array
*/
public function toArray(): array
{
return $this->items;
}
public function offsetSet(mixed $offset, mixed $value): void
{
$this->items = static::arraySet($this->items, $offset, $value, true);
}
public function offsetExists(mixed $offset): bool
{
return static::arrayHas(array: $this->items, key: $offset);
}
public function offsetUnset(mixed $offset): void
{
static::arrayRemove($this->items, $offset);
}
public function offsetGet(mixed $offset): mixed
{
return static::arrayGet(array: $this->items, key: $offset);
}
}