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
|
738 |
|
public function __construct(InstantiatorInterface $instantiator, MutatorInterface $mutator) |
40
|
|
|
{ |
41
|
738 |
|
$this->instantiator = $instantiator; |
42
|
738 |
|
$this->mutator = $mutator; |
43
|
738 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
297 |
|
public function prepare($data, ContextInterface $context) |
49
|
|
|
{ |
50
|
297 |
|
return $this->decode(parent::prepare($data, $context)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
27 |
|
public function visitBoolean($data, TypeMetadataInterface $type, ContextInterface $context) |
57
|
|
|
{ |
58
|
27 |
|
return parent::visitBoolean( |
59
|
27 |
|
filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE), |
60
|
18 |
|
$type, |
61
|
|
|
$context |
62
|
18 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $data |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
abstract protected function decode($data); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
216 |
View Code Duplication |
protected function doVisitObjectProperty( |
|
|
|
|
76
|
|
|
$data, |
77
|
|
|
$name, |
78
|
|
|
PropertyMetadataInterface $property, |
79
|
|
|
ContextInterface $context |
80
|
|
|
) { |
81
|
216 |
|
if (!$property->isWritable() || !isset($data[$name])) { |
82
|
102 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
171 |
|
$value = $this->navigator->navigate($data[$name], $context, $property->getType()); |
86
|
|
|
|
87
|
171 |
|
if ($value === null && $context->isNullIgnored()) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// FIXME - Detect errors |
92
|
171 |
|
$this->mutator->setValue( |
93
|
171 |
|
$this->result, |
94
|
171 |
|
$property->hasMutator() ? $property->getMutator() : $property->getName(), |
95
|
|
|
$value |
96
|
114 |
|
); |
97
|
|
|
|
98
|
171 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
216 |
|
protected function createResult($class) |
105
|
|
|
{ |
106
|
216 |
|
return $this->instantiator->instantiate($class); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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.