|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Serializer\Normalizer; |
|
15
|
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\Route; |
|
17
|
|
|
use Symfony\Component\Serializer\Exception\CircularReferenceException; |
|
18
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
|
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
|
20
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
|
21
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Daniel West <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class RouteNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface, NormalizerAwareInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use NormalizerAwareTrait; |
|
29
|
|
|
|
|
30
|
|
|
private const ALREADY_CALLED = 'ROUTE_NORMALIZER_ALREADY_CALLED'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Route $object |
|
34
|
|
|
* @param mixed|null $format |
|
35
|
|
|
*/ |
|
36
|
|
|
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null |
|
37
|
|
|
{ |
|
38
|
|
|
$context[self::ALREADY_CALLED] = true; |
|
39
|
|
|
|
|
40
|
|
|
$finalRoute = $object; |
|
41
|
|
|
|
|
42
|
|
|
$redirectedRoutes = [$finalRoute->getId()]; |
|
43
|
|
|
while ($nextRedirect = $finalRoute->getRedirect()) { |
|
44
|
|
|
if (\in_array($nextRedirect->getId(), $redirectedRoutes, true)) { |
|
45
|
|
|
throw new CircularReferenceException(sprintf('The redirect routes result in a circular reference: %s', implode(' -> ', $redirectedRoutes))); |
|
46
|
|
|
} |
|
47
|
|
|
$redirectedRoutes[] = $nextRedirect->getId(); |
|
48
|
|
|
$finalRoute = $nextRedirect; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$isRedirect = $finalRoute !== $object; |
|
52
|
|
|
if ($isRedirect) { |
|
53
|
|
|
$object->setPage($finalRoute->getPage()); |
|
54
|
|
|
$object->setPageData($finalRoute->getPageData()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$normalized = $this->normalizer->normalize($object, $format, $context); |
|
58
|
|
|
|
|
59
|
|
|
if ($isRedirect) { |
|
60
|
|
|
$normalized['redirectPath'] = $finalRoute->getPath(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$operationName = $context['operation_name'] ?? null; |
|
64
|
|
|
if ('_api_/routes_manifest/{id}.{_format}_get' === $operationName) { |
|
65
|
|
|
return [ |
|
66
|
|
|
'resource_iris' => $this->getResourceIrisFromArray($normalized), |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $normalized; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function getResourceIrisFromArray(array $resource): array |
|
74
|
|
|
{ |
|
75
|
|
|
$iris = []; |
|
76
|
|
|
if (isset($resource['@id'])) { |
|
77
|
|
|
$iris[] = $resource['@id']; |
|
78
|
|
|
} |
|
79
|
|
|
foreach ($resource as $resourceValue) { |
|
80
|
|
|
// may be a string or simple |
|
81
|
|
|
// may be an array representing a resource |
|
82
|
|
|
// may be an array of any other values |
|
83
|
|
|
// may be an array of arrays |
|
84
|
|
|
if (\is_array($resourceValue)) { |
|
85
|
|
|
// check if the array is representing a new resource |
|
86
|
|
|
if (isset($resourceValue['@id'])) { |
|
87
|
|
|
array_push($iris, ...$this->getResourceIrisFromArray($resourceValue)); |
|
88
|
|
|
} |
|
89
|
|
|
// check if the array contains more resources |
|
90
|
|
|
foreach ($resourceValue as $nestedValue) { |
|
91
|
|
|
if (isset($nestedValue['@id'])) { |
|
92
|
|
|
array_push($iris, ...$this->getResourceIrisFromArray($nestedValue)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $iris; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function supportsNormalization($data, $format = null, $context = []): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $data instanceof Route; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function hasCacheableSupportsMethod(): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|