1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\Serializer; |
15
|
|
|
|
16
|
|
|
use Silverback\ApiComponentBundle\ApiComponentBundleEvents; |
17
|
|
|
use Silverback\ApiComponentBundle\Event\PreNormalizeEvent; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
19
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
20
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
21
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
22
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
23
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
24
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
25
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Daniel West <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class ApiNormalizer implements NormalizerAwareInterface, ContextAwareNormalizerInterface, CacheableSupportsMethodInterface |
31
|
|
|
{ |
32
|
|
|
use NormalizerAwareTrait; |
33
|
|
|
private const ALREADY_CALLED = 'API_COMPONENT_RESOURCE_NORMALIZER_ALREADY_CALLED'; |
34
|
|
|
|
35
|
|
|
private PropertyAccessor $propertyAccessor; |
36
|
|
|
private EventDispatcher $eventDispatcher; |
37
|
|
|
|
38
|
|
|
public function __construct(EventDispatcher $eventDispatcher) |
39
|
|
|
{ |
40
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
41
|
|
|
$this->eventDispatcher = $eventDispatcher; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function supportsNormalization($data, string $format = null, array $context = []): bool |
45
|
|
|
{ |
46
|
|
|
if (!isset($context[self::ALREADY_CALLED])) { |
47
|
|
|
$context[self::ALREADY_CALLED] = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return !(!\is_object($data) || |
51
|
|
|
\in_array($this->getResourceId($data), $context[self::ALREADY_CALLED], true)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function normalize($resource, string $format = null, array $context = []) |
55
|
|
|
{ |
56
|
|
|
$context[self::ALREADY_CALLED][] = $this->getResourceId($resource); |
57
|
|
|
|
58
|
|
|
$event = new PreNormalizeEvent($resource); |
59
|
|
|
$this->eventDispatcher->dispatch($event, ApiComponentBundleEvents::PRE_NORMALIZE); |
60
|
|
|
|
61
|
|
|
return $this->normalizer->normalize($event->getResource(), $format, $context); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function hasCacheableSupportsMethod(): bool |
65
|
|
|
{ |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getResourceId($object) |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
return \get_class($object) . '/' . $this->propertyAccessor->getValue($object, 'id'); |
73
|
|
|
} catch (NoSuchPropertyException $e) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|