Commit fdaea85856a6a9c288b5df964a10800abaa2acaf
1 parent
3be26990
extended classes
Showing
1 changed file
with
386 additions
and
16 deletions
Show diff stats
app/library/App/Mvc/ExtendedApiEndpoint.php
1 | <?php | 1 | <?php |
2 | -/** | ||
3 | - * Created by PhpStorm. | ||
4 | - * User: Alex Savenko | ||
5 | - * Date: 14.02.2017 | ||
6 | - * Time: 16:15 | ||
7 | - */ | ||
8 | 2 | ||
9 | namespace App\Mvc; | 3 | namespace App\Mvc; |
10 | 4 | ||
5 | +use PhalconApi\Constants\HttpMethods; | ||
6 | +use PhalconApi\Constants\PostedDataMethods; | ||
7 | +use PhalconApi\Core; | ||
11 | 8 | ||
12 | -use PhalconRest\Api\ApiEndpoint; | 9 | +class ExtendedApiEndpoint |
10 | +{ | ||
11 | + const ALL = 'all'; | ||
12 | + const FIND = 'find'; | ||
13 | + const CREATE = 'create'; | ||
14 | + const UPDATE = 'update'; | ||
15 | + const REMOVE = 'remove'; | ||
13 | 16 | ||
14 | -class ExtendedApiEndpoint extends ApiEndpoint { | 17 | + protected $name; |
18 | + protected $description; | ||
15 | 19 | ||
16 | - protected $exampleRequest; | 20 | + protected $httpMethod; |
21 | + protected $path; | ||
22 | + protected $handlerMethod; | ||
23 | + | ||
24 | + protected $postedDataMethod = PostedDataMethods::AUTO; | ||
25 | + protected $exampleResponse; | ||
26 | + | ||
27 | + protected $allowedRoles = []; | ||
28 | + protected $deniedRoles = []; | ||
29 | + | ||
30 | + | ||
31 | + public function __construct($path, $httpMethod = HttpMethods::GET, $handlerMethod = null) | ||
32 | + { | ||
33 | + $this->path = $path; | ||
34 | + $this->httpMethod = $httpMethod; | ||
35 | + $this->handlerMethod = $handlerMethod; | ||
36 | + } | ||
37 | + | ||
38 | + /** | ||
39 | + * Returns pre-configured all endpoint | ||
40 | + * | ||
41 | + * @return static | ||
42 | + */ | ||
43 | + public static function all() | ||
44 | + { | ||
45 | + return self::factory('/', HttpMethods::GET, 'all') | ||
46 | + ->name(self::ALL) | ||
47 | + ->description('Returns all items'); | ||
48 | + } | ||
49 | + | ||
50 | + /** | ||
51 | + * @param string $description Description for the endpoint | ||
52 | + * | ||
53 | + * @return static | ||
54 | + */ | ||
55 | + public function description($description) | ||
56 | + { | ||
57 | + $this->description = $description; | ||
58 | + return $this; | ||
59 | + } | ||
60 | + | ||
61 | + /** | ||
62 | + * @param string $name Name for the endpoint | ||
63 | + * | ||
64 | + * @return static | ||
65 | + */ | ||
66 | + public function name($name) | ||
67 | + { | ||
68 | + $this->name = $name; | ||
69 | + return $this; | ||
70 | + } | ||
71 | + | ||
72 | + /** | ||
73 | + * Returns endpoint with default values | ||
74 | + * | ||
75 | + * @param string $path | ||
76 | + * @param string $httpMethod | ||
77 | + * @param string $handlerMethod | ||
78 | + * | ||
79 | + * @return static | ||
80 | + */ | ||
81 | + public static function factory($path, $httpMethod = HttpMethods::GET, $handlerMethod = null) | ||
82 | + { | ||
83 | + return new ExtendedApiEndpoint($path, $httpMethod, $handlerMethod); | ||
84 | + } | ||
85 | + | ||
86 | + /** | ||
87 | + * Returns pre-configured find endpoint | ||
88 | + * | ||
89 | + * @return static | ||
90 | + */ | ||
91 | + public static function find() | ||
92 | + { | ||
93 | + return self::factory('/{id}', HttpMethods::GET, 'find') | ||
94 | + ->name(self::FIND) | ||
95 | + ->description('Returns the item identified by {id}'); | ||
96 | + } | ||
97 | + | ||
98 | + /** | ||
99 | + * Returns pre-configured create endpoint | ||
100 | + * | ||
101 | + * @return static | ||
102 | + */ | ||
103 | + public static function create() | ||
104 | + { | ||
105 | + return self::factory('/', HttpMethods::POST, 'create') | ||
106 | + ->name(self::CREATE) | ||
107 | + ->description('Creates a new item using the posted data'); | ||
108 | + } | ||
109 | + | ||
110 | + /** | ||
111 | + * Returns pre-configured update endpoint | ||
112 | + * | ||
113 | + * @return static | ||
114 | + */ | ||
115 | + public static function update() | ||
116 | + { | ||
117 | + return self::factory('/{id}', HttpMethods::PUT, 'update') | ||
118 | + ->name(self::UPDATE) | ||
119 | + ->description('Updates an existing item identified by {id}, using the posted data'); | ||
120 | + } | ||
121 | + | ||
122 | + /** | ||
123 | + * Returns pre-configured remove endpoint | ||
124 | + * | ||
125 | + * @return static | ||
126 | + */ | ||
127 | + public static function remove() | ||
128 | + { | ||
129 | + return self::factory('/{id}', HttpMethods::DELETE, 'remove') | ||
130 | + ->name(self::REMOVE) | ||
131 | + ->description('Removes the item identified by {id}'); | ||
132 | + } | ||
133 | + | ||
134 | + /** | ||
135 | + * Returns pre-configured GET endpoint | ||
136 | + * | ||
137 | + * @param $path | ||
138 | + * @param string $handlerMethod | ||
139 | + * | ||
140 | + * @return ExtendedApiEndpoint | ||
141 | + */ | ||
142 | + public static function get($path, $handlerMethod = null) | ||
143 | + { | ||
144 | + return self::factory($path, HttpMethods::GET, $handlerMethod); | ||
145 | + } | ||
146 | + | ||
147 | + /** | ||
148 | + * Returns pre-configured POST endpoint | ||
149 | + * | ||
150 | + * @param $path | ||
151 | + * @param string $handlerMethod | ||
152 | + * | ||
153 | + * @return ExtendedApiEndpoint | ||
154 | + */ | ||
155 | + public static function post($path, $handlerMethod = null) | ||
156 | + { | ||
157 | + return self::factory($path, HttpMethods::POST, $handlerMethod); | ||
158 | + } | ||
159 | + | ||
160 | + /** | ||
161 | + * Returns pre-configured PUT endpoint | ||
162 | + * | ||
163 | + * @param $path | ||
164 | + * @param string $handlerMethod | ||
165 | + * | ||
166 | + * @return ExtendedApiEndpoint | ||
167 | + */ | ||
168 | + public static function put($path, $handlerMethod = null) | ||
169 | + { | ||
170 | + return self::factory($path, HttpMethods::PUT, $handlerMethod); | ||
171 | + } | ||
172 | + | ||
173 | + /** | ||
174 | + * Returns pre-configured DELETE endpoint | ||
175 | + * | ||
176 | + * @param $path | ||
177 | + * @param string $handlerMethod | ||
178 | + * | ||
179 | + * @return ExtendedApiEndpoint | ||
180 | + */ | ||
181 | + public static function delete($path, $handlerMethod = null) | ||
182 | + { | ||
183 | + return self::factory($path, HttpMethods::DELETE, $handlerMethod); | ||
184 | + } | ||
185 | + | ||
186 | + /** | ||
187 | + * Returns pre-configured HEAD endpoint | ||
188 | + * | ||
189 | + * @param $path | ||
190 | + * @param string $handlerMethod | ||
191 | + * | ||
192 | + * @return ExtendedApiEndpoint | ||
193 | + */ | ||
194 | + public static function head($path, $handlerMethod = null) | ||
195 | + { | ||
196 | + return self::factory($path, HttpMethods::HEAD, $handlerMethod); | ||
197 | + } | ||
198 | + | ||
199 | + /** | ||
200 | + * Returns pre-configured OPTIONS endpoint | ||
201 | + * | ||
202 | + * @param $path | ||
203 | + * @param string $handlerMethod | ||
204 | + * | ||
205 | + * @return ExtendedApiEndpoint | ||
206 | + */ | ||
207 | + public static function options($path, $handlerMethod = null) | ||
208 | + { | ||
209 | + return self::factory($path, HttpMethods::OPTIONS, $handlerMethod); | ||
210 | + } | ||
211 | + | ||
212 | + /** | ||
213 | + * Returns pre-configured PATCH endpoint | ||
214 | + * | ||
215 | + * @param $path | ||
216 | + * @param string $handlerMethod | ||
217 | + * | ||
218 | + * @return ExtendedApiEndpoint | ||
219 | + */ | ||
220 | + public static function patch($path, $handlerMethod = null) | ||
221 | + { | ||
222 | + return self::factory($path, HttpMethods::PATCH, $handlerMethod); | ||
223 | + } | ||
224 | + | ||
225 | + /** | ||
226 | + * @return string Unique identifier for this endpoint (returns a combination of the HTTP method and the path) | ||
227 | + */ | ||
228 | + public function getIdentifier() | ||
229 | + { | ||
230 | + return $this->getHttpMethod() . ' ' . $this->getPath(); | ||
231 | + } | ||
232 | + | ||
233 | + /** | ||
234 | + * @return string HTTP method of the endpoint | ||
235 | + */ | ||
236 | + public function getHttpMethod() | ||
237 | + { | ||
238 | + return $this->httpMethod; | ||
239 | + } | ||
240 | + | ||
241 | + /** | ||
242 | + * @return string Path of the endpoint, relative to the collection | ||
243 | + */ | ||
244 | + public function getPath() | ||
245 | + { | ||
246 | + return $this->path; | ||
247 | + } | ||
17 | 248 | ||
18 | /** | 249 | /** |
19 | - * @param string $exampleRequest Example of the request of the endpoint | 250 | + * @param string $handlerMethod Name of controller-method to be called for the endpoint |
251 | + * | ||
252 | + * @return static | ||
253 | + */ | ||
254 | + public function handlerMethod($handlerMethod) | ||
255 | + { | ||
256 | + $this->handlerMethod = $handlerMethod; | ||
257 | + return $this; | ||
258 | + } | ||
259 | + | ||
260 | + /** | ||
261 | + * @return string Name of controller-method to be called for the endpoint | ||
262 | + */ | ||
263 | + public function getHandlerMethod() | ||
264 | + { | ||
265 | + return $this->handlerMethod; | ||
266 | + } | ||
267 | + | ||
268 | + /** | ||
269 | + * @return string|null Name of the endpoint | ||
270 | + */ | ||
271 | + public function getName() | ||
272 | + { | ||
273 | + return $this->name; | ||
274 | + } | ||
275 | + | ||
276 | + /** | ||
277 | + * @return string Description for the endpoint | ||
278 | + */ | ||
279 | + public function getDescription() | ||
280 | + { | ||
281 | + return $this->description; | ||
282 | + } | ||
283 | + | ||
284 | + /** | ||
285 | + * @param string $exampleResponse Example of the response of the endpoint | ||
20 | * | 286 | * |
21 | * @return $this | 287 | * @return $this |
22 | */ | 288 | */ |
23 | - public function exampleRequest($exampleRequest) | 289 | + public function exampleResponse($exampleResponse) |
24 | { | 290 | { |
25 | - $this->exampleRequest = $exampleRequest; | 291 | + $this->exampleResponse = $exampleResponse; |
26 | return $this; | 292 | return $this; |
27 | } | 293 | } |
28 | 294 | ||
29 | /** | 295 | /** |
30 | - * @return string Example of the request of the endpoint | 296 | + * @return string Example of the response of the endpoint |
297 | + */ | ||
298 | + public function getExampleResponse() | ||
299 | + { | ||
300 | + return $this->exampleResponse; | ||
301 | + } | ||
302 | + | ||
303 | + /** | ||
304 | + * @return string $method One of the method constants defined in PostedDataMethods | ||
31 | */ | 305 | */ |
32 | - public function getExampleRequest() | 306 | + public function getPostedDataMethod() |
33 | { | 307 | { |
34 | - return $this->exampleRequest; | 308 | + return $this->postedDataMethod; |
35 | } | 309 | } |
36 | 310 | ||
37 | -} | ||
38 | \ No newline at end of file | 311 | \ No newline at end of file |
312 | + /** | ||
313 | + * Sets the posted data method to POST | ||
314 | + * | ||
315 | + * @return static | ||
316 | + */ | ||
317 | + public function expectsPostData() | ||
318 | + { | ||
319 | + $this->postedDataMethod(PostedDataMethods::POST); | ||
320 | + return $this; | ||
321 | + } | ||
322 | + | ||
323 | + /** | ||
324 | + * @param string $method One of the method constants defined in PostedDataMethods | ||
325 | + * | ||
326 | + * @return static | ||
327 | + */ | ||
328 | + public function postedDataMethod($method) | ||
329 | + { | ||
330 | + $this->postedDataMethod = $method; | ||
331 | + return $this; | ||
332 | + } | ||
333 | + | ||
334 | + /** | ||
335 | + * Sets the posted data method to JSON_BODY | ||
336 | + * | ||
337 | + * @return static | ||
338 | + */ | ||
339 | + public function expectsJsonData() | ||
340 | + { | ||
341 | + $this->postedDataMethod(PostedDataMethods::JSON_BODY); | ||
342 | + return $this; | ||
343 | + } | ||
344 | + | ||
345 | + /** | ||
346 | + * Allows access to this endpoint for role with the given names. | ||
347 | + * | ||
348 | + * @param ...array $roleNames Names of the roles to allow | ||
349 | + * | ||
350 | + * @return static | ||
351 | + */ | ||
352 | + public function allow() | ||
353 | + { | ||
354 | + $roleNames = func_get_args(); | ||
355 | + | ||
356 | + // Flatten array to allow array inputs | ||
357 | + $roleNames = Core::array_flatten($roleNames); | ||
358 | + | ||
359 | + foreach ($roleNames as $role) { | ||
360 | + | ||
361 | + if (!in_array($role, $this->allowedRoles)) { | ||
362 | + $this->allowedRoles[] = $role; | ||
363 | + } | ||
364 | + } | ||
365 | + | ||
366 | + return $this; | ||
367 | + } | ||
368 | + | ||
369 | + /** | ||
370 | + * @return string[] Array of allowed role-names | ||
371 | + */ | ||
372 | + public function getAllowedRoles() | ||
373 | + { | ||
374 | + return $this->allowedRoles; | ||
375 | + } | ||
376 | + | ||
377 | + /** | ||
378 | + * Denies access to this endpoint for role with the given names. | ||
379 | + * | ||
380 | + * @param ...array $roleNames Names of the roles to allow | ||
381 | + * | ||
382 | + * @return static | ||
383 | + */ | ||
384 | + public function deny() | ||
385 | + { | ||
386 | + $roleNames = func_get_args(); | ||
387 | + | ||
388 | + // Flatten array to allow array inputs | ||
389 | + $roleNames = Core::array_flatten($roleNames); | ||
390 | + | ||
391 | + foreach ($roleNames as $role) { | ||
392 | + | ||
393 | + if (!in_array($role, $this->deniedRoles)) { | ||
394 | + $this->deniedRoles[] = $role; | ||
395 | + } | ||
396 | + } | ||
397 | + | ||
398 | + return $this; | ||
399 | + } | ||
400 | + | ||
401 | + /** | ||
402 | + * @return string[] Array of denied role-names | ||
403 | + */ | ||
404 | + public function getDeniedRoles() | ||
405 | + { | ||
406 | + return $this->deniedRoles; | ||
407 | + } | ||
408 | +} |