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