1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the FiveLab Resource package |
7
|
|
|
* |
8
|
|
|
* (c) FiveLab |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FiveLab\Component\Resource\Serializer; |
15
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\ResourceInterface; |
17
|
|
|
use FiveLab\Component\Resource\Serializer\Events\AfterDenormalizationEvent; |
18
|
|
|
use FiveLab\Component\Resource\Serializer\Events\AfterNormalizationEvent; |
19
|
|
|
use FiveLab\Component\Resource\Serializer\Events\BeforeDenormalizationEvent; |
20
|
|
|
use FiveLab\Component\Resource\Serializer\Events\BeforeNormalizationEvent; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\Serializer\Serializer as SymfonySerializer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Override the default serializer for add ability for adding eventable and hook system. |
26
|
|
|
* Note: we use setter for injection for not affect original constructor. |
27
|
|
|
* |
28
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Serializer extends SymfonySerializer |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var EventDispatcherInterface|null |
34
|
|
|
*/ |
35
|
|
|
private ?EventDispatcherInterface $eventDispatcher = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set event dispatcher |
39
|
|
|
* |
40
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
41
|
|
|
*/ |
42
|
|
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
43
|
|
|
{ |
44
|
|
|
$this->eventDispatcher = $eventDispatcher; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
8 |
|
* {@inheritdoc} |
49
|
|
|
* |
50
|
8 |
|
* @throws \Throwable |
51
|
|
|
*/ |
52
|
8 |
|
public function normalize($data, $format = null, array $context = []) |
53
|
8 |
|
{ |
54
|
|
|
if ($data instanceof ResourceInterface && $this->eventDispatcher) { |
55
|
|
|
$event = new BeforeNormalizationEvent($data, (string) $format, $context); |
56
|
|
|
$this->eventDispatcher->dispatch($event, SerializationEvents::BEFORE_NORMALIZATION); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$normalized = parent::normalize($data, $format, $context); |
60
|
4 |
|
$normalized = $this->executeNormalizationCallable($normalized, $context['after_normalization'] ?? null); |
61
|
|
|
|
62
|
4 |
|
if ($data instanceof ResourceInterface && $this->eventDispatcher) { |
63
|
4 |
|
$event = new AfterNormalizationEvent($data, $normalized, (string) $format, $context); |
64
|
4 |
|
$this->eventDispatcher->dispatch($event, SerializationEvents::AFTER_NORMALIZATION); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
return $normalized; |
68
|
|
|
} |
69
|
4 |
|
|
70
|
|
|
/** |
71
|
2 |
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
2 |
|
public function denormalize($data, $type, $format = null, array $context = []) |
74
|
2 |
|
{ |
75
|
2 |
|
if (\is_a($type, ResourceInterface::class, true) && $this->eventDispatcher) { |
76
|
|
|
$event = new BeforeDenormalizationEvent($data, $type, (string) $format, $context); |
77
|
2 |
|
$this->eventDispatcher->dispatch($event, SerializationEvents::BEFORE_DENORMALIZATION); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
$denormalized = $this->executeNormalizationCallable($data, $context['before_denormalization'] ?? null); |
81
|
|
|
$denormalized = parent::denormalize($denormalized, $type, $format, $context); |
82
|
|
|
|
83
|
|
|
if (\is_a($type, ResourceInterface::class, true) && $this->eventDispatcher) { |
84
|
4 |
|
$event = new AfterDenormalizationEvent($data, $denormalized, (string) $format, $context); |
85
|
3 |
|
$this->eventDispatcher->dispatch($event, SerializationEvents::AFTER_DENORMALIZATION); |
86
|
4 |
|
} |
87
|
2 |
|
|
88
|
|
|
return $denormalized; |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
/** |
92
|
3 |
|
* Execute normalization callable |
93
|
3 |
|
* |
94
|
|
|
* @param mixed $data |
95
|
|
|
* @param \Closure|null $callable |
96
|
3 |
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
private function executeNormalizationCallable($data, \Closure $callable = null) |
100
|
|
|
{ |
101
|
|
|
if ($callable) { |
102
|
4 |
|
return $callable($data); |
103
|
|
|
} |
104
|
4 |
|
|
105
|
4 |
|
return $data; |
106
|
4 |
|
} |
107
|
|
|
} |
108
|
|
|
|