1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Deserialization\Denormalizer; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\Accessor\AccessorInterface; |
8
|
|
|
use Chubbyphp\Deserialization\DeserializerLogicException; |
9
|
|
|
use Chubbyphp\Deserialization\DeserializerRuntimeException; |
10
|
|
|
|
11
|
|
|
final class ConvertTypeFieldDenormalizer implements FieldDenormalizerInterface |
12
|
|
|
{ |
13
|
|
|
const TYPE_BOOL = 'boolean'; |
14
|
|
|
const TYPE_FLOAT = 'float'; |
15
|
|
|
const TYPE_INT = 'int'; |
16
|
|
|
const TYPE_STRING = 'string'; |
17
|
|
|
|
18
|
|
|
const TYPES = [ |
19
|
|
|
self::TYPE_BOOL, |
20
|
|
|
self::TYPE_FLOAT, |
21
|
|
|
self::TYPE_INT, |
22
|
|
|
self::TYPE_STRING, |
23
|
|
|
]; |
24
|
|
|
/** |
25
|
|
|
* @var AccessorInterface |
26
|
|
|
*/ |
27
|
|
|
private $accessor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
private $emptyToNull; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws DeserializerLogicException |
41
|
|
|
*/ |
42
|
|
|
public function __construct(AccessorInterface $accessor, string $type, bool $emptyToNull = false) |
43
|
|
|
{ |
44
|
|
|
if (!in_array($type, self::TYPES, true)) { |
45
|
|
|
throw DeserializerLogicException::createConvertTypeDoesNotExists($type); |
46
|
25 |
|
} |
47
|
|
|
|
48
|
25 |
|
$this->accessor = $accessor; |
49
|
1 |
|
$this->type = $type; |
50
|
|
|
$this->emptyToNull = $emptyToNull; |
51
|
|
|
} |
52
|
24 |
|
|
53
|
24 |
|
/** |
54
|
24 |
|
* @param object $object |
55
|
24 |
|
* @param mixed $value |
56
|
|
|
* |
57
|
|
|
* @throws DeserializerRuntimeException |
58
|
|
|
*/ |
59
|
|
|
public function denormalizeField( |
60
|
|
|
string $path, |
61
|
|
|
$object, |
62
|
|
|
$value, |
63
|
|
|
DenormalizerContextInterface $context, |
64
|
|
|
DenormalizerInterface $denormalizer = null |
65
|
|
|
): void { |
66
|
24 |
|
if ('' === $value && $this->emptyToNull) { |
67
|
|
|
$this->accessor->setValue($object, null); |
68
|
|
|
|
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->accessor->setValue($object, $this->convertType($value)); |
73
|
24 |
|
} |
74
|
1 |
|
|
75
|
|
|
/** |
76
|
1 |
|
* @param mixed $value |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
23 |
|
*/ |
80
|
23 |
|
private function convertType($value) |
81
|
|
|
{ |
82
|
|
|
$type = gettype($value); |
83
|
|
|
|
84
|
|
|
if ($this->type === $type || !is_scalar($value)) { |
85
|
|
|
return $value; |
86
|
|
|
} |
87
|
23 |
|
|
88
|
|
|
switch ($this->type) { |
89
|
23 |
|
case self::TYPE_BOOL: |
90
|
|
|
return $this->convertBool($value); |
91
|
23 |
|
case self::TYPE_INT: |
92
|
3 |
|
return $this->convertInt($value); |
93
|
|
|
case self::TYPE_FLOAT: |
94
|
|
|
return $this->convertFloat($value); |
95
|
20 |
|
default: |
96
|
20 |
|
return $this->convertString($value); |
97
|
5 |
|
} |
98
|
15 |
|
} |
99
|
5 |
|
|
100
|
10 |
|
/** |
101
|
6 |
|
* @param bool|float|int|string $value |
102
|
|
|
* |
103
|
4 |
|
* @return bool|float|int|string |
104
|
|
|
*/ |
105
|
|
|
private function convertBool($value) |
106
|
|
|
{ |
107
|
|
|
if ('true' === $value) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ('false' === $value) { |
112
|
5 |
|
return false; |
113
|
|
|
} |
114
|
5 |
|
|
115
|
1 |
|
return $value; |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
/** |
119
|
1 |
|
* @param mixed $value |
120
|
|
|
* |
121
|
|
|
* @return bool|float|int|string |
122
|
3 |
|
*/ |
123
|
|
|
private function convertFloat($value) |
124
|
|
|
{ |
125
|
|
|
if (!is_numeric($value)) { |
126
|
|
|
return $value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return (float) $value; |
130
|
6 |
|
} |
131
|
|
|
|
132
|
6 |
|
/** |
133
|
3 |
|
* @param mixed $value |
134
|
|
|
* |
135
|
|
|
* @return bool|float|int|string |
136
|
3 |
|
*/ |
137
|
|
|
private function convertInt($value) |
138
|
|
|
{ |
139
|
|
|
if (!is_numeric($value)) { |
140
|
|
|
return $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ((string) (int) $value !== (string) $value) { |
144
|
5 |
|
return $value; |
145
|
|
|
} |
146
|
5 |
|
|
147
|
2 |
|
return (int) $value; |
148
|
|
|
} |
149
|
|
|
|
150
|
3 |
|
/** |
151
|
1 |
|
* @param bool|float|int|string $value |
152
|
|
|
* |
153
|
|
|
* @return bool|float|int|string |
154
|
2 |
|
*/ |
155
|
|
|
private function convertString($value) |
156
|
|
|
{ |
157
|
|
|
if (is_bool($value)) { |
158
|
|
|
return $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return (string) $value; |
162
|
4 |
|
} |
163
|
|
|
} |
164
|
|
|
|