1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\Core\Models; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use W2w\Lib\Apie\Apie; |
7
|
|
|
use W2w\Lib\Apie\Core\SearchFilters\SearchFilterRequest; |
8
|
|
|
use W2w\Lib\Apie\Events\NormalizeEvent; |
9
|
|
|
use W2w\Lib\Apie\Events\ResponseEvent; |
10
|
|
|
use W2w\Lib\Apie\Interfaces\ResourceSerializerInterface; |
11
|
|
|
use W2w\Lib\Apie\PluginInterfaces\ResourceLifeCycleInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Data class returned by ApiResourceFacade. |
15
|
|
|
*/ |
16
|
|
|
class ApiResourceFacadeResponse |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ResourceSerializerInterface |
20
|
|
|
*/ |
21
|
|
|
private $serializer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var mixed |
25
|
|
|
*/ |
26
|
|
|
private $resource; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $acceptHeader; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var iterable<ResourceLifeCycleInterface> |
35
|
|
|
*/ |
36
|
|
|
private $resourceLifeCycles; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ResourceSerializerInterface $serializer |
40
|
|
|
* @param mixed $resource |
41
|
|
|
* @param string|null $acceptHeader |
42
|
|
|
* @param ResourceLifeCycleInterface[] |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
ResourceSerializerInterface $serializer, |
46
|
|
|
$resource, |
47
|
|
|
?string $acceptHeader, |
48
|
|
|
iterable $resourceLifeCycles = [] |
49
|
|
|
) { |
50
|
|
|
$this->serializer = $serializer; |
51
|
|
|
$this->resource = $resource; |
52
|
|
|
$this->acceptHeader = $acceptHeader; |
53
|
|
|
$this->resourceLifeCycles = $resourceLifeCycles; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Helper method to call the method on all all lifecycle instances. |
58
|
|
|
* |
59
|
|
|
* @param string $event |
60
|
|
|
* @param mixed[] $args |
61
|
|
|
*/ |
62
|
|
|
protected function runLifeCycleEvent(string $event, ...$args) |
63
|
|
|
{ |
64
|
|
|
foreach ($this->resourceLifeCycles as $resourceLifeCycle) { |
65
|
|
|
$resourceLifeCycle->$event(...$args); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function getResource() |
73
|
|
|
{ |
74
|
|
|
return $this->resource; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return ResponseInterface |
79
|
|
|
*/ |
80
|
|
|
public function getResponse(): ResponseInterface |
81
|
|
|
{ |
82
|
|
|
$event = new ResponseEvent($this->resource, $this->acceptHeader ?? 'application/json'); |
83
|
|
|
$this->runLifeCycleEvent('onPreCreateResponse', $event); |
84
|
|
|
if (!$event->getResponse()) { |
85
|
|
|
$event->setResponse( |
86
|
|
|
$this->serializer->toResponse($this->resource, $this->getAcceptHeader()) |
87
|
|
|
->withHeader('x-apie', Apie::VERSION) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
$this->runLifeCycleEvent('onPostCreateResponse', $event); |
91
|
|
|
|
92
|
|
|
return $event->getResponse(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets data the way we would send it normalized. |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function getNormalizedData() |
101
|
|
|
{ |
102
|
|
|
$event = new NormalizeEvent($this->resource, $this->acceptHeader ?? 'application/json'); |
103
|
|
|
$this->runLifeCycleEvent('onPreCreateNormalizedData', $event); |
104
|
|
|
if (!$event->hasNormalizedData()) { |
105
|
|
|
$event->setNormalizedData($this->serializer->normalize($this->resource, $this->getAcceptHeader())); |
106
|
|
|
} |
107
|
|
|
$this->runLifeCycleEvent('onPostCreateNormalizedData', $event); |
108
|
|
|
return $event->getNormalizedData(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the accept header. |
113
|
|
|
* |
114
|
|
|
* @return string|null |
115
|
|
|
*/ |
116
|
|
|
public function getAcceptHeader(): ?string |
117
|
|
|
{ |
118
|
|
|
return $this->acceptHeader ?? 'application/json'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the serializer. |
123
|
|
|
* |
124
|
|
|
* @return ResourceSerializerInterface |
125
|
|
|
*/ |
126
|
|
|
protected function getSerializer(): ResourceSerializerInterface |
127
|
|
|
{ |
128
|
|
|
return $this->serializer; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|