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