1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\LaravelEndpointResources; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Spatie\LaravelEndpointResources\EndpointTypes\ActionEndpointType; |
7
|
|
|
use Spatie\LaravelEndpointResources\EndpointTypes\ControllerEndpointType; |
8
|
|
|
use Spatie\LaravelEndpointResources\EndpointTypes\EndpointType; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
|
13
|
|
|
class EndpointResource extends JsonResource |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
protected $endpointResourceType; |
17
|
|
|
|
18
|
|
|
/** @var \Illuminate\Support\Collection */ |
19
|
|
|
protected $endPointTypes; |
20
|
|
|
|
21
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
22
|
|
|
protected $model; |
23
|
|
|
|
24
|
|
|
public function __construct(Model $model = null, string $endpointResourceType = null) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($model); |
27
|
|
|
|
28
|
|
|
$this->endPointTypes = new Collection(); |
29
|
|
|
$this->model = $model; |
30
|
|
|
$this->endpointResourceType = $endpointResourceType ?? EndpointResourceType::LOCAL; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function addController(string $controller, $parameters = null): JsonResource |
34
|
|
|
{ |
35
|
|
|
$this->endPointTypes->push(new ControllerEndpointType( |
36
|
|
|
$controller, |
37
|
|
|
$this->resolveProvidedParameters($parameters) |
38
|
|
|
)); |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addAction(array $action, $parameters = null, string $httpVerb = null): JsonResource |
44
|
|
|
{ |
45
|
|
|
$this->endPointTypes->push(new ActionEndpointType( |
46
|
|
|
$action, |
47
|
|
|
$this->resolveProvidedParameters($parameters), |
48
|
|
|
$httpVerb, |
49
|
|
|
)); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function mergeCollectionEndpoints() : JsonResource |
55
|
|
|
{ |
56
|
|
|
$this->endpointResourceType = EndpointResourceType::MULTI; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function toArray($request) |
62
|
|
|
{ |
63
|
|
|
return $this->endPointTypes->mapWithKeys(function (EndPointType $endpointType) { |
64
|
|
|
if ($endpointType instanceof ControllerEndpointType) { |
65
|
|
|
return $this->resolveControllerEndpoints($endpointType); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $endpointType->getEndpoints($this->model); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function resolveProvidedParameters($parameters = null) |
73
|
|
|
{ |
74
|
|
|
$parameters = Arr::wrap($parameters); |
75
|
|
|
|
76
|
|
|
return count($parameters) === 0 |
77
|
|
|
? request()->route()->parameters() |
78
|
|
|
: $parameters; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function resolveControllerEndpoints(ControllerEndpointType $endpointType): array |
82
|
|
|
{ |
83
|
|
|
if ($this->endpointResourceType === EndpointResourceType::LOCAL) { |
84
|
|
|
return $endpointType->getEndpoints($this->model); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($this->endpointResourceType === EndpointResourceType::GLOBAL) { |
88
|
|
|
return $endpointType->getCollectionEndpoints(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->endpointResourceType === EndpointResourceType::MULTI) { |
92
|
|
|
return array_merge( |
93
|
|
|
$endpointType->getEndpoints($this->model), |
94
|
|
|
$endpointType->getCollectionEndpoints() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return []; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|