1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DataObjects\Casts\Metadata; |
4
|
|
|
|
5
|
|
|
use ArrayObject as BaseArrayObject; |
6
|
|
|
use JsonSerializable; |
7
|
|
|
use Serializable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ArrayObject |
11
|
|
|
* @package ByTIC\DataObjects\Casts\Metadata |
12
|
|
|
*/ |
13
|
|
|
class Metadata extends BaseArrayObject implements JsonSerializable, Serializable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var null|callable |
17
|
|
|
*/ |
18
|
|
|
protected $observer = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $key |
22
|
|
|
* @param $default |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
|
|
public function get(string $key, $default = null) |
26
|
|
|
{ |
27
|
|
|
if (strpos($key, '.') === false) { |
28
|
|
|
$value = $this->getByKey($key); |
29
|
|
|
} else { |
30
|
|
|
$value = $this->getByPath($key); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $value === null ? $default : $value; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $name |
38
|
|
|
* @param $value |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function set($name, $value) |
42
|
|
|
{ |
43
|
|
|
return $this->setDataItem($name, $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $key |
48
|
|
|
* @return mixed|null |
49
|
|
|
*/ |
50
|
|
|
public function getByKey($key) |
51
|
|
|
{ |
52
|
|
|
if ($this->hasByKey($key)) { |
53
|
|
|
return $this[$key]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Determine if the given configuration value exists. |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasByKey($key) |
66
|
|
|
{ |
67
|
|
|
return isset($this[$key]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $path |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function getByPath(string $path) |
75
|
|
|
{ |
76
|
|
|
$segments = explode('.', $path); |
77
|
|
|
$value = $this; |
78
|
|
|
foreach ($segments as $segment) { |
79
|
|
|
if (isset($value[$segment])) { |
80
|
|
|
$value = $value[$segment]; |
81
|
|
|
} else { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $value; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $name |
91
|
|
|
* @param $value |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
protected function setDataItem($name, $value) |
95
|
|
|
{ |
96
|
|
|
if (null === $name) { |
97
|
|
|
$this[] = $value; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (strpos($name, '.') === false) { |
102
|
|
|
$this[$name] = $value; |
103
|
|
|
$this->callObserver(); |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
$parts = explode('.', $name); |
107
|
|
|
$ptr =& $this; |
108
|
|
|
|
109
|
|
|
if (is_array($parts)) { |
|
|
|
|
110
|
|
|
foreach ($parts as $part) { |
111
|
|
|
if ('[]' == $part) { |
112
|
|
|
if (is_array($ptr)) { |
113
|
|
|
$ptr =& $ptr[]; |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
if (!isset($ptr[$part])) { |
117
|
|
|
$ptr[$part] = []; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$ptr =& $ptr[$part]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$ptr = $value; |
126
|
|
|
$this->callObserver(); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the instance as an array. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function toArray() |
137
|
|
|
{ |
138
|
|
|
return $this->getArrayCopy(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function serialize() |
145
|
|
|
{ |
146
|
|
|
return serialize($this->toArray()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Constructs the object. |
151
|
|
|
* @link https://php.net/manual/en/serializable.unserialize.php |
152
|
|
|
* @param string $serialized The string representation of the object. |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function unserialize($data) |
156
|
|
|
{ |
157
|
|
|
$data = @unserialize($data); |
158
|
|
|
if (!is_array($data)) { |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
foreach ($data as $property => $value) { |
162
|
|
|
$this[$property] = $value; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the array that should be JSON serialized. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function jsonSerialize() |
172
|
|
|
{ |
173
|
|
|
return $this->getArrayCopy(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
public function offsetSet($key, $value) |
180
|
|
|
{ |
181
|
|
|
parent::offsetSet($key, $value); |
182
|
|
|
$this->callObserver(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param callable|null $observer |
187
|
|
|
* @return Metadata |
188
|
|
|
*/ |
189
|
|
|
public function setObserver(?callable $observer): self |
190
|
|
|
{ |
191
|
|
|
$this->observer = $observer; |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function callObserver() |
196
|
|
|
{ |
197
|
|
|
call_user_func($this->observer, $this); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|