Passed
Push — v2 ( e7bb8d...182303 )
by Alexander
02:36
created

OverridesFractal::getScopedParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Transformers\Concerns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use League\Fractal\ParamBag;
7
use League\Fractal\Scope;
8
use LogicException;
9
10
/**
11
 * A trait to be used by a transformer to override Fractal's transformer methods.
12
 *
13
 * @package flugger/laravel-responder
14
 * @author  Alexander Tømmerås <[email protected]>
15
 * @license The MIT License
16
 */
17
trait OverridesFractal
18
{
19
    /**
20
     * Overrides Fractal's getter for available includes.
21
     *
22
     * @return array
23
     */
24
    public function getAvailableIncludes()
25
    {
26
        return $this->availableIncludes ?: [];
27
    }
28
29
    /**
30
     * Overrides Fractal's getter for default includes.
31
     *
32
     * @return array
33
     */
34
    public function getDefaultIncludes()
35
    {
36
        return array_keys($this->load ?: []);
37
    }
38
39
    /**
40
     * Overrides Fractal's method for including a relation.
41
     *
42
     * @param  \League\Fractal\Scope $scope
43
     * @param  string                $relation
44
     * @param  mixed                 $data
45
     * @return \League\Fractal\Resource\ResourceInterface
46
     * @throws \LogicException
47
     */
48
    protected function callIncludeMethod(Scope $scope, $relation, $data)
49
    {
50
        $parameters = $this->getScopedParameters($scope, $relation);
51
52
        if ($method = $this->getIncludeMethod($relation)) {
53
            return $this->$method($this->filterData($data, $relation), $parameters);
54
        } elseif ($data instanceof Model) {
55
            return $this->makeResource($relation, $this->filterData($data->$relation, $relation));
56
        }
57
58
        throw new LogicException('Cannot resolve relation [' . $relation . '] in [' . self::class . ']');
59
    }
60
61
    /**
62
     * Get scoped parameters for a relation.
63
     *
64
     * @param  \League\Fractal\Scope $scope
65
     * @param  string                $relation
66
     * @return \League\Fractal\ParamBag
67
     */
68
    protected function getScopedParameters(Scope $scope, string $relation): ParamBag
69
    {
70
        return $scope->getManager()->getIncludeParams($scope->getIdentifier($relation));
71
    }
72
73
    /**
74
     * Get the name of an existing include method.
75
     *
76
     * @param  string $relation
77
     * @return string|null
78
     */
79
    protected function getIncludeMethod(string $relation)
80
    {
81
        return method_exists($this, $method = 'include' . ucfirst($relation)) ? $method : null;
82
    }
83
84
    /**
85
     * Filter data using a filter method.
86
     *
87
     * @param  mixed  $data
88
     * @param  string $relation
89
     * @return mixed
90
     */
91
    protected function filterData($data, string $relation)
92
    {
93
        if (! $method = $this->getFilterMethod($relation)) {
94
            return $data;
95
        }
96
97
        return $method($data);
98
99
    }
100
101
    /**
102
     * Get the name of an existing filter method.
103
     *
104
     * @param  string $relation
105
     * @return string|null
106
     */
107
    protected function getFilterMethod(string $relation)
108
    {
109
        return method_exists($this, $method = 'filter' . ucfirst($relation)) ? $method : null;
110
    }
111
112
    /**
113
     * Make a related resource.
114
     *
115
     * @param  string $relation
116
     * @param  mixed  $data
117
     * @return \League\Fractal\Resource\ResourceInterface|false
118
     */
119
    protected abstract function makeResource(string $relation, $data);
120
}