|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Bundle\JsonApiBundle\EventListener; |
|
5
|
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\AbstractDocument; |
|
7
|
|
|
use Mikemirten\Component\JsonApi\Document\ErrorObject; |
|
8
|
|
|
use Mikemirten\Component\JsonApi\Document\NoDataDocument; |
|
9
|
|
|
use Mikemirten\Component\JsonApi\Document\ResourceObject; |
|
10
|
|
|
use Mikemirten\Component\JsonApi\Document\SingleResourceDocument; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* JsonApi view listener |
|
16
|
|
|
* Handles a JsonApi-document or its part responded by controller |
|
17
|
|
|
* |
|
18
|
|
|
* @package Mikemirten\Bundle\JsonApiBundle\EventListener |
|
19
|
|
|
*/ |
|
20
|
|
|
class JsonApiViewListener |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* On Kernel View event handler |
|
24
|
|
|
* |
|
25
|
|
|
* @param GetResponseForControllerResultEvent $event |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function onKernelView(GetResponseForControllerResultEvent $event) |
|
28
|
|
|
{ |
|
29
|
4 |
|
$result = $event->getControllerResult(); |
|
30
|
|
|
|
|
31
|
4 |
|
if ($result instanceof AbstractDocument) { |
|
32
|
1 |
|
$event->setResponse($this->createResponse($result)); |
|
33
|
1 |
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
if ($result instanceof ResourceObject) { |
|
37
|
1 |
|
$event->setResponse($this->handleResource($result)); |
|
38
|
1 |
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
if ($result instanceof ErrorObject) { |
|
42
|
1 |
|
$event->setResponse($this->handleError($result)); |
|
43
|
|
|
} |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Handle single resource object |
|
48
|
|
|
* |
|
49
|
|
|
* @param ResourceObject $resource |
|
50
|
|
|
* @return Response |
|
51
|
|
|
*/ |
|
52
|
1 |
|
protected function handleResource(ResourceObject $resource): Response |
|
53
|
|
|
{ |
|
54
|
1 |
|
$document = new SingleResourceDocument($resource); |
|
55
|
|
|
|
|
56
|
1 |
|
return $this->createResponse($document); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Handle error |
|
61
|
|
|
* |
|
62
|
|
|
* @param ErrorObject $error |
|
63
|
|
|
* @return Response |
|
64
|
|
|
*/ |
|
65
|
1 |
|
protected function handleError(ErrorObject $error): Response |
|
66
|
|
|
{ |
|
67
|
1 |
|
$document = new NoDataDocument(); |
|
68
|
1 |
|
$document->addError($error); |
|
69
|
|
|
|
|
70
|
1 |
|
return $this->createResponse($document); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Create response |
|
75
|
|
|
* |
|
76
|
|
|
* @param AbstractDocument $document |
|
77
|
|
|
* @return Response |
|
78
|
|
|
*/ |
|
79
|
3 |
|
protected function createResponse(AbstractDocument $document): Response |
|
80
|
|
|
{ |
|
81
|
3 |
|
$encoded = $this->encode($document->toArray()); |
|
82
|
3 |
|
$response = new Response($encoded); |
|
83
|
|
|
|
|
84
|
3 |
|
$response->headers->set('Content-Type', 'application/vnd.api+json'); |
|
85
|
|
|
|
|
86
|
3 |
|
return $response; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Encode object into a json-string |
|
91
|
|
|
* |
|
92
|
|
|
* @param mixed $object |
|
93
|
|
|
* @return string |
|
94
|
|
|
* @throws \LogicException |
|
95
|
|
|
*/ |
|
96
|
3 |
|
protected function encode($object): string |
|
97
|
|
|
{ |
|
98
|
3 |
|
$encoded = json_encode($object); |
|
99
|
|
|
|
|
100
|
3 |
|
if (json_last_error() === JSON_ERROR_NONE) { |
|
101
|
3 |
|
return $encoded; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
throw new \LogicException('Encoding error: ' . json_last_error_msg()); |
|
105
|
|
|
} |
|
106
|
|
|
} |