1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Slice; |
4
|
|
|
|
5
|
|
|
use Bavix\Exceptions; |
6
|
|
|
use Bavix\Helpers\Arr; |
7
|
|
|
use Bavix\Helpers\Str; |
8
|
|
|
use Bavix\Iterator\Iterator; |
9
|
|
|
|
10
|
|
|
class Slice extends Iterator |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Slice constructor. |
15
|
|
|
* |
16
|
|
|
* @param array $data |
17
|
|
|
* @param array|Slice $parameters |
18
|
|
|
*/ |
19
|
12 |
|
public function __construct(array $data, $parameters = null) |
20
|
|
|
{ |
21
|
12 |
|
parent::__construct($data); |
22
|
|
|
|
23
|
12 |
|
if (null !== $parameters) |
24
|
|
|
{ |
25
|
12 |
|
$this->walk($parameters); |
26
|
|
|
} |
27
|
12 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array|\Traversable $data |
31
|
|
|
* |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
1 |
|
public static function from($data): self |
35
|
|
|
{ |
36
|
1 |
|
if ($data instanceof self) |
37
|
|
|
{ |
38
|
1 |
|
return $data; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return new static( |
42
|
1 |
|
Arr::iterator($data) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param int|bool $depth |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
5 |
|
public function asArray($depth = INF) |
52
|
|
|
{ |
53
|
5 |
|
if (!$depth || $depth <= 0) |
54
|
|
|
{ |
55
|
1 |
|
return $this->data; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
$results = []; |
59
|
|
|
|
60
|
5 |
|
foreach (parent::asArray() as $key => $data) |
61
|
|
|
{ |
62
|
5 |
|
$results[$key] = |
63
|
5 |
|
$data instanceof self ? |
64
|
1 |
|
$data->asArray(\is_bool($depth) ? INF : --$depth) : |
65
|
5 |
|
$data; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
return $results; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Slice|array $slice |
73
|
|
|
*/ |
74
|
12 |
|
protected function walk($slice) |
75
|
|
|
{ |
76
|
12 |
|
if (\is_array($slice)) |
77
|
|
|
{ |
78
|
12 |
|
$slice = $this->make($slice); |
79
|
|
|
} |
80
|
|
|
|
81
|
12 |
|
Arr::walkRecursive($this->data, function (&$value) use ($slice) { |
82
|
|
|
|
83
|
12 |
|
if (\is_object($value) && $value instanceof Raw) |
84
|
|
|
{ |
85
|
12 |
|
$value = $value->getData(); |
86
|
12 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
12 |
|
if (empty($value) || !\is_string($value)) |
90
|
|
|
{ |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
12 |
|
if (Str::first($value) === '%' && |
95
|
12 |
|
Str::last($value) === '%' && |
96
|
12 |
|
\substr_count($value, '%') === 2) |
97
|
|
|
{ |
98
|
12 |
|
$path = Str::sub($value, 1, -1); |
99
|
12 |
|
$value = $slice->getRequired($path); |
100
|
|
|
} |
101
|
|
|
|
102
|
12 |
|
}); |
103
|
12 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return \Generator|Slice[] |
107
|
|
|
*/ |
108
|
1 |
|
public function asGenerator() |
109
|
|
|
{ |
110
|
1 |
|
foreach ($this->data as $key => $object) |
111
|
|
|
{ |
112
|
1 |
|
yield $key => $this->make($object); |
113
|
|
|
} |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
1 |
|
public function keys() |
120
|
|
|
{ |
121
|
1 |
|
return Arr::getKeys($this->data); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Slice[] |
126
|
|
|
*/ |
127
|
1 |
|
public function asObject() |
128
|
|
|
{ |
129
|
1 |
|
return Arr::iterator($this->asGenerator()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param array $data |
134
|
|
|
* |
135
|
|
|
* @return static |
136
|
|
|
*/ |
137
|
12 |
|
public function setData(array $data) |
138
|
|
|
{ |
139
|
12 |
|
$this->data = $data; |
140
|
|
|
|
141
|
12 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $offset |
146
|
|
|
* @param mixed $default |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
2 |
|
public function getData($offset, $default = null) |
151
|
|
|
{ |
152
|
2 |
|
return Arr::get($this->data, $offset, $default); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $offset |
157
|
|
|
* |
158
|
|
|
* @return Slice |
159
|
|
|
*/ |
160
|
1 |
|
public function getSlice($offset) |
161
|
|
|
{ |
162
|
1 |
|
return $this->make($this->getRequired($offset)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $data |
167
|
|
|
* |
168
|
|
|
* @return Slice |
169
|
|
|
*/ |
170
|
12 |
|
public function make(array $data) |
171
|
|
|
{ |
172
|
12 |
|
return (clone $this)->setData($data); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $offset |
177
|
|
|
* |
178
|
|
|
* @return array|mixed |
179
|
|
|
*/ |
180
|
12 |
|
public function getRequired($offset) |
181
|
|
|
{ |
182
|
12 |
|
return Arr::getRequired($this->data, $offset); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @inheritdoc |
187
|
|
|
*/ |
188
|
1 |
|
public function offsetExists($offset) |
189
|
|
|
{ |
190
|
1 |
|
return Arr::get($this->data, $offset) !== null; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
4 |
|
public function offsetGet($offset) |
197
|
|
|
{ |
198
|
4 |
|
return $this->getRequired($offset); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @inheritdoc |
203
|
|
|
*/ |
204
|
3 |
|
public function offsetSet($offset, $value) |
205
|
|
|
{ |
206
|
3 |
|
if (null === $offset) |
207
|
|
|
{ |
208
|
1 |
|
throw new Exceptions\Invalid('Slice does not support NULL'); |
209
|
|
|
} |
210
|
|
|
|
211
|
2 |
|
Arr::set($this->data, $offset, $value); |
212
|
2 |
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @inheritdoc |
216
|
|
|
*/ |
217
|
2 |
|
public function offsetUnset($offset) |
218
|
|
|
{ |
219
|
2 |
|
Arr::remove($this->data, $offset); |
220
|
2 |
|
} |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|