1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace W2w\Lib\Apie\Plugins\PrimaryKey\Normalizers; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
6
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
7
|
|
|
use Symfony\Component\Serializer\SerializerAwareTrait; |
8
|
|
|
use W2w\Lib\Apie\Core\ApiResourceMetadataFactory; |
9
|
|
|
use W2w\Lib\Apie\Core\ClassResourceConverter; |
10
|
|
|
use W2w\Lib\Apie\Core\IdentifierExtractor; |
11
|
|
|
use W2w\Lib\Apie\Core\Resources\ApiResourcesInterface; |
12
|
|
|
use W2w\Lib\Apie\Plugins\PrimaryKey\ValueObjects\PrimaryKeyReference; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* If a resource has an other api resource as a child, this class will map it as a url and not try to hydrate it as an url. |
16
|
|
|
*/ |
17
|
|
|
class ApiePrimaryKeyNormalizer implements ContextAwareNormalizerInterface, SerializerAwareInterface |
18
|
|
|
{ |
19
|
|
|
use SerializerAwareTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ApiResourcesInterface |
23
|
|
|
*/ |
24
|
|
|
private $apiResources; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var IdentifierExtractor |
28
|
|
|
*/ |
29
|
|
|
private $identifierExtractor; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ApiResourceMetadataFactory |
33
|
|
|
*/ |
34
|
|
|
private $metadataFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ClassResourceConverter |
38
|
|
|
*/ |
39
|
|
|
private $converter; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $baseUrl; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
ApiResourcesInterface $apiResources, |
48
|
|
|
IdentifierExtractor $identifierExtractor, |
49
|
|
|
ApiResourceMetadataFactory $metadataFactory, |
50
|
|
|
ClassResourceConverter $converter, |
51
|
|
|
string $baseUrl |
52
|
|
|
) { |
53
|
|
|
$this->apiResources = $apiResources; |
54
|
|
|
$this->identifierExtractor = $identifierExtractor; |
55
|
|
|
$this->metadataFactory = $metadataFactory; |
56
|
|
|
$this->converter = $converter; |
57
|
|
|
$this->baseUrl = rtrim($baseUrl, '/') . '/'; |
58
|
|
|
} |
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function supportsNormalization($data, string $format = null, array $context = []) |
63
|
|
|
{ |
64
|
|
|
if (empty($context['object_hierarchy']) || !empty($context['disable_pk_normalize'])) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
foreach ($this->apiResources->getApiResources() as $apiResource) { |
68
|
|
|
// if someone is really stupid to add this as an API resource.... |
69
|
|
|
if ($apiResource === PrimaryKeyReference::class) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
$resourceContext = $this->metadataFactory->getMetadata($apiResource)->getContext(); |
73
|
|
|
$identifier = $this->identifierExtractor->getIdentifierKeyOfClass($apiResource, $resourceContext); |
74
|
|
|
if (null !== $identifier && is_a($data, $apiResource)) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function normalize($object, string $format = null, array $context = []) |
85
|
|
|
{ |
86
|
|
|
$metadata = $this->metadataFactory->getMetadata($object); |
87
|
|
|
$resourceContext = $this->metadataFactory->getMetadata($object)->getContext(); |
88
|
|
|
$identifierValue = $this->identifierExtractor->getIdentifierValue($object, $resourceContext); |
89
|
|
|
return $this->serializer->normalize( |
90
|
|
|
new PrimaryKeyReference( |
91
|
|
|
$metadata, |
92
|
|
|
$this->baseUrl . $this->converter->normalize($metadata->getClassName()), |
93
|
|
|
$identifierValue |
94
|
|
|
), |
95
|
|
|
$format, |
96
|
|
|
$context |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|