|
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\Serializer\ResourceMetadata\ResourceMetadataProvider; |
|
17
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
|
18
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
19
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
|
20
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
|
21
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
|
22
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Daniel West <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class MetadataNormalizer implements NormalizerInterface, NormalizerAwareInterface |
|
28
|
|
|
{ |
|
29
|
|
|
use NormalizerAwareTrait; |
|
30
|
|
|
|
|
31
|
|
|
public const ALREADY_CALLED = 'METADATA_NORMALIZER_ALREADY_CALLED'; |
|
32
|
|
|
|
|
33
|
|
|
private PropertyAccessor $propertyAccessor; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(private string $metadataKey, private readonly ResourceMetadataProvider $resourceMetadataProvider) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function supportsNormalization($data, $format = null, array $context = []): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (!\is_object($data)) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
if (!$this->resourceMetadataProvider->resourceMetadataExists($data)) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
try { |
|
49
|
|
|
$id = $this->propertyAccessor->getValue($data, 'id'); |
|
50
|
|
|
} catch (NoSuchPropertyException $e) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
if (!isset($context[self::ALREADY_CALLED])) { |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return !\in_array($id, $context[self::ALREADY_CALLED], true); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null |
|
61
|
|
|
{ |
|
62
|
|
|
$context[self::ALREADY_CALLED][] = $this->propertyAccessor->getValue($object, 'id'); |
|
63
|
|
|
$metadataContext = $context; |
|
64
|
|
|
unset($metadataContext['operation'], $metadataContext['operation_name'], $metadataContext['resource_class']); |
|
65
|
|
|
if (isset($metadataContext['groups'])) { |
|
66
|
|
|
$metadataContext['groups'][] = 'cwa_resource:metadata'; |
|
67
|
|
|
} else { |
|
68
|
|
|
$metadataContext['groups'] = ['cwa_resource:metadata']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$resourceMetadata = $this->resourceMetadataProvider->findResourceMetadata($object); |
|
72
|
|
|
$metadataContext['resource_class'] = $resourceMetadata::class; |
|
73
|
|
|
|
|
74
|
|
|
$normalizedResourceMetadata = $this->normalizer->normalize($resourceMetadata, $format, $metadataContext); |
|
75
|
|
|
$data = $this->normalizer->normalize($object, $format, $context); |
|
76
|
|
|
|
|
77
|
|
|
$data[$this->metadataKey] = empty($normalizedResourceMetadata) ? null : $normalizedResourceMetadata; |
|
78
|
|
|
|
|
79
|
|
|
return $data; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the types potentially supported by this normalizer. |
|
84
|
|
|
* |
|
85
|
|
|
* For each supported formats (if applicable), the supported types should be |
|
86
|
|
|
* returned as keys, and each type should be mapped to a boolean indicating |
|
87
|
|
|
* if the result of supportsNormalization() can be cached or not |
|
88
|
|
|
* (a result cannot be cached when it depends on the context or on the data.) |
|
89
|
|
|
* A null value means that the normalizer does not support the corresponding |
|
90
|
|
|
* type. |
|
91
|
|
|
* |
|
92
|
|
|
* Use type "object" to match any classes or interfaces, |
|
93
|
|
|
* and type "*" to match any types. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getSupportedTypes(?string $format): array |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
return ['object' => false]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.