1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Navigator; |
13
|
|
|
|
14
|
|
|
use Ivory\Serializer\Context\ContextInterface; |
15
|
|
|
use Ivory\Serializer\Direction; |
16
|
|
|
use Ivory\Serializer\Event\PostDeserializeEvent; |
17
|
|
|
use Ivory\Serializer\Event\PostSerializeEvent; |
18
|
|
|
use Ivory\Serializer\Event\PreDeserializeEvent; |
19
|
|
|
use Ivory\Serializer\Event\PreSerializeEvent; |
20
|
|
|
use Ivory\Serializer\Event\SerializerEvents; |
21
|
|
|
use Ivory\Serializer\Mapping\TypeMetadataInterface; |
22
|
|
|
use Ivory\Serializer\Type\Guesser\TypeGuesser; |
23
|
|
|
use Ivory\Serializer\Type\Guesser\TypeGuesserInterface; |
24
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author GeLo <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class EventNavigator implements NavigatorInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var NavigatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $navigator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EventDispatcherInterface |
38
|
|
|
*/ |
39
|
|
|
private $dispatcher; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TypeGuesserInterface |
43
|
|
|
*/ |
44
|
|
|
private $typeGuesser; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param NavigatorInterface $navigator |
48
|
|
|
* @param EventDispatcherInterface $dispatcher |
49
|
|
|
* @param TypeGuesserInterface|null $typeGuesser |
50
|
|
|
*/ |
51
|
16 |
|
public function __construct( |
52
|
|
|
NavigatorInterface $navigator, |
53
|
|
|
EventDispatcherInterface $dispatcher, |
54
|
|
|
TypeGuesserInterface $typeGuesser = null |
55
|
|
|
) { |
56
|
16 |
|
$this->navigator = $navigator; |
57
|
16 |
|
$this->dispatcher = $dispatcher; |
58
|
16 |
|
$this->typeGuesser = $typeGuesser ?: new TypeGuesser(); |
59
|
16 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
16 |
|
public function navigate($data, ContextInterface $context, TypeMetadataInterface $type = null) |
65
|
|
|
{ |
66
|
16 |
|
$type = $type ?: $this->typeGuesser->guess($data); |
67
|
16 |
|
$serialization = $context->getDirection() === Direction::SERIALIZATION; |
68
|
|
|
|
69
|
16 |
View Code Duplication |
if ($serialization) { |
|
|
|
|
70
|
8 |
|
$this->dispatcher->dispatch( |
71
|
8 |
|
SerializerEvents::PRE_SERIALIZE, |
72
|
8 |
|
$event = new PreSerializeEvent($data, $type, $context) |
73
|
4 |
|
); |
74
|
4 |
|
} else { |
75
|
8 |
|
$this->dispatcher->dispatch( |
76
|
8 |
|
SerializerEvents::PRE_DESERIALIZE, |
77
|
8 |
|
$event = new PreDeserializeEvent($data, $type, $context) |
78
|
4 |
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
16 |
|
$result = $this->navigator->navigate($data = $event->getData(), $context, $type = $event->getType()); |
82
|
|
|
|
83
|
16 |
View Code Duplication |
if ($serialization) { |
|
|
|
|
84
|
8 |
|
$this->dispatcher->dispatch( |
85
|
8 |
|
SerializerEvents::POST_SERIALIZE, |
86
|
8 |
|
new PostSerializeEvent($data, $type, $context) |
87
|
4 |
|
); |
88
|
4 |
|
} else { |
89
|
8 |
|
$this->dispatcher->dispatch( |
90
|
8 |
|
SerializerEvents::POST_DESERIALIZE, |
91
|
8 |
|
new PostDeserializeEvent($result, $type, $context) |
92
|
4 |
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
16 |
|
return $result; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.