|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright MediaCT. All rights reserved. |
|
4
|
|
|
* https://www.mediact.nl |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Mediact\DataContainer; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Implements DataContainerInterface |
|
11
|
|
|
*/ |
|
12
|
|
|
trait DataContainerDecoratorTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var DataContainerInterface */ |
|
15
|
|
|
private $storage; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Set data on a new storage. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $data |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function setData(array $data) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->storage = new DataContainer($data); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set the storage. |
|
31
|
|
|
* |
|
32
|
|
|
* @param DataContainerInterface $storage |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function setStorage(DataContainerInterface $storage) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->storage = $storage; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the storage. |
|
43
|
|
|
* |
|
44
|
|
|
* @return DataContainerInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getStorage(): DataContainerInterface |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->storage; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Check whether a path exists. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $path |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public function has(string $path): bool |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getStorage() |
|
61
|
|
|
->has($path); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get a value of a path. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $path |
|
68
|
|
|
* @param mixed $default |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public function get(string $path, $default = null) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->getStorage() |
|
75
|
|
|
->get($path, $default); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the contained array. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function all(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getStorage() |
|
86
|
|
|
->all(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set a value on a path. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $path |
|
93
|
|
|
* @param mixed $value |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function set(string $path, $value = null) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->getStorage() |
|
100
|
|
|
->set($path, $value); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Remove a path if it exists. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $pattern |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function remove(string $pattern) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->getStorage() |
|
113
|
|
|
->remove($pattern); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Find paths that match a pattern. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $pattern |
|
120
|
|
|
* |
|
121
|
|
|
* @return string[] |
|
122
|
|
|
*/ |
|
123
|
|
|
public function glob(string $pattern): array |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->getStorage() |
|
126
|
|
|
->glob($pattern); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Find paths that match a pattern an their replacements. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $pattern |
|
133
|
|
|
* @param string $replacement |
|
134
|
|
|
* |
|
135
|
|
|
* @return string[] |
|
136
|
|
|
*/ |
|
137
|
|
|
public function expand(string $pattern, string $replacement): array |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->getStorage() |
|
140
|
|
|
->expand($pattern, $replacement); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Branch into a list of data containers. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $pattern |
|
147
|
|
|
* |
|
148
|
|
|
* @return DataContainerInterface[] |
|
149
|
|
|
*/ |
|
150
|
|
|
public function branch(string $pattern): array |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->getStorage() |
|
153
|
|
|
->branch($pattern); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get a node from the container. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $path |
|
160
|
|
|
* |
|
161
|
|
|
* @return DataContainerInterface |
|
162
|
|
|
*/ |
|
163
|
|
|
public function node(string $path): DataContainerInterface |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->getStorage() |
|
166
|
|
|
->node($path); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Copy paths matching a pattern to another path. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $pattern |
|
173
|
|
|
* @param string $replacement |
|
174
|
|
|
* |
|
175
|
|
|
* @return void |
|
176
|
|
|
*/ |
|
177
|
|
|
public function copy(string $pattern, string $replacement) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->getStorage() |
|
180
|
|
|
->copy($pattern, $replacement); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Move paths matching a pattern to another path. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $pattern |
|
187
|
|
|
* @param string $replacement |
|
188
|
|
|
* |
|
189
|
|
|
* @return void |
|
190
|
|
|
*/ |
|
191
|
|
|
public function move(string $pattern, string $replacement) |
|
192
|
|
|
{ |
|
193
|
|
|
$this->getStorage() |
|
194
|
|
|
->move($pattern, $replacement); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Prepare the object for cloning. |
|
199
|
|
|
* |
|
200
|
|
|
* @return void |
|
201
|
|
|
*/ |
|
202
|
|
|
public function __clone() |
|
203
|
|
|
{ |
|
204
|
|
|
$this->storage = clone $this->storage; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|