|
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\Visitor; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\Serializer\Context\ContextInterface; |
|
15
|
|
|
use Ivory\Serializer\Instantiator\InstantiatorInterface; |
|
16
|
|
|
use Ivory\Serializer\Mapping\PropertyMetadataInterface; |
|
17
|
|
|
use Ivory\Serializer\Mapping\TypeMetadataInterface; |
|
18
|
|
|
use Ivory\Serializer\Mutator\MutatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author GeLo <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class AbstractDeserializationVisitor extends AbstractGenericVisitor |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var InstantiatorInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $instantiator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MutatorInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $mutator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param InstantiatorInterface $instantiator |
|
37
|
|
|
* @param MutatorInterface $mutator |
|
38
|
|
|
*/ |
|
39
|
1492 |
|
public function __construct(InstantiatorInterface $instantiator, MutatorInterface $mutator) |
|
40
|
|
|
{ |
|
41
|
1492 |
|
$this->instantiator = $instantiator; |
|
42
|
1492 |
|
$this->mutator = $mutator; |
|
43
|
1492 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
584 |
|
public function prepare($data, ContextInterface $context) |
|
49
|
|
|
{ |
|
50
|
584 |
|
return $this->decode(parent::prepare($data, $context)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
48 |
|
public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context) |
|
57
|
|
|
{ |
|
58
|
48 |
|
return parent::visitBoolean( |
|
59
|
48 |
|
filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE), |
|
60
|
36 |
|
$type, |
|
61
|
36 |
|
$context |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param mixed $data |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
abstract protected function decode($data); |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
388 |
|
protected function doVisitObjectProperty( |
|
76
|
|
|
$data, |
|
77
|
|
|
$name, |
|
78
|
|
|
PropertyMetadataInterface $property, |
|
79
|
|
|
ContextInterface $context |
|
80
|
|
|
) { |
|
81
|
388 |
|
$type = $property->getType(); |
|
82
|
|
|
|
|
83
|
388 |
|
if ($type === null) { |
|
84
|
|
|
throw new \RuntimeException(sprintf( |
|
85
|
|
|
'You must define the type of the %s:%s.', |
|
86
|
|
|
$property->getClass(), |
|
87
|
|
|
$property->getName() |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
388 |
|
if (!$property->isWritable() || !isset($data[$name])) { |
|
92
|
152 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
328 |
|
$value = $this->navigator->navigate($data[$name], $context, $type); |
|
96
|
|
|
|
|
97
|
328 |
|
if ($value === null && $context->isNullIgnored()) { |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
328 |
|
$this->mutator->setValue( |
|
102
|
328 |
|
$this->result, |
|
103
|
328 |
|
$property->hasMutator() ? $property->getMutator() : $property->getName(), |
|
104
|
246 |
|
$value |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
328 |
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
416 |
|
protected function createResult($class) |
|
114
|
|
|
{ |
|
115
|
416 |
|
return $this->instantiator->instantiate($class); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|