-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathNestedArrayAccess.php
More file actions
210 lines (186 loc) · 5.62 KB
/
NestedArrayAccess.php
File metadata and controls
210 lines (186 loc) · 5.62 KB
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
<?php
namespace RocketTheme\Toolbox\ArrayTraits;
use function is_array;
use function is_object;
/**
* Implements nested ArrayAccess interface with dot notation.
*
* @package RocketTheme\Toolbox\ArrayTraits
* @author RocketTheme
* @license MIT
*/
trait NestedArrayAccess
{
/**
* @var string
* @phpstan-var non-empty-string
*/
protected $nestedSeparator = '.';
/**
* Get value by using dot notation for nested arrays/objects.
*
* @example $value = $this->get('this.is.my.nested.variable');
*
* @param string $name Dot separated path to the requested value.
* @param mixed $default Default value (or null).
* @param string|null $separator Separator, defaults to '.'
* @return mixed Value.
*/
public function get($name, $default = null, $separator = null)
{
$current = $this->items;
if ($name === '') {
return $current;
}
$path = explode($separator ?: $this->nestedSeparator, $name);
foreach ($path as $field) {
if (is_object($current) && isset($current->{$field})) {
$current = $current->{$field};
} elseif (is_array($current) && isset($current[$field])) {
$current = $current[$field];
} else {
return $default;
}
}
return $current;
}
/**
* Set value by using dot notation for nested arrays/objects.
*
* @example $data->set('this.is.my.nested.variable', $value);
*
* @param string $name Dot separated path to the requested value.
* @param mixed $value New value.
* @param string|null $separator Separator, defaults to '.'
* @return $this
*/
public function set($name, $value, $separator = null)
{
$path = $name !== '' ? explode($separator ?: $this->nestedSeparator, $name) : [];
$current = &$this->items;
foreach ($path as $field) {
if (is_object($current)) {
// Handle objects.
if (!isset($current->{$field})) {
$current->{$field} = [];
}
$current = &$current->{$field};
} else {
// Handle arrays and scalars.
if (!is_array($current)) {
$current = [$field => []];
} elseif (!isset($current[$field])) {
$current[$field] = [];
}
$current = &$current[$field];
}
}
$current = $value;
return $this;
}
/**
* Unset value by using dot notation for nested arrays/objects.
*
* @example $data->undef('this.is.my.nested.variable');
*
* @param string $name Dot separated path to the requested value.
* @param string|null $separator Separator, defaults to '.'
* @return $this
*/
public function undef($name, $separator = null)
{
// Handle empty string.
if ($name === '') {
$this->items = [];
return $this;
}
$path = explode($separator ?: $this->nestedSeparator, $name);
$var = array_pop($path);
$current = &$this->items;
foreach ($path as $field) {
if (is_object($current)) {
// Handle objects.
if (!isset($current->{$field})) {
return $this;
}
$current = &$current->{$field};
} else {
// Handle arrays and scalars.
if (!is_array($current) || !isset($current[$field])) {
return $this;
}
$current = &$current[$field];
}
}
unset($current[$var]);
return $this;
}
/**
* Set default value by using dot notation for nested arrays/objects.
*
* @example $data->def('this.is.my.nested.variable', 'default');
*
* @param string $name Dot separated path to the requested value.
* @param mixed $default Default value (or null).
* @param string|null $separator Separator, defaults to '.'
* @return $this
*/
public function def($name, $default = null, $separator = null)
{
$this->set($name, $this->get($name, $default, $separator), $separator);
return $this;
}
/**
* Whether or not an offset exists.
*
* @param string $offset An offset to check for.
* @return bool Returns TRUE on success or FALSE on failure.
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->get($offset) !== null;
}
/**
* Returns the value at specified offset.
*
* @param string $offset The offset to retrieve.
* @return mixed Can return all value types.
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Assigns a value to the specified offset.
*
* @param string|null $offset The offset to assign the value to.
* @param mixed $value The value to set.
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->items[] = $value;
} else {
$this->set($offset, $value);
}
}
/**
* Unsets variable at specified offset.
*
* @param string|null $offset
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
if (null === $offset) {
$this->items[] = [];
} else {
$this->undef($offset);
}
}
}