1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\Core; |
4
|
|
|
|
5
|
|
|
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterRequest; |
6
|
|
|
use W2w\Lib\Apie\Exceptions\InvalidReturnTypeOfApiResourceException; |
7
|
|
|
use W2w\Lib\Apie\Exceptions\MethodNotAllowedException; |
8
|
|
|
use W2w\Lib\Apie\Interfaces\SearchFilterProviderInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class that does the action to retrieve an Api resource. |
12
|
|
|
*/ |
13
|
|
|
class ApiResourceRetriever |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ApiResourceMetadataFactory |
17
|
|
|
*/ |
18
|
|
|
private $factory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ApiResourceMetadataFactory $factory |
22
|
|
|
*/ |
23
|
|
|
public function __construct(ApiResourceMetadataFactory $factory) |
24
|
|
|
{ |
25
|
|
|
$this->factory = $factory; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $resourceClass |
30
|
|
|
* @param string|int $id |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function retrieve(string $resourceClass, $id) |
34
|
|
|
{ |
35
|
|
|
$metadata = $this->factory->getMetadata($resourceClass); |
36
|
|
|
if (!$metadata->allowGet()) { |
37
|
|
|
throw new MethodNotAllowedException('get $id'); |
38
|
|
|
} |
39
|
|
|
$result = $metadata->getResourceRetriever() |
40
|
|
|
->retrieve($resourceClass, $id, $metadata->getContext()); |
41
|
|
|
if (!$result instanceof $resourceClass) { |
42
|
|
|
throw new InvalidReturnTypeOfApiResourceException($metadata->getResourceRetriever(), $this->getType($result), $resourceClass); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $resourceClass |
50
|
|
|
* @param SearchFilterRequest|null $searchFilterRequest |
51
|
|
|
* @return iterable |
52
|
|
|
*/ |
53
|
|
|
public function retrieveAll(string $resourceClass, ?SearchFilterRequest $searchFilterRequest = null) |
54
|
|
|
{ |
55
|
|
|
if (!$searchFilterRequest) { |
56
|
|
|
$searchFilterRequest = new SearchFilterRequest(); |
57
|
|
|
} |
58
|
|
|
$metadata = $this->factory->getMetadata($resourceClass); |
59
|
|
|
if (!$metadata->allowGetAll()) { |
60
|
|
|
throw new MethodNotAllowedException('get all'); |
61
|
|
|
} |
62
|
|
|
if (!$metadata->hasResourceRetriever()) { |
63
|
|
|
// Many OpenAPI generators expect the get all call to be working at all times. |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
$retriever = $metadata->getResourceRetriever(); |
67
|
|
|
if ($retriever instanceof SearchFilterProviderInterface) { |
68
|
|
|
$searchFilterRequest = $searchFilterRequest->applySearchFilter($retriever->getSearchFilter($metadata)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$result = $retriever->retrieveAll($resourceClass, $metadata->getContext(), $searchFilterRequest); |
72
|
|
|
foreach ($result as $instance) { |
73
|
|
|
if (!$instance instanceof $resourceClass) { |
74
|
|
|
throw new InvalidReturnTypeOfApiResourceException($metadata->getResourceRetriever(), $this->getType($instance), $resourceClass); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns a type display of an object instance. |
83
|
|
|
* |
84
|
|
|
* @param mixed $object |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
private function getType($object) |
88
|
|
|
{ |
89
|
|
|
if (is_object($object)) { |
90
|
|
|
return get_class($object); |
91
|
|
|
} |
92
|
|
|
if (is_string($object)) { |
93
|
|
|
return 'string ' . json_encode($object); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return gettype($object); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|