Passed
Push — master ( c13b73...f2713c )
by Arthur
05:06
created

Transformer::getIncludedRelations()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 8
nop 1
dl 0
loc 24
rs 8.8333
c 0
b 0
f 0
ccs 14
cts 14
cp 1
crap 7
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 29.10.18
6
 * Time: 09:35.
7
 */
8
9
namespace Foundation\Abstracts\Transformers;
10
11
use Foundation\Contracts\Transformable;
12
use Illuminate\Http\Resources\Json\JsonResource;
13
14
abstract class Transformer extends JsonResource implements Transformable
15
{
16
    protected $include = [];
17
18
    protected $available = [];
19
20
    /**
21
     * Resolve the resource to an array.
22
     *
23
     * @param \Illuminate\Http\Request|null $request
24
     *
25
     * @return array
26 10
     */
27
    public function resolve($request = null)
28 10
    {
29
        return array_merge(parent::resolve($request), $this->filter($this->getIncludedRelations($request)));
30 10
    }
31
32
    protected function parseRequestIncludeParameter($request)
33 10
    {
34
        if (isset($request->include) && is_string($request->include))
35 10
            return explode(',', $request->include);
36 1
37
        return [];
38
    }
39 10
40
    protected function compileRelations(array $includedRequestRelations)
41
    {
42 10
        return array_unique(array_merge($this->include, array_intersect($this->available, $includedRequestRelations)));
43
    }
44 10
45
    public function getIncludedRelations($request)
46
    {
47 10
        $requestedRelations= $this->parseRequestIncludeParameter($request);
48
        $relations = [];
49 10
        foreach ($this->compileRelations($requestedRelations) as $relation) {
50 10
            if (is_string($relation) && method_exists($this, 'transform' . ucfirst(strtolower($relation)))) {
51 5
                $data = null;
52 5
                if ($this->resource !== null) {
53 5
                    $method = 'transform' . ucfirst(strtolower($relation));
54 5
                    $data = $this->$method($this->resource);
55 5
                }
56
                if ($data instanceof JsonResource) {
57 5
                    if ($data->resource === null)
58 4
                        $data = null;
59 1
                    else
60
                        $data->jsonSerialize();
61 3
                }
62
                $relations[strtolower($relation)] = $data;
63
            } else {
64 5
                throw new \Exception('invalid relation or not relation_transform_method given in ' . get_short_class_name(static::class));
65
            }
66 5
        }
67
68
        return $relations;
69
    }
70 10
71
    /**
72
     * @param $relations
73
     */
74
    public function include($relations)
75
    {
76 2
        if (is_string($relations))
77
            $relations = [$relations];
78 2
        $this->include = array_unique(array_merge($this->include, $relations));
79 2
        return $this;
80
    }
81 2
82
    /**
83 2
     * @param $relations
84
     */
85
    public function available($relations)
86
    {
87
        if (is_string($relations))
88
            $relations = [$relations];
89
        $this->available = array_unique(array_merge($this->available, $relations));
90
        return $this;
91
    }
92
93
    public static function resource($model): self
94
    {
95
        return new static($model);
96
    }
97
98
    public static function collection($resource)
99 10
    {
100
        return new TransformerCollection($resource, static::class);
101 10
    }
102
103
    public function serialize()
104 6
    {
105
        return json_decode(json_encode($this->jsonSerialize()), true);
106 6
    }
107
}
108