1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\Core\Models; |
4
|
|
|
|
5
|
|
|
use W2w\Lib\Apie\Annotations\ApiResource; |
6
|
|
|
use W2w\Lib\Apie\Exceptions\InvalidReturnTypeOfApiResourceException; |
7
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourcePersisterInterface; |
8
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceRetrieverInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Metadata class of an ApiResource. This is a composite of |
12
|
|
|
* - a class name |
13
|
|
|
* - an ApiResource annotation |
14
|
|
|
* - a retriever class instance (optional) |
15
|
|
|
* - a persister class instance (optional) |
16
|
|
|
*/ |
17
|
|
|
class ApiResourceClassMetadata |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $className; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ApiResource |
26
|
|
|
*/ |
27
|
|
|
private $resource; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ApiResourceRetrieverInterface|null |
31
|
|
|
*/ |
32
|
|
|
private $resourceRetriever; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ApiResourcePersisterInterface|null |
36
|
|
|
*/ |
37
|
|
|
private $resourcePersister; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $className |
41
|
|
|
* @param ApiResource $resource |
42
|
|
|
* @param ApiResourceRetrieverInterface|null $resourceRetriever |
43
|
|
|
* @param ApiResourcePersisterInterface|null $resourcePersister |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
string $className, |
47
|
|
|
ApiResource $resource, |
48
|
|
|
?ApiResourceRetrieverInterface $resourceRetriever, |
49
|
|
|
?ApiResourcePersisterInterface $resourcePersister |
50
|
|
|
) { |
51
|
|
|
$this->className = $className; |
52
|
|
|
$this->resource = $resource; |
53
|
|
|
$this->resourceRetriever = $resourceRetriever; |
54
|
|
|
$this->resourcePersister = $resourcePersister; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns true if the Api resource has a retriever instance. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function hasResourceRetriever(): bool |
63
|
|
|
{ |
64
|
|
|
return !empty($this->resourceRetriever); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the retriever instance. |
69
|
|
|
* |
70
|
|
|
* @return ApiResourceRetrieverInterface |
71
|
|
|
*/ |
72
|
|
|
public function getResourceRetriever(): ApiResourceRetrieverInterface |
73
|
|
|
{ |
74
|
|
|
if (empty($this->resourceRetriever)) { |
75
|
|
|
throw new InvalidReturnTypeOfApiResourceException( |
76
|
|
|
null, |
77
|
|
|
'(null)', |
78
|
|
|
ApiResourceRetrieverInterface::class |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
return $this->resourceRetriever; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns true if the Api resource has a persister instance. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasResourcePersister(): bool |
90
|
|
|
{ |
91
|
|
|
return !empty($this->resourcePersister); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the persister instance. |
96
|
|
|
* |
97
|
|
|
* @return ApiResourcePersisterInterface |
98
|
|
|
*/ |
99
|
|
|
public function getResourcePersister(): ApiResourcePersisterInterface |
100
|
|
|
{ |
101
|
|
|
if (empty($this->resourcePersister)) { |
102
|
|
|
throw new InvalidReturnTypeOfApiResourceException( |
103
|
|
|
null, |
104
|
|
|
'(null)', |
105
|
|
|
ApiResourcePersisterInterface::class |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
return $this->resourcePersister; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the context metadata of the instance. This will be sent to the persister and retriever. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getContext(): array |
117
|
|
|
{ |
118
|
|
|
return $this->resource->context ?? []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns true if GET /resource/ is allowed. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function allowGetAll(): bool |
127
|
|
|
{ |
128
|
|
|
return !in_array('get', $this->resource->disabledMethods) && !in_array('get-all', $this->resource->disabledMethods); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns true if GET /resource/{id} is allowed. |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function allowGet(): bool |
137
|
|
|
{ |
138
|
|
|
return !in_array('get', $this->resource->disabledMethods) && $this->hasResourceRetriever(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns true if POST /resource/ is allowed. |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function allowPost(): bool |
147
|
|
|
{ |
148
|
|
|
return !in_array('post', $this->resource->disabledMethods) && $this->hasResourcePersister(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns true if DELETE /resource/{id} is allowed. |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function allowDelete(): bool |
157
|
|
|
{ |
158
|
|
|
return !in_array('delete', $this->resource->disabledMethods) && $this->hasResourceRetriever() && $this->hasResourcePersister(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns true if PUT /resource/{id} is allowed. |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function allowPut(): bool |
167
|
|
|
{ |
168
|
|
|
return !in_array('put', $this->resource->disabledMethods) && $this->allowGet() && $this->hasResourcePersister(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|