OverridesFractal::normalizeRelations()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Transformers\Concerns;
4
5
use League\Fractal\Resource\ResourceInterface;
6
use League\Fractal\Scope;
7
8
/**
9
 * A trait to be used by a transformer to override Fractal's transformer methods.
10
 *
11
 * @package flugger/laravel-responder
12
 * @author  Alexander Tømmerås <[email protected]>
13
 * @license The MIT License
14
 */
15
trait OverridesFractal
16
{
17
    /**
18
     * Overrides Fractal's getter for available includes.
19
     *
20
     * @return array
21
     */
22 47
    public function getAvailableIncludes()
23
    {
24 47
        if ($this->relations == ['*']) {
25 1
            return $this->resolveScopedIncludes($this->getCurrentScope());
26
        }
27
28 46
        return array_keys($this->normalizeRelations($this->relations));
29
    }
30
31
    /**
32
     * Overrides Fractal's getter for default includes.
33
     *
34
     * @return array
35
     */
36 46
    public function getDefaultIncludes()
37
    {
38 46
        return array_keys($this->normalizeRelations($this->load));
39
    }
40
41
    /**
42
     * Overrides Fractal's method for including a relation.
43
     *
44
     * @param  \League\Fractal\Scope $scope
45
     * @param  string                $identifier
46
     * @param  mixed                 $data
47
     * @return \League\Fractal\Resource\ResourceInterface
48
     */
49 23
    protected function callIncludeMethod(Scope $scope, $identifier, $data)
50
    {
51 23
        $parameters = iterator_to_array($scope->getManager()->getIncludeParams($scope->getIdentifier($identifier)));
52
53 23
        return $this->includeResource($identifier, $data, $parameters);
54
    }
55
56
    /**
57
     * Resolve scoped includes for the given scope.
58
     *
59
     * @param  \League\Fractal\Scope $scope
60
     * @return array
61
     */
62 1
    protected function resolveScopedIncludes(Scope $scope): array
63
    {
64 1
        $level = count($scope->getParentScopes());
65 1
        $includes = $scope->getManager()->getRequestedIncludes();
66
67
        return collect($includes)->map(function ($include) {
68 1
            return explode('.', $include);
69
        })->filter(function ($include) use ($level) {
70 1
            return count($include) > $level;
71 1
        })->pluck($level)->unique()->all();
72
    }
73
74
    /**
75
     * Get the current scope of the transformer.
76
     *
77
     * @return \League\Fractal\Scope
78
     */
79
    public abstract function getCurrentScope();
80
81
    /**
82
     * Normalize relations to force a key value structure.
83
     *
84
     * @param  array $relations
85
     * @return array
86
     */
87
    protected abstract function normalizeRelations(array $relations): array;
88
89
    /**
90
     * Include a related resource.
91
     *
92
     * @param  string $identifier
93
     * @param  mixed  $data
94
     * @param  array  $parameters
95
     * @return \League\Fractal\Resource\ResourceInterface
96
     */
97
    protected abstract function includeResource(string $identifier, $data, array $parameters): ResourceInterface;
98
}