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; |
13
|
|
|
|
14
|
|
|
use Ivory\Serializer\Context\Context; |
15
|
|
|
use Ivory\Serializer\Context\ContextInterface; |
16
|
|
|
use Ivory\Serializer\Mapping\TypeMetadataInterface; |
17
|
|
|
use Ivory\Serializer\Navigator\Navigator; |
18
|
|
|
use Ivory\Serializer\Navigator\NavigatorInterface; |
19
|
|
|
use Ivory\Serializer\Registry\VisitorRegistry; |
20
|
|
|
use Ivory\Serializer\Registry\VisitorRegistryInterface; |
21
|
|
|
use Ivory\Serializer\Type\Parser\TypeParser; |
22
|
|
|
use Ivory\Serializer\Type\Parser\TypeParserInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Serializer implements SerializerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var NavigatorInterface |
31
|
|
|
*/ |
32
|
|
|
private $navigator; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var VisitorRegistryInterface |
36
|
|
|
*/ |
37
|
|
|
private $visitorRegistry; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TypeParserInterface |
41
|
|
|
*/ |
42
|
|
|
private $typeParser; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param NavigatorInterface|null $navigator |
46
|
|
|
* @param VisitorRegistryInterface|null $visitorRegistry |
47
|
|
|
* @param TypeParserInterface|null $typeParser |
48
|
|
|
*/ |
49
|
1492 |
|
public function __construct( |
50
|
|
|
NavigatorInterface $navigator = null, |
51
|
|
|
VisitorRegistryInterface $visitorRegistry = null, |
52
|
|
|
TypeParserInterface $typeParser = null |
53
|
|
|
) { |
54
|
1492 |
|
$this->navigator = $navigator ?: new Navigator(); |
55
|
1492 |
|
$this->visitorRegistry = $visitorRegistry ?: VisitorRegistry::create(); |
56
|
1492 |
|
$this->typeParser = $typeParser ?: new TypeParser(); |
57
|
1492 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
888 |
|
public function serialize($data, $format, ContextInterface $context = null) |
63
|
|
|
{ |
64
|
888 |
|
return $this->navigate($data, Direction::SERIALIZATION, $format, $context); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
604 |
|
public function deserialize($data, $type, $format, ContextInterface $context = null) |
71
|
|
|
{ |
72
|
604 |
|
if (is_string($type)) { |
73
|
588 |
|
$type = $this->typeParser->parse($type); |
74
|
|
|
} |
75
|
|
|
|
76
|
604 |
View Code Duplication |
if (!$type instanceof TypeMetadataInterface) { |
|
|
|
|
77
|
16 |
|
throw new \InvalidArgumentException(sprintf( |
78
|
16 |
|
'The type must be a string or a "%s", got "%s".', |
79
|
16 |
|
TypeMetadataInterface::class, |
80
|
16 |
|
is_object($type) ? get_class($type) : gettype($type) |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
|
84
|
588 |
|
return $this->navigate($data, Direction::DESERIALIZATION, $format, $context, $type); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param mixed $data |
89
|
|
|
* @param int $direction |
90
|
|
|
* @param string $format |
91
|
|
|
* @param ContextInterface|null $context |
92
|
|
|
* @param TypeMetadataInterface|null $type |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
1476 |
|
private function navigate( |
97
|
|
|
$data, |
98
|
|
|
$direction, |
99
|
|
|
$format, |
100
|
|
|
ContextInterface $context = null, |
101
|
|
|
TypeMetadataInterface $type = null |
102
|
|
|
) { |
103
|
1476 |
|
$visitor = $this->visitorRegistry->getVisitor($direction, $format); |
104
|
|
|
|
105
|
1472 |
|
$context = $context ?: new Context(); |
106
|
1472 |
|
$context->initialize($this->navigator, $visitor, $direction, $format); |
107
|
1472 |
|
$this->navigator->navigate($visitor->prepare($data, $context), $context, $type); |
108
|
|
|
|
109
|
1456 |
|
return $visitor->getResult(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.