1
|
|
|
<?php |
2
|
|
|
namespace Kartenmacherei\RestFramework; |
3
|
|
|
|
4
|
|
|
use Kartenmacherei\RestFramework\Action\Action; |
5
|
|
|
use Kartenmacherei\RestFramework\Request\DeleteRequest; |
6
|
|
|
use Kartenmacherei\RestFramework\Request\GetRequest; |
7
|
|
|
use Kartenmacherei\RestFramework\Request\Method\DeleteRequestMethod; |
8
|
|
|
use Kartenmacherei\RestFramework\Request\Method\GetRequestMethod; |
9
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PatchRequestMethod; |
10
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PostRequestMethod; |
11
|
|
|
use Kartenmacherei\RestFramework\Request\Method\PutRequestMethod; |
12
|
|
|
use Kartenmacherei\RestFramework\Request\Method\UnsupportedRequestMethodException; |
13
|
|
|
use Kartenmacherei\RestFramework\Request\PatchRequest; |
14
|
|
|
use Kartenmacherei\RestFramework\Request\PostRequest; |
15
|
|
|
use Kartenmacherei\RestFramework\Request\PutRequest; |
16
|
|
|
use Kartenmacherei\RestFramework\Request\Request; |
17
|
|
|
use Kartenmacherei\RestFramework\RestResource\RestResource; |
18
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsDeleteRequests; |
19
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsGetRequests; |
20
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsPatchRequests; |
21
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsPostRequests; |
22
|
|
|
use Kartenmacherei\RestFramework\RestResource\SupportsPutRequests; |
23
|
|
|
|
24
|
|
|
class ActionMapper |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param Request $request |
28
|
|
|
* @param RestResource $resource |
29
|
|
|
* @return Action |
30
|
|
|
* @throws UnsupportedRequestMethodException |
31
|
|
|
*/ |
32
|
|
|
public function getAction(Request $request, RestResource $resource): Action |
33
|
|
|
{ |
34
|
|
|
if (!$resource->supports($request->getMethod())) { |
35
|
|
|
throw new UnsupportedRequestMethodException(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @var RestResource|SupportsDeleteRequests|SupportsGetRequests|SupportsPatchRequests|SupportsPostRequests|SupportsPutRequests $resource */ |
39
|
|
|
switch ($request->getMethod()) { |
40
|
|
|
case new DeleteRequestMethod(): |
41
|
|
|
/** @var DeleteRequest $request */ |
42
|
|
|
return $resource->getDeleteCommand($request); |
|
|
|
|
43
|
|
|
case new GetRequestMethod(): |
44
|
|
|
/** @var GetRequest $request */ |
45
|
|
|
return $resource->getQuery($request); |
|
|
|
|
46
|
|
|
case new PatchRequestMethod(): |
47
|
|
|
/** @var PatchRequest $request */ |
48
|
|
|
return $resource->getPatchCommand($request); |
|
|
|
|
49
|
|
|
case new PostRequestMethod(): |
50
|
|
|
/** @var PostRequest $request */ |
51
|
|
|
return $resource->getPostCommand($request); |
|
|
|
|
52
|
|
|
case new PutRequestMethod(): |
53
|
|
|
/** @var PutRequest $request */ |
54
|
|
|
return $resource->getPutCommand($request); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
throw new UnsupportedRequestMethodException(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
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: