1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apie\PrimaryKeyPlugin\Normalizers; |
4
|
|
|
|
5
|
|
|
use Apie\Core\ApiResourceMetadataFactory; |
6
|
|
|
use Apie\Core\ClassResourceConverter; |
7
|
|
|
use Apie\Core\IdentifierExtractor; |
8
|
|
|
use Apie\Core\PluginInterfaces\FrameworkConnectionInterface; |
9
|
|
|
use Apie\Core\Resources\ApiResourcesInterface; |
10
|
|
|
use Apie\CorePlugin\ResourceSerializers\SymfonySerializerAdapter; |
|
|
|
|
11
|
|
|
use Apie\PrimaryKeyPlugin\ValueObjects\PrimaryKeyReference; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
13
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
14
|
|
|
use Symfony\Component\Serializer\SerializerAwareTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* 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. |
18
|
|
|
*/ |
19
|
|
|
class ApiePrimaryKeyNormalizer implements ContextAwareNormalizerInterface, SerializerAwareInterface |
20
|
|
|
{ |
21
|
|
|
use SerializerAwareTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ApiResourcesInterface |
25
|
|
|
*/ |
26
|
|
|
private $apiResources; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var IdentifierExtractor |
30
|
|
|
*/ |
31
|
|
|
private $identifierExtractor; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ApiResourceMetadataFactory |
35
|
|
|
*/ |
36
|
|
|
private $metadataFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ClassResourceConverter |
40
|
|
|
*/ |
41
|
|
|
private $converter; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var FrameworkConnectionInterface |
45
|
|
|
*/ |
46
|
|
|
private $frameworkConnection; |
47
|
|
|
|
48
|
|
|
public function __construct( |
49
|
|
|
ApiResourcesInterface $apiResources, |
50
|
|
|
IdentifierExtractor $identifierExtractor, |
51
|
|
|
ApiResourceMetadataFactory $metadataFactory, |
52
|
|
|
ClassResourceConverter $converter, |
53
|
|
|
FrameworkConnectionInterface $frameworkConnection |
54
|
|
|
) { |
55
|
|
|
$this->apiResources = $apiResources; |
56
|
|
|
$this->identifierExtractor = $identifierExtractor; |
57
|
|
|
$this->metadataFactory = $metadataFactory; |
58
|
|
|
$this->converter = $converter; |
59
|
|
|
$this->frameworkConnection = $frameworkConnection; |
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function supportsNormalization($data, $format = null, array $context = []) |
65
|
|
|
{ |
66
|
|
|
if ($format === SymfonySerializerAdapter::INTERNAL_FOR_DATALAYER || empty($context['object_hierarchy']) || !empty($context['disable_pk_normalize'])) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
foreach ($this->apiResources->getApiResources() as $apiResource) { |
70
|
|
|
// if someone is really stupid to add this as an API resource.... |
71
|
|
|
if ($apiResource === PrimaryKeyReference::class) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
$resourceContext = $this->metadataFactory->getMetadata($apiResource)->getContext(); |
75
|
|
|
$identifier = $this->identifierExtractor->getIdentifierKeyOfClass($apiResource, $resourceContext); |
76
|
|
|
if (null !== $identifier && is_a($data, $apiResource)) { |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function normalize($object, $format = null, array $context = []) |
87
|
|
|
{ |
88
|
|
|
$metadata = $this->metadataFactory->getMetadata($object); |
89
|
|
|
$resourceContext = $this->metadataFactory->getMetadata($object)->getContext(); |
90
|
|
|
$identifierValue = $this->identifierExtractor->getIdentifierValue($object, $resourceContext); |
91
|
|
|
return $this->serializer->normalize( |
92
|
|
|
new PrimaryKeyReference( |
93
|
|
|
$metadata, |
94
|
|
|
$this->frameworkConnection->getUrlForResource($object), |
|
|
|
|
95
|
|
|
$identifierValue |
96
|
|
|
), |
97
|
|
|
$format, |
98
|
|
|
$context |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths