|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Support; |
|
3
|
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
|
|
7
|
|
|
class DotArray implements ArrayAccess |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var array */ |
|
10
|
|
|
protected $items; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param array $items |
|
14
|
|
|
*/ |
|
15
|
24 |
|
public function __construct(array $items = []) |
|
16
|
|
|
{ |
|
17
|
24 |
|
$this->items = $items; |
|
18
|
24 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
4 |
|
public function getRawData() |
|
24
|
|
|
{ |
|
25
|
4 |
|
return $this->items; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
16 |
|
public function get($name, $default = null) |
|
32
|
|
|
{ |
|
33
|
16 |
|
if ($name === '') { |
|
34
|
2 |
|
return $this->items; |
|
35
|
|
|
} |
|
36
|
16 |
|
$names = explode('.', $name); |
|
37
|
16 |
|
$dataToReturn = $this->items; |
|
38
|
16 |
|
while (count($names)) { |
|
39
|
16 |
|
$name = array_shift($names); |
|
40
|
16 |
|
if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) { |
|
41
|
9 |
|
return $default; |
|
42
|
|
|
} |
|
43
|
15 |
|
$dataToReturn = $dataToReturn[$name]; |
|
44
|
15 |
|
} |
|
45
|
15 |
|
return $dataToReturn; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function set($name, $value) |
|
52
|
|
|
{ |
|
53
|
2 |
|
if ($name === '') { |
|
54
|
|
|
$this->items = $value; |
|
55
|
|
|
} |
|
56
|
2 |
|
$names = explode('.', $name); |
|
57
|
2 |
|
$dataToSet = &$this->items; |
|
58
|
2 |
|
while (count($names)) { |
|
59
|
2 |
|
$name = array_shift($names); |
|
60
|
2 |
|
if (!is_array($dataToSet)) { |
|
61
|
2 |
|
$dataToSet = []; |
|
62
|
2 |
|
} |
|
63
|
2 |
|
if (!array_key_exists($name, $dataToSet)) { |
|
64
|
2 |
|
$dataToSet[$name] = null; |
|
65
|
2 |
|
} |
|
66
|
2 |
|
$dataToSet = &$dataToSet[$name]; |
|
67
|
2 |
|
} |
|
68
|
2 |
|
$dataToSet = $value; |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function has($name) |
|
75
|
|
|
{ |
|
76
|
4 |
|
if ($name === '') { |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
4 |
|
$names = explode('.', $name); |
|
80
|
4 |
|
$dataToReturn = $this->items; |
|
81
|
4 |
|
while (count($names)) { |
|
82
|
4 |
|
$name = array_shift($names); |
|
83
|
4 |
|
if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) { |
|
84
|
4 |
|
return false; |
|
85
|
|
|
} |
|
86
|
4 |
|
$dataToReturn = $dataToReturn[$name]; |
|
87
|
4 |
|
} |
|
88
|
4 |
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function remove($name) |
|
95
|
|
|
{ |
|
96
|
2 |
|
if ($name === '') { |
|
97
|
|
|
$this->items = []; |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
2 |
|
$names = explode('.', $name); |
|
101
|
2 |
|
$dataToReturn = &$this->items; |
|
102
|
2 |
|
while (count($names)) { |
|
103
|
2 |
|
$name = array_shift($names); |
|
104
|
2 |
|
if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) { |
|
105
|
2 |
|
return false; |
|
106
|
|
|
} |
|
107
|
2 |
|
if (count($names) === 0) { |
|
108
|
2 |
|
unset($dataToReturn[$name]); |
|
109
|
2 |
|
} else { |
|
110
|
2 |
|
$dataToReturn = &$dataToReturn[$name]; |
|
111
|
|
|
} |
|
112
|
2 |
|
} |
|
113
|
2 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function subset($name) |
|
120
|
|
|
{ |
|
121
|
2 |
|
$subset = $this->get($name); |
|
122
|
2 |
|
if (!is_array($subset)) { |
|
123
|
2 |
|
throw new InvalidArgumentException('subset must be an array.'); |
|
124
|
|
|
} |
|
125
|
2 |
|
return new static($subset); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
|
|
public function offsetExists($offset) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->has($offset); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritdoc} |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function offsetGet($offset) |
|
140
|
|
|
{ |
|
141
|
2 |
|
if (strpos($offset, '||') !== false) { |
|
142
|
2 |
|
list($offset, $default) = explode('||', $offset); |
|
143
|
2 |
|
return $this->get($offset, $default); |
|
144
|
|
|
} |
|
145
|
2 |
|
return $this->get($offset); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
|
|
public function offsetSet($offset, $value) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->set($offset, $value); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
*/ |
|
159
|
|
|
public function offsetUnset($offset) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->remove($offset); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|