|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kartenmacherei\RestFramework\RestResource; |
|
3
|
|
|
|
|
4
|
|
|
use Kartenmacherei\RestFramework\Request\Method\DeleteRequestMethod; |
|
5
|
|
|
use Kartenmacherei\RestFramework\Request\Method\GetRequestMethod; |
|
6
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PatchRequestMethod; |
|
7
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PostRequestMethod; |
|
8
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PutRequestMethod; |
|
9
|
|
|
use Kartenmacherei\RestFramework\Request\Method\RequestMethod; |
|
10
|
|
|
use Kartenmacherei\RestFramework\Request\Pattern; |
|
11
|
|
|
use Kartenmacherei\RestFramework\Request\Uri; |
|
12
|
|
|
|
|
13
|
|
|
abstract class RestResource |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param Uri $uri |
|
17
|
|
|
* @return bool |
|
18
|
|
|
*/ |
|
19
|
|
|
public function isIdentifiedBy(Uri $uri): bool |
|
20
|
|
|
{ |
|
21
|
|
|
return $uri->matches($this->getUriPattern()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param RequestMethod $method |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public function supports(RequestMethod $method): bool |
|
29
|
|
|
{ |
|
30
|
|
|
return in_array($method, $this->getSupportedMethods()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return RequestMethod[] |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getSupportedMethods(): array |
|
37
|
|
|
{ |
|
38
|
|
|
$implementedInterfaces = class_implements($this); |
|
39
|
|
|
return array_values(array_intersect_key($this->getMethodMap(), $implementedInterfaces)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return Pattern |
|
44
|
|
|
*/ |
|
45
|
|
|
abstract protected function getUriPattern(): Pattern; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
private function getMethodMap(): array |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
SupportsDeleteRequests::class => new DeleteRequestMethod(), |
|
54
|
|
|
SupportsGetRequests::class => new GetRequestMethod(), |
|
55
|
|
|
SupportsPatchRequests::class => new PatchRequestMethod(), |
|
56
|
|
|
SupportsPostRequests::class => new PostRequestMethod(), |
|
57
|
|
|
SupportsPutRequests::class => new PutRequestMethod() |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|