Completed
Pull Request — master (#324)
by
unknown
05:43
created

Scope::getParentScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the League\Fractal package.
5
 *
6
 * (c) Phil Sturgeon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 **/
11
12
namespace League\Fractal;
13
14
use InvalidArgumentException;
15
use League\Fractal\Resource\Collection;
16
use League\Fractal\Resource\Item;
17
use League\Fractal\Resource\NullResource;
18
use League\Fractal\Resource\ResourceInterface;
19
use League\Fractal\Serializer\SerializerAbstract;
20
21
/**
22
 * Scope
23
 *
24
 * The scope class acts as a tracker, relating a specific resource in a specific
25
 * context. For example, the same resource could be attached to multiple scopes.
26
 * There are root scopes, parent scopes and child scopes.
27
 */
28
class Scope
29
{
30
    /**
31
     * @var array
32
     */
33
    protected $availableIncludes = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $scopeIdentifier;
39
40
    /**
41
     * @var \League\Fractal\Manager
42
     */
43
    protected $manager;
44
45
    /**
46
     * @var ResourceInterface
47
     */
48
    protected $resource;
49
50
    /**
51
     * @var array
52
     */
53
    protected $parentScopes = [];
54
55
    /**
56
     * Create a new scope instance.
57
     *
58
     * @param Manager           $manager
59
     * @param ResourceInterface $resource
60
     * @param string            $scopeIdentifier
61
     *
62
     * @return void
63
     */
64 56
    public function __construct(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null)
65
    {
66 56
        $this->manager = $manager;
67 56
        $this->resource = $resource;
68 56
        $this->scopeIdentifier = $scopeIdentifier;
69 56
    }
70
71
    /**
72
     * Embed a scope as a child of the current scope.
73
     *
74
     * @internal
75
     *
76
     * @param string            $scopeIdentifier
77
     * @param ResourceInterface $resource
78
     *
79
     * @return \League\Fractal\Scope
80
     */
81 29
    public function embedChildScope($scopeIdentifier, $resource)
82
    {
83 29
        return $this->manager->createData($resource, $scopeIdentifier, $this);
84
    }
85
86
    /**
87
     * Get the current identifier.
88
     *
89
     * @return string
90
     */
91 29
    public function getScopeIdentifier()
92
    {
93 29
        return $this->scopeIdentifier;
94
    }
95
96
    /**
97
     * Get the unique identifier for this scope.
98
     *
99
     * @param string $appendIdentifier
100
     *
101
     * @return string
102
     */
103 25
    public function getIdentifier($appendIdentifier = null)
104
    {
105 25
        $identifierParts = array_merge($this->parentScopes, [$this->scopeIdentifier, $appendIdentifier]);
106
107 25
        return implode('.', array_filter($identifierParts));
108
    }
109
110
    /**
111
     * Getter for parentScopes.
112
     *
113
     * @return mixed
114
     */
115 30
    public function getParentScopes()
116
    {
117 30
        return $this->parentScopes;
118
    }
119
120
    /**
121
     * Getter for resource.
122
     *
123
     * @return ResourceInterface
124
     */
125 1
    public function getResource()
126
    {
127 1
        return $this->resource;
128
    }
129
130
    /**
131
     * Getter for manager.
132
     *
133
     * @return \League\Fractal\Manager
134
     */
135 25
    public function getManager()
136
    {
137 25
        return $this->manager;
138 1
    }
139
140
    /**
141
     * Is Requested.
142
     *
143
     * Check if - in relation to the current scope - this specific segment is allowed.
144
     * That means, if a.b.c is requested and the current scope is a.b, then c is allowed. If the current
145
     * scope is a then c is not allowed, even if it is there and potentially transformable.
146
     *
147
     * @internal
148
     *
149
     * @param string $checkScopeSegment
150
     *
151
     * @return bool Returns the new number of elements in the array.
152
     */
153 34 View Code Duplication
    public function isRequested($checkScopeSegment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155 34
        if ($this->parentScopes) {
156 16
            $scopeArray = array_slice($this->parentScopes, 1);
157 16
            array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment);
158 16
        } else {
159 34
            $scopeArray = [$checkScopeSegment];
160
        }
161
162 34
        $scopeString = implode('.', (array) $scopeArray);
163
164 34
        return in_array($scopeString, $this->manager->getRequestedIncludes());
165
    }
166
167
    /**
168
     * Is Excluded.
169
     *
170
     * Check if - in relation to the current scope - this specific segment should
171
     * be excluded. That means, if a.b.c is excluded and the current scope is a.b,
172
     * then c will not be allowed in the transformation whether it appears in
173
     * the list of default or available, requested includes.
174
     *
175
     * @internal
176
     *
177
     * @param string $checkScopeSegment
178
     *
179
     * @return bool
180
     */
181 25 View Code Duplication
    public function isExcluded($checkScopeSegment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183 25
        if ($this->parentScopes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->parentScopes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
184 3
            $scopeArray = array_slice($this->parentScopes, 1);
185 3
            array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment);
186 3
        } else {
187 25
            $scopeArray = [$checkScopeSegment];
188
        }
189
190 25
        $scopeString = implode('.', (array) $scopeArray);
191
192 25
        return in_array($scopeString, $this->manager->getRequestedExcludes());
193
    }
194
195
    /**
196
     * Push Parent Scope.
197
     *
198
     * Push a scope identifier into parentScopes
199
     *
200
     * @internal
201
     *
202
     * @param string $identifierSegment
203
     *
204
     * @return int Returns the new number of elements in the array.
205
     */
206 1
    public function pushParentScope($identifierSegment)
207
    {
208 1
        return array_push($this->parentScopes, $identifierSegment);
209
    }
210
211
    /**
212
     * Set parent scopes.
213
     *
214
     * @internal
215
     *
216
     * @param string[] $parentScopes Value to set.
217
     *
218
     * @return $this
219
     */
220 29
    public function setParentScopes($parentScopes)
221
    {
222 29
        $this->parentScopes = $parentScopes;
223
224 29
        return $this;
225
    }
226
227
    /**
228
     * Convert the current data for this scope to an array.
229
     *
230
     * @return array
231
     */
232 45
    public function toArray()
233
    {
234 45
        list($rawData, $rawIncludedData) = $this->executeResourceTransformers();
235
236 44
        $serializer = $this->manager->getSerializer();
237
238 44
        $data = $this->serializeResource($serializer, $rawData);
239
240
        // If the serializer wants the includes to be side-loaded then we'll
241
        // serialize the included data and merge it with the data.
242 43
        if ($serializer->sideloadIncludes()) {
243 27
            $includedData = $serializer->includedData($this->resource, $rawIncludedData);
244
245
            // If the serializer wants to inject additional information
246
            // about the included resources, it can do so now.
247 27
            $data = $serializer->injectData($data, $rawIncludedData);
248
249 27
            if ($this->isRootScope()) {
250
                // If the serializer wants to have a final word about all
251
                // the objects that are sideloaded, it can do so now.
252 27
                $includedData = $serializer->filterIncludes(
253 27
                    $includedData,
254
                    $data
255 27
                );
256 27
            }
257
258 27
            $data = array_merge($data, $includedData);
259 27
        }
260
261 43
        if ($this->resource instanceof Collection) {
262 26
            if ($this->resource->hasCursor()) {
263 1
                $pagination = $serializer->cursor($this->resource->getCursor());
264 26
            } elseif ($this->resource->hasPaginator()) {
265 4
                $pagination = $serializer->paginator($this->resource->getPaginator());
266 4
            }
267
268 26
            if (! empty($pagination)) {
269 5
                $this->resource->setMetaValue(key($pagination), current($pagination));
270 5
            }
271 26
        }
272
273
        // Pull out all of OUR metadata and any custom meta data to merge with the main level data
274 43
        $meta = $serializer->meta($this->resource->getMeta());
275
276
        // in case of returning NullResource we should return null and not to go with array_merge
277 43
        if (is_null($data)) {
278
            if (!empty($meta)) {
279
                return $meta;
280
            }
281
            return null;
282
        }
283
284 43
        return array_merge($data, $meta);
285
    }
286
287
    /**
288
     * Convert the current data for this scope to JSON.
289
     *
290
     * @return string
291
     */
292 32
    public function toJson()
293
    {
294 32
        return json_encode($this->toArray());
295
    }
296
297
    /**
298
     * Execute the resources transformer and return the data and included data.
299
     *
300
     * @internal
301
     *
302
     * @return array
303
     */
304 45
    protected function executeResourceTransformers()
305
    {
306 45
        $transformer = $this->resource->getTransformer();
307 45
        $data = $this->resource->getData();
308
309 45
        $transformedData = $includedData = [];
310
311 45
        if ($this->resource instanceof Item) {
312 29
            list($transformedData, $includedData[]) = $this->fireTransformer($transformer, $data);
313 45
        } elseif ($this->resource instanceof Collection) {
314 26
            foreach ($data as $value) {
315 25
                list($transformedData[], $includedData[]) = $this->fireTransformer($transformer, $value);
316 26
            }
317 31
        } elseif ($this->resource instanceof NullResource) {
318 4
            $transformedData = null;
319 4
            $includedData = [];
320 4
        } else {
321 2
            throw new InvalidArgumentException(
322
                'Argument $resource should be an instance of League\Fractal\Resource\Item'
323
                .' or League\Fractal\Resource\Collection'
324 2
            );
325
        }
326
327 43
        return [$transformedData, $includedData];
328
    }
329
330
    /**
331
     * Serialize a resource
332
     *
333
     * @internal
334
     *
335
     * @param SerializerAbstract $serializer
336
     * @param mixed              $data
337
     *
338
     * @return array
339
     */
340 43
    protected function serializeResource(SerializerAbstract $serializer, $data)
341
    {
342 43
        $resourceKey = $this->resource->getResourceKey();
343
344 43
        if ($this->resource instanceof Collection) {
345 26
            return $serializer->collection($resourceKey, $data);
346
        }
347
348 31
        if ($this->resource instanceof Item) {
349 29
            return $serializer->item($resourceKey, $data);
350
        }
351
352 4
        return $serializer->null();
353
    }
354
355
    /**
356
     * Fire the main transformer.
357
     *
358
     * @internal
359
     *
360
     * @param TransformerAbstract|callable $transformer
361
     * @param mixed                        $data
362
     *
363
     * @return array
364
     */
365 41
    protected function fireTransformer($transformer, $data)
366
    {
367 41
        $includedData = [];
368
369 41
        if (is_callable($transformer)) {
370 4
            $transformedData = call_user_func($transformer, $data);
371 4
        } else {
372 37
            $transformer->setCurrentScope($this);
373 37
            $transformedData = $transformer->transform($data);
374
        }
375
376 41
        if ($this->transformerHasIncludes($transformer)) {
377 35
            $includedData = $this->fireIncludedTransformers($transformer, $data);
378 35
            $transformedData = $this->manager->getSerializer()->mergeIncludes($transformedData, $includedData);
379 35
        }
380
381 41
        return [$transformedData, $includedData];
382
    }
383
384
    /**
385
     * Fire the included transformers.
386
     *
387
     * @internal
388
     *
389
     * @param \League\Fractal\TransformerAbstract $transformer
390
     * @param mixed                               $data
391
     *
392
     * @return array
393
     */
394 35
    protected function fireIncludedTransformers($transformer, $data)
395
    {
396 35
        $this->availableIncludes = $transformer->getAvailableIncludes();
397
398 35
        return $transformer->processIncludedResources($this, $data) ?: [];
399
    }
400
401
    /**
402
     * Determine if a transformer has any available includes.
403
     *
404
     * @internal
405
     *
406
     * @param TransformerAbstract|callable $transformer
407
     *
408
     * @return bool
409
     */
410 41
    protected function transformerHasIncludes($transformer)
411
    {
412 41
        if (! $transformer instanceof TransformerAbstract) {
413 4
            return false;
414
        }
415
416 37
        $defaultIncludes = $transformer->getDefaultIncludes();
417 37
        $availableIncludes = $transformer->getAvailableIncludes();
418
419 37
        return ! empty($defaultIncludes) || ! empty($availableIncludes);
420
    }
421
422
    /**
423
     * Check, if this is the root scope.
424
     *
425
     * @return bool
426
     */
427 27
    protected function isRootScope()
428
    {
429 27
        return empty($this->parentScopes);
430
    }
431
}
432