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\NormalizerAwareInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Daniel West <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class RouteNormalizer implements NormalizerInterface, NormalizerAwareInterface |
26
|
|
|
{ |
27
|
|
|
use NormalizerAwareTrait; |
28
|
|
|
|
29
|
|
|
private const ALREADY_CALLED = 'ROUTE_NORMALIZER_ALREADY_CALLED'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Route $object |
33
|
|
|
* @param mixed|null $format |
34
|
|
|
*/ |
35
|
|
|
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null |
36
|
|
|
{ |
37
|
|
|
$context[self::ALREADY_CALLED] = true; |
38
|
|
|
|
39
|
|
|
$finalRoute = $object; |
40
|
|
|
|
41
|
|
|
$redirectedRoutes = [$finalRoute->getId()]; |
42
|
|
|
while ($nextRedirect = $finalRoute->getRedirect()) { |
43
|
|
|
if (\in_array($nextRedirect->getId(), $redirectedRoutes, true)) { |
44
|
|
|
throw new CircularReferenceException(sprintf('The redirect routes result in a circular reference: %s', implode(' -> ', $redirectedRoutes))); |
45
|
|
|
} |
46
|
|
|
$redirectedRoutes[] = $nextRedirect->getId(); |
47
|
|
|
$finalRoute = $nextRedirect; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$isRedirect = $finalRoute !== $object; |
51
|
|
|
if ($isRedirect) { |
52
|
|
|
$object->setPage($finalRoute->getPage()); |
53
|
|
|
$object->setPageData($finalRoute->getPageData()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$normalized = $this->normalizer->normalize($object, $format, $context); |
57
|
|
|
|
58
|
|
|
if ($isRedirect) { |
59
|
|
|
$normalized['redirectPath'] = $finalRoute->getPath(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$operationName = $context['operation_name'] ?? null; |
63
|
|
|
if ('_api_/routes_manifest/{id}{._format}_get' === $operationName) { |
64
|
|
|
return [ |
65
|
|
|
'resource_iris' => $this->getResourceIrisFromArray($normalized), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $normalized; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getResourceIrisFromArray(array $resource, array $iris = []): array |
73
|
|
|
{ |
74
|
|
|
$resourceId = $resource['@id'] ?? null; |
75
|
|
|
if ( |
76
|
|
|
$resourceId |
77
|
|
|
&& !str_starts_with($resource['@id'] ?? null, '/.well-known/') |
|
|
|
|
78
|
|
|
&& '/_/resource_metadatas' !== $resourceId |
79
|
|
|
&& !\in_array($resourceId, $iris, true) |
80
|
|
|
) { |
81
|
|
|
$iris[] = $resourceId; |
82
|
|
|
} |
83
|
|
|
foreach ($resource as $resourceKey => $resourceValue) { |
84
|
|
|
// may be a string or simple |
85
|
|
|
// may be an array representing a resource |
86
|
|
|
// may be an array of any other values |
87
|
|
|
// may be an array of arrays |
88
|
|
|
if (\is_array($resourceValue)) { |
89
|
|
|
// check if the array is representing a new resource |
90
|
|
|
if (isset($resourceValue['@id'])) { |
91
|
|
|
$iris = $this->getResourceIrisFromArray($resourceValue, $iris); |
92
|
|
|
} |
93
|
|
|
// check if the array contains more resources |
94
|
|
|
foreach ($resourceValue as $nestedValue) { |
95
|
|
|
if (isset($nestedValue['@id'])) { |
96
|
|
|
$iris = $this->getResourceIrisFromArray($nestedValue, $iris); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $iris; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function supportsNormalization($data, $format = null, $context = []): bool |
106
|
|
|
{ |
107
|
|
|
return !isset($context[self::ALREADY_CALLED]) && $data instanceof Route; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getSupportedTypes(?string $format): array |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
return [Route::class => false]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|