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