1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Flipbox Factory |
5
|
|
|
* @copyright Copyright (c) 2017, Flipbox Digital |
6
|
|
|
* @link https://github.com/flipbox/transform/releases/latest |
7
|
|
|
* @license https://github.com/flipbox/transform/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Flipbox\Transform; |
11
|
|
|
|
12
|
|
|
use Flipbox\Transform\Transformers\TransformerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Flipbox Factory <[email protected]> |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class Scope |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $scopeIdentifier; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Transform |
27
|
|
|
*/ |
28
|
|
|
protected $transform; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $parentScopes = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Scope constructor. |
37
|
|
|
* @param Transform $transform |
38
|
|
|
* @param string|null $scopeIdentifier |
39
|
|
|
* @param array $parentScopes |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
Transform $transform, |
43
|
|
|
string $scopeIdentifier = null, |
44
|
|
|
array $parentScopes = [] |
45
|
|
|
) { |
46
|
|
|
$this->transform = $transform; |
47
|
|
|
$this->scopeIdentifier = $scopeIdentifier; |
48
|
|
|
$this->parentScopes = $parentScopes; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Transform |
53
|
|
|
*/ |
54
|
|
|
public function getTransform(): Transform |
55
|
|
|
{ |
56
|
|
|
return $this->transform; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $key |
61
|
|
|
* @return ParamBag |
62
|
|
|
*/ |
63
|
|
|
public function getParams(string $key = null): ParamBag |
64
|
|
|
{ |
65
|
|
|
return $this->getTransform()->getParams( |
66
|
|
|
$this->getIdentifier($key) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the current identifier. |
72
|
|
|
* |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
|
|
public function getScopeIdentifier() |
76
|
|
|
{ |
77
|
|
|
return $this->scopeIdentifier; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the unique identifier for this scope. |
82
|
|
|
* |
83
|
|
|
* @param string $appendIdentifier |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getIdentifier(string $appendIdentifier = null): string |
88
|
|
|
{ |
89
|
|
|
return implode( |
90
|
|
|
'.', |
91
|
|
|
array_filter(array_merge( |
92
|
|
|
$this->parentScopes, |
93
|
|
|
[ |
94
|
|
|
$this->scopeIdentifier, |
95
|
|
|
$appendIdentifier |
96
|
|
|
] |
97
|
|
|
)) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Getter for parentScopes. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getParentScopes(): array |
107
|
|
|
{ |
108
|
|
|
return $this->parentScopes; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Is Requested. |
113
|
|
|
* |
114
|
|
|
* Check if - in relation to the current scope - this specific segment is allowed. |
115
|
|
|
* That means, if a.b.c is requested and the current scope is a.b, then c is allowed. If the current |
116
|
|
|
* scope is a then c is not allowed, even if it is there and potentially transformable. |
117
|
|
|
* |
118
|
|
|
* @internal |
119
|
|
|
* |
120
|
|
|
* @param string $checkScopeSegment |
121
|
|
|
* |
122
|
|
|
* @return bool Returns the new number of elements in the array. |
123
|
|
|
*/ |
124
|
|
|
public function isRequested($checkScopeSegment): bool |
125
|
|
|
{ |
126
|
|
|
return in_array( |
127
|
|
|
$this->scopeString($checkScopeSegment), |
128
|
|
|
$this->transform->getIncludes() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Is Excluded. |
134
|
|
|
* |
135
|
|
|
* Check if - in relation to the current scope - this specific segment should |
136
|
|
|
* be excluded. That means, if a.b.c is excluded and the current scope is a.b, |
137
|
|
|
* then c will not be allowed in the transformation whether it appears in |
138
|
|
|
* the list of default or available, requested includes. |
139
|
|
|
* |
140
|
|
|
* @internal |
141
|
|
|
* |
142
|
|
|
* @param string $checkScopeSegment |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isExcluded($checkScopeSegment): bool |
147
|
|
|
{ |
148
|
|
|
return in_array( |
149
|
|
|
$this->scopeString($checkScopeSegment), |
150
|
|
|
$this->transform->getExcludes() |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param TransformerInterface|callable $transformer |
156
|
|
|
* @param mixed $data |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function transform(callable $transformer, $data): array |
160
|
|
|
{ |
161
|
|
|
$includedData = []; |
162
|
|
|
|
163
|
|
|
// Transform data |
164
|
|
|
$transformedData = $this->parseValue($transformer, $data); |
165
|
|
|
|
166
|
|
|
// Bail now |
167
|
|
|
if (null === $transformedData) { |
168
|
|
|
return $includedData; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
foreach ($transformedData as $key => $val) { |
|
|
|
|
172
|
|
|
if (!$this->includeValue($transformer, $key)) { |
173
|
|
|
continue; |
174
|
|
|
} |
175
|
|
|
$includedData[$key] = $this->parseValue($val, $data, $key); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// Return only the requested fields |
179
|
|
|
$includedData = $this->filterFields($includedData); |
180
|
|
|
|
181
|
|
|
return $includedData; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param callable $transformer |
186
|
|
|
* @param string $key |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
protected function includeValue(callable $transformer, string $key): bool |
190
|
|
|
{ |
191
|
|
|
// Ignore optional (that have not been explicitly requested) |
192
|
|
|
if ($transformer instanceof TransformerInterface && |
193
|
|
|
in_array($key, $transformer->getIncludes(), true) && |
194
|
|
|
!$this->isRequested($key) |
195
|
|
|
) { |
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Ignore excludes |
200
|
|
|
if ($this->isExcluded($key)) { |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return true; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param $val |
209
|
|
|
* @param $data |
210
|
|
|
* @param string|null $key |
211
|
|
|
* @return array|string|null |
212
|
|
|
*/ |
213
|
|
|
protected function parseValue($val, $data, string $key = null) |
214
|
|
|
{ |
215
|
|
|
if (is_callable($val)) { |
216
|
|
|
return call_user_func_array($val, [$data, $this, $key]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $val; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $identifier |
224
|
|
|
* @return Scope |
225
|
|
|
*/ |
226
|
|
|
public function childScope(string $identifier): Scope |
227
|
|
|
{ |
228
|
|
|
$parentScopes = $this->getParentScopes(); |
229
|
|
|
$parentScopes[] = $this->getScopeIdentifier(); |
230
|
|
|
|
231
|
|
|
return new Scope( |
232
|
|
|
$this->getTransform(), |
233
|
|
|
$identifier, |
234
|
|
|
$parentScopes |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Check, if this is the root scope. |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
protected function isRootScope(): bool |
244
|
|
|
{ |
245
|
|
|
return empty($this->parentScopes); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Filter the provided data with the requested filter fields for |
250
|
|
|
* the scope resource |
251
|
|
|
* |
252
|
|
|
* @internal |
253
|
|
|
* |
254
|
|
|
* @param array $data |
255
|
|
|
* |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
protected function filterFields(array $data): array |
259
|
|
|
{ |
260
|
|
|
$fields = $this->getFilterFields(); |
261
|
|
|
|
262
|
|
|
if ($fields === null) { |
263
|
|
|
return $data; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return array_intersect_key( |
267
|
|
|
$data, |
268
|
|
|
array_flip( |
269
|
|
|
iterator_to_array($fields) |
270
|
|
|
) |
271
|
|
|
); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Return the requested filter fields for the scope resource |
276
|
|
|
* |
277
|
|
|
* @internal |
278
|
|
|
* |
279
|
|
|
* @return ParamBag|null |
280
|
|
|
*/ |
281
|
|
|
protected function getFilterFields() |
282
|
|
|
{ |
283
|
|
|
return $this->transform->getField( |
284
|
|
|
$this->getScopeIdentifier() |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param string $checkScopeSegment |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
|
|
private function scopeString(string $checkScopeSegment): string |
293
|
|
|
{ |
294
|
|
|
if ($this->parentScopes) { |
|
|
|
|
295
|
|
|
$scopeArray = array_slice($this->parentScopes, 1); |
296
|
|
|
array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment); |
297
|
|
|
} else { |
298
|
|
|
$scopeArray = [$checkScopeSegment]; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return implode('.', (array)$scopeArray); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.