1
|
|
|
<?php |
2
|
|
|
namespace Kartenmacherei\RestFramework; |
3
|
|
|
|
4
|
|
|
use Kartenmacherei\RestFramework\Action\Action; |
5
|
|
|
use Kartenmacherei\RestFramework\Request\Method\DeleteRequestMethod; |
6
|
|
|
use Kartenmacherei\RestFramework\Request\Method\GetRequestMethod; |
7
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PatchRequestMethod; |
8
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PostRequestMethod; |
9
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PutRequestMethod; |
10
|
|
|
use Kartenmacherei\RestFramework\Request\Method\RequestMethod; |
11
|
|
|
use Kartenmacherei\RestFramework\Request\Method\UnsupportedRequestMethodException; |
12
|
|
|
use Kartenmacherei\RestFramework\RestResource\RestResource; |
13
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsDeleteRequests; |
14
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsGetRequests; |
15
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsPatchRequests; |
16
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsPostRequests; |
17
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsPutRequests; |
18
|
|
|
|
19
|
|
|
class ActionMapper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param RequestMethod $requestMethod |
23
|
|
|
* @param RestResource $resource |
24
|
|
|
* @return Action |
25
|
|
|
* @throws UnsupportedRequestMethodException |
26
|
|
|
*/ |
27
|
|
|
public function getAction(RequestMethod $requestMethod, RestResource $resource): Action |
28
|
|
|
{ |
29
|
|
|
if (!$resource->supports($requestMethod)) { |
30
|
|
|
throw new UnsupportedRequestMethodException(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @var RestResource|SupportsDeleteRequests|SupportsGetRequests|SupportsPatchRequests|SupportsPostRequests|SupportsPutRequests $resource */ |
34
|
|
|
switch ($requestMethod) { |
35
|
|
|
case new DeleteRequestMethod(): |
36
|
|
|
return $resource->getDeleteCommand(); |
|
|
|
|
37
|
|
|
case new GetRequestMethod(): |
38
|
|
|
return $resource->getQuery(); |
|
|
|
|
39
|
|
|
case new PatchRequestMethod(): |
40
|
|
|
return $resource->getPatchCommand(); |
|
|
|
|
41
|
|
|
case new PostRequestMethod(): |
42
|
|
|
return $resource->getPostCommand(); |
|
|
|
|
43
|
|
|
case new PutRequestMethod(): |
44
|
|
|
return $resource->getPutCommand(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
throw new UnsupportedRequestMethodException(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: