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\Utility\ApiResourceRouteFinder; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Daniel West <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class AbstractResourceNormalizer implements DenormalizerInterface, DenormalizerAwareInterface |
25
|
|
|
{ |
26
|
|
|
use DenormalizerAwareTrait; |
27
|
|
|
|
28
|
|
|
private const ALREADY_CALLED = 'ABSTRACT_COMPONENT_NORMALIZER_ALREADY_CALLED'; |
29
|
|
|
|
30
|
|
|
private ApiResourceRouteFinder $routeFinder; |
31
|
|
|
|
32
|
|
|
public function __construct(ApiResourceRouteFinder $routeFinder) |
33
|
|
|
{ |
34
|
|
|
$this->routeFinder = $routeFinder; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
|
|
return !isset($context[self::ALREADY_CALLED]) && (new \ReflectionClass($type))->isAbstract(); |
41
|
|
|
} catch (\ReflectionException $exception) { |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function denormalize($data, $type, $format = null, array $context = []): mixed |
47
|
|
|
{ |
48
|
|
|
$context[self::ALREADY_CALLED] = true; |
49
|
|
|
|
50
|
|
|
if (\is_string($data)) { |
51
|
|
|
$iri = $data; |
52
|
|
|
} else { |
53
|
|
|
$iri = $data['@id'] ?? null; |
54
|
|
|
} |
55
|
|
|
if ($iri) { |
56
|
|
|
$routeParameters = $this->routeFinder->findByIri($iri); |
57
|
|
|
$type = $routeParameters['_api_resource_class']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->denormalizer->denormalize($data, $type, $format, $context); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getSupportedTypes(?string $format): array |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
return ['object' => false]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.