1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\Core; |
4
|
|
|
|
5
|
|
|
use W2w\Lib\Apie\Exceptions\InvalidClassTypeException; |
6
|
|
|
use W2w\Lib\Apie\Exceptions\InvalidReturnTypeOfApiResourceException; |
7
|
|
|
use W2w\Lib\Apie\Exceptions\MethodNotAllowedException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class that does the persist action by reading the metadata of an Api resource. |
11
|
|
|
*/ |
12
|
|
|
class ApiResourcePersister |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ApiResourceMetadataFactory |
16
|
|
|
*/ |
17
|
|
|
private $factory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param ApiResourceMetadataFactory $factory |
21
|
|
|
*/ |
22
|
|
|
public function __construct(ApiResourceMetadataFactory $factory) |
23
|
|
|
{ |
24
|
|
|
$this->factory = $factory; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Persist a new resource. |
29
|
|
|
* |
30
|
|
|
* @param mixed $resource |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function persistNew($resource) |
34
|
|
|
{ |
35
|
|
|
$resourceClass = get_class($resource); |
36
|
|
|
$metadata = $this->factory->getMetadata($resourceClass); |
37
|
|
|
if (!$metadata->allowPost()) { |
38
|
|
|
throw new MethodNotAllowedException('post'); |
39
|
|
|
} |
40
|
|
|
$result = $metadata->getResourcePersister() |
41
|
|
|
->persistNew($resource, $metadata->getContext()); |
42
|
|
|
if (!$result instanceof $resourceClass) { |
43
|
|
|
throw new InvalidReturnTypeOfApiResourceException($metadata->getResourcePersister(), $this->getType($result), $resourceClass); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Persist an existing resource. |
51
|
|
|
* |
52
|
|
|
* @param mixed $resource |
53
|
|
|
* @param string|int $id |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function persistExisting($resource, $id) |
57
|
|
|
{ |
58
|
|
|
$resourceClass = get_class($resource); |
59
|
|
|
$metadata = $this->factory->getMetadata($resourceClass); |
60
|
|
|
if (!$metadata->allowPut()) { |
61
|
|
|
throw new MethodNotAllowedException('put'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$result = $metadata->getResourcePersister() |
65
|
|
|
->persistExisting($resource, $id, $metadata->getContext()); |
66
|
|
|
if (!$result instanceof $resourceClass) { |
67
|
|
|
throw new InvalidReturnTypeOfApiResourceException($metadata->getResourcePersister(), $this->getType($result), $resourceClass); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Removes an existing resource. |
75
|
|
|
* |
76
|
|
|
* @param string $resourceClass |
77
|
|
|
* @param string|int $id |
78
|
|
|
*/ |
79
|
|
|
public function delete(string $resourceClass, $id) |
80
|
|
|
{ |
81
|
|
|
$metadata = $this->factory->getMetadata($resourceClass); |
82
|
|
|
if (!$metadata->allowDelete()) { |
83
|
|
|
throw new MethodNotAllowedException('delete'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$metadata->getResourcePersister()->remove($resourceClass, $id, $metadata->getContext()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns a type display of an object instance. |
91
|
|
|
* |
92
|
|
|
* @param mixed $object |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
private function getType($object) |
96
|
|
|
{ |
97
|
|
|
if (is_object($object)) { |
98
|
|
|
return get_class($object); |
99
|
|
|
} |
100
|
|
|
if (is_string($object)) { |
101
|
|
|
return 'string ' . json_encode($object); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return gettype($object); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|