|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\LaravelResourceEndpoints; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Spatie\LaravelResourceEndpoints\EndpointTypes\ControllerEndpointType; |
|
8
|
|
|
use Spatie\LaravelResourceEndpoints\EndpointTypes\EndpointType; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
10
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
|
11
|
|
|
|
|
12
|
|
|
class EndpointResource extends JsonResource |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $endpointResourceType; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Spatie\LaravelResourceEndpoints\EndpointsGroup */ |
|
18
|
|
|
private $endpointsGroup; |
|
19
|
|
|
|
|
20
|
|
|
public static function create(Model $model = null, string $endpointResourceType = null): EndpointResource |
|
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 ?? EndpointResourceType::ITEM; |
|
30
|
|
|
$this->endpointsGroup = new EndpointsGroup(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function endpoint($endpoint, $parameters = null, $httpVerb = null): EndpointResource |
|
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(): EndpointResource |
|
70
|
|
|
{ |
|
71
|
|
|
$this->endpointResourceType = EndpointResourceType::MULTI; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function toArray($request) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->endpointsGroup |
|
79
|
|
|
->getEndpointTypes() |
|
80
|
|
|
->map(function (EndpointType $endpointType) use ($request) { |
|
81
|
|
|
return $endpointType->hasParameters() === false |
|
82
|
|
|
? $endpointType->parameters($request->route()->parameters()) |
|
83
|
|
|
: $endpointType; |
|
84
|
|
|
}) |
|
85
|
|
|
->mapWithKeys(function (EndPointType $endpointType) { |
|
86
|
|
|
if ($endpointType instanceof ControllerEndpointType) { |
|
87
|
|
|
return $this->resolveEndpointsFromControllerEndpointType($endpointType); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $endpointType->getEndpoints($this->resource); |
|
91
|
|
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function resolveEndpointsFromControllerEndpointType(ControllerEndpointType $endpointType): array |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->endpointResourceType === EndpointResourceType::ITEM) { |
|
97
|
|
|
return $endpointType->getEndpoints($this->resource); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($this->endpointResourceType === EndpointResourceType::COLLECTION) { |
|
101
|
|
|
return $endpointType->getCollectionEndpoints(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($this->endpointResourceType === EndpointResourceType::MULTI) { |
|
105
|
|
|
return array_merge( |
|
106
|
|
|
$endpointType->getEndpoints($this->resource), |
|
107
|
|
|
$endpointType->getCollectionEndpoints() |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return []; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|