1 | <?php |
||
31 | class JsonApiViewListener |
||
32 | { |
||
33 | /** |
||
34 | * Link-repositories provider |
||
35 | * |
||
36 | * @var RepositoryProvider |
||
37 | */ |
||
38 | protected $linkRepositoryProvider; |
||
39 | |||
40 | /** |
||
41 | * Object-handlers |
||
42 | * |
||
43 | * @var ObjectHandlerInterface[] |
||
44 | */ |
||
45 | protected $objectHandlers = []; |
||
46 | |||
47 | /** |
||
48 | * Object-handlers resolved by supported class |
||
49 | * |
||
50 | * @var ObjectHandlerInterface[] |
||
51 | */ |
||
52 | protected $resolvedObjectHandlers = []; |
||
53 | |||
54 | /** |
||
55 | * JsonApiViewListener constructor. |
||
56 | * |
||
57 | * @param RepositoryProvider $linkRepositoryProvider |
||
58 | */ |
||
59 | 14 | public function __construct(RepositoryProvider $linkRepositoryProvider) |
|
63 | |||
64 | /** |
||
65 | * Add object-handler |
||
66 | * |
||
67 | * @param ObjectHandlerInterface $handler |
||
68 | */ |
||
69 | 3 | public function addObjectHandler(ObjectHandlerInterface $handler) |
|
73 | |||
74 | /** |
||
75 | * On Kernel View event handler |
||
76 | * |
||
77 | * @param GetResponseForControllerResultEvent $event |
||
78 | */ |
||
79 | 13 | public function onKernelView(GetResponseForControllerResultEvent $event) |
|
93 | |||
94 | /** |
||
95 | * Handle result |
||
96 | * |
||
97 | * @param mixed $result |
||
98 | * @return Response | null |
||
99 | */ |
||
100 | 12 | protected function handleResult($result) |
|
132 | |||
133 | /** |
||
134 | * Handle document Json API view |
||
135 | * |
||
136 | * @param JsonApiDocumentView $view |
||
137 | * @return Response |
||
138 | */ |
||
139 | 1 | protected function handleDocumentView(JsonApiDocumentView $view) |
|
147 | |||
148 | /** |
||
149 | * Handle single object Json API view |
||
150 | * |
||
151 | * @param JsonApiObjectView $view |
||
152 | * @return Response |
||
153 | */ |
||
154 | 5 | protected function handleObjectView(JsonApiObjectView $view): Response |
|
171 | |||
172 | /** |
||
173 | * Handle a callback after a resource-object has created. |
||
174 | * |
||
175 | * @param AbstractJsonApiView $view |
||
176 | * @param ResourceObject $resource |
||
177 | */ |
||
178 | 7 | protected function handleResourceCallback(AbstractJsonApiView $view, ResourceObject $resource) |
|
185 | |||
186 | /** |
||
187 | * Handle links of document |
||
188 | * |
||
189 | * @param AbstractJsonApiView $view |
||
190 | * @param AbstractDocument $document |
||
191 | */ |
||
192 | 7 | protected function handleDocumentLinks(AbstractJsonApiView $view, AbstractDocument $document) |
|
214 | |||
215 | /** |
||
216 | * Handle a callback after a document has created. |
||
217 | * |
||
218 | * @param AbstractJsonApiView $view |
||
219 | * @param AbstractDocument $document |
||
220 | */ |
||
221 | 7 | protected function handleDocumentCallback(AbstractJsonApiView $view, AbstractDocument $document) |
|
228 | |||
229 | /** |
||
230 | * Handle supposed to be included to document resources |
||
231 | * |
||
232 | * @param AbstractDocument $document |
||
233 | * @param IncludedObjectsAwareInterface $view |
||
234 | */ |
||
235 | 7 | protected function handleIncludedResources(AbstractDocument $document, IncludedObjectsAwareInterface $view) |
|
243 | |||
244 | /** |
||
245 | * Handle object-iterator |
||
246 | * |
||
247 | * @param JsonApiIteratorView $view |
||
248 | * @return Response |
||
249 | */ |
||
250 | 3 | protected function handleIteratorView(JsonApiIteratorView $view): Response |
|
272 | |||
273 | /** |
||
274 | * Handle response data besides of the document itself |
||
275 | * |
||
276 | * @param Response $response |
||
277 | * @param HttpAttributesAwareInterface $view |
||
278 | */ |
||
279 | 8 | protected function handleHttpAttributes(Response $response, HttpAttributesAwareInterface $view) |
|
284 | |||
285 | /** |
||
286 | * Handle object |
||
287 | * |
||
288 | * @param $object |
||
289 | * @return ResourceObject |
||
290 | */ |
||
291 | 8 | protected function handleObject($object): ResourceObject |
|
301 | |||
302 | /** |
||
303 | * Get handler supports given class |
||
304 | * |
||
305 | * @param string $class |
||
306 | * @return ObjectHandlerInterface |
||
307 | * @throws \LogicException |
||
308 | */ |
||
309 | 3 | protected function getHandler(string $class): ObjectHandlerInterface |
|
324 | |||
325 | /** |
||
326 | * Handle single resource object |
||
327 | * |
||
328 | * @param ResourceObject $resource |
||
329 | * @return Response |
||
330 | */ |
||
331 | 1 | protected function handleResource(ResourceObject $resource): Response |
|
338 | |||
339 | /** |
||
340 | * Handle error |
||
341 | * |
||
342 | * @param ErrorObject $error |
||
343 | * @return Response |
||
344 | */ |
||
345 | 1 | protected function handleError(ErrorObject $error): Response |
|
353 | |||
354 | /** |
||
355 | * Create response |
||
356 | * |
||
357 | * @param AbstractDocument $document |
||
358 | * @return Response |
||
359 | */ |
||
360 | 11 | protected function createResponse(AbstractDocument $document): Response |
|
369 | |||
370 | /** |
||
371 | * Encode object into a json-string |
||
372 | * |
||
373 | * @param mixed $object |
||
374 | * @return string |
||
375 | * @throws \LogicException |
||
376 | */ |
||
377 | 11 | protected function encode($object): string |
|
387 | } |