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
|
549 |
|
public function __construct( |
50
|
|
|
NavigatorInterface $navigator = null, |
51
|
|
|
VisitorRegistryInterface $visitorRegistry = null, |
52
|
|
|
TypeParserInterface $typeParser = null |
53
|
|
|
) { |
54
|
549 |
|
$this->navigator = $navigator ?: new Navigator(); |
55
|
549 |
|
$this->visitorRegistry = $visitorRegistry ?: VisitorRegistry::create(); |
56
|
549 |
|
$this->typeParser = $typeParser ?: new TypeParser(); |
57
|
549 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
315 |
|
public function serialize($data, $format, ContextInterface $context = null) |
63
|
2 |
|
{ |
64
|
315 |
|
return $this->navigate($data, Direction::SERIALIZATION, $format, $context); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
234 |
|
public function deserialize($data, $type, $format, ContextInterface $context = null) |
71
|
|
|
{ |
72
|
234 |
|
if (is_string($type)) { |
73
|
234 |
|
$type = $this->typeParser->parse($type); |
74
|
156 |
|
} |
75
|
|
|
|
76
|
234 |
|
if (!$type instanceof TypeMetadataInterface) { |
77
|
|
|
throw new \InvalidArgumentException(sprintf( |
78
|
|
|
'The type must be a string or a "%s", got "%s".', |
79
|
|
|
TypeMetadataInterface::class, |
80
|
|
|
is_object($type) ? get_class($type) : gettype($type) |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
|
84
|
234 |
|
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
|
549 |
|
private function navigate( |
97
|
|
|
$data, |
98
|
|
|
$direction, |
99
|
|
|
$format, |
100
|
|
|
ContextInterface $context = null, |
101
|
|
|
TypeMetadataInterface $type = null |
102
|
|
|
) { |
103
|
549 |
|
$visitor = $this->visitorRegistry->getVisitor($direction, $format); |
104
|
|
|
|
105
|
549 |
|
$context = $context ?: new Context(); |
106
|
549 |
|
$context->initialize($this->navigator, $visitor, $direction); |
107
|
549 |
|
$this->navigator->navigate($visitor->prepare($data, $context), $context, $type); |
108
|
|
|
|
109
|
549 |
|
return $visitor->getResult(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|