Completed
Push — master ( b01d16...d9d1e2 )
by Ruben
03:30
created

LinkResource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\ResourceLinks;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Spatie\ResourceLinks\EndpointTypes\ControllerEndpointType;
8
use Spatie\ResourceLinks\EndpointTypes\EndpointType;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Http\Resources\Json\JsonResource;
11
12
class LinkResource extends JsonResource
13
{
14
    /** @var string */
15
    private $endpointResourceType;
16
17
    /** @var \Spatie\ResourceLinks\Links */
18
    private $endpointsGroup;
19
20
    public static function create(Model $model = null, string $endpointResourceType = null): LinkResource
21
    {
22
        return new self($model, $endpointResourceType);
23
    }
24
25
    public function __construct(Model $model = null, string $endpointResourceType = null)
26
    {
27
        parent::__construct($model);
28
29
        $this->endpointResourceType = $endpointResourceType ?? LinkResourceType::ITEM;
30
        $this->endpointsGroup = new Links();
31
    }
32
33
    public function endpoint($endpoint, $parameters = null, $httpVerb = null): LinkResource
34
    {
35
        if ($endpoint instanceof Closure) {
36
            $endpoint($this->endpointsGroup);
37
38
            return $this;
39
        }
40
41
        if (is_array($endpoint)) {
42
            $this->endpointsGroup
43
                ->action($endpoint)
44
                ->httpVerb($httpVerb)
45
                ->parameters(Arr::wrap($parameters));
46
47
            return $this;
48
        }
49
50
        if (is_string($endpoint) && method_exists($endpoint, '__invoke')) {
51
            $this->endpointsGroup
52
                ->invokableController($endpoint)
53
                ->parameters(Arr::wrap($parameters));
54
55
            return $this;
56
        }
57
58
        if (is_string($endpoint)) {
59
            $this->endpointsGroup
60
                ->controller($endpoint)
61
                ->parameters(Arr::wrap($parameters));
62
63
            return $this;
64
        }
65
66
        return $this;
67
    }
68
69
    public function mergeCollectionEndpoints(): LinkResource
70
    {
71
        $this->endpointResourceType = LinkResourceType::MULTI;
72
73
        return $this;
74
    }
75
76
    public function toArray($request)
77
    {
78
        return $this->endpointsGroup
79
            ->getEndpointTypes()
80
            ->mapWithKeys(function (EndPointType $endpointType) use ($request) {
81
                $endpointType->parameters($request->route()->parameters());
82
83
                if ($endpointType instanceof ControllerEndpointType) {
84
                    return $this->resolveEndpointsFromControllerEndpointType($endpointType);
85
                }
86
87
                return $endpointType->getEndpoints($this->resource);
88
            });
89
    }
90
91
    private function resolveEndpointsFromControllerEndpointType(ControllerEndpointType $endpointType): array
92
    {
93
        if ($this->endpointResourceType === LinkResourceType::ITEM) {
94
            return $endpointType->getEndpoints($this->resource);
95
        }
96
97
        if ($this->endpointResourceType === LinkResourceType::COLLECTION) {
98
            return $endpointType->getCollectionEndpoints();
99
        }
100
101
        if ($this->endpointResourceType === LinkResourceType::MULTI) {
102
            return array_merge(
103
                $endpointType->getEndpoints($this->resource),
104
                $endpointType->getCollectionEndpoints()
105
            );
106
        }
107
108
        return [];
109
    }
110
}
111