|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jenschude <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Common; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @package Commercetools\Core\Model\Common |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractJsonDeserializeObject implements JsonDeserializeInterface, ObjectTreeInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use ObjectTreeTrait; |
|
14
|
|
|
|
|
15
|
|
|
const JSON_DESERIALIZE_INTERFACE = JsonDeserializeInterface::class; |
|
16
|
|
|
const TYPEABLE_INTERFACE = TypeableInterface::class; |
|
17
|
|
|
|
|
18
|
|
|
abstract protected function initialize($field); |
|
19
|
|
|
|
|
20
|
|
|
protected $rawData = []; |
|
21
|
|
|
protected $typeData = []; |
|
22
|
|
|
protected $initialized = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $data |
|
26
|
|
|
* @param Context|callable $context |
|
27
|
|
|
*/ |
|
28
|
3191 |
|
public function __construct(array $data = [], $context = null) |
|
29
|
|
|
{ |
|
30
|
3191 |
|
$this->setContext($context); |
|
31
|
3191 |
|
if (!is_null($data)) { |
|
|
|
|
|
|
32
|
3191 |
|
$this->rawData = $this->typeData = $data; |
|
33
|
|
|
} |
|
34
|
3191 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected static $primitives = [ |
|
37
|
|
|
'bool' => 'is_bool', |
|
38
|
|
|
'int' => 'is_int', |
|
39
|
|
|
'string' => 'is_string', |
|
40
|
|
|
'float' => 'is_float', |
|
41
|
|
|
'array' => 'is_array' |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var bool[] |
|
46
|
|
|
*/ |
|
47
|
|
|
protected static $interfaces = []; |
|
48
|
|
|
|
|
49
|
|
|
public function __sleep() |
|
50
|
|
|
{ |
|
51
|
|
|
return array_diff(array_keys(get_object_vars($this)), ['context']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $type |
|
56
|
|
|
* @param $interfaceName |
|
57
|
|
|
* @return bool |
|
58
|
|
|
* @internal |
|
59
|
|
|
*/ |
|
60
|
673 |
|
protected function hasInterface($type, $interfaceName) |
|
61
|
|
|
{ |
|
62
|
673 |
|
$interfaceName = trim($interfaceName, '\\'); |
|
63
|
673 |
|
$cacheKey = $interfaceName . '-' . $type; |
|
64
|
673 |
|
if (!isset(static::$interfaces[$cacheKey])) { |
|
65
|
107 |
|
$interface = false; |
|
66
|
107 |
|
if ($this->isPrimitive($type) === false |
|
67
|
107 |
|
&& isset(class_implements($type)[$interfaceName]) |
|
68
|
|
|
) { |
|
69
|
94 |
|
$interface = true; |
|
70
|
|
|
} |
|
71
|
107 |
|
static::$interfaces[$cacheKey] = $interface; |
|
72
|
|
|
} |
|
73
|
673 |
|
return static::$interfaces[$cacheKey]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $type |
|
78
|
|
|
* @return string|false |
|
79
|
|
|
* @internal |
|
80
|
|
|
*/ |
|
81
|
2393 |
|
protected function isPrimitive($type) |
|
82
|
|
|
{ |
|
83
|
2393 |
|
if (!isset(static::$primitives[$type])) { |
|
84
|
875 |
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2334 |
|
return static::$primitives[$type]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string|bool $type |
|
92
|
|
|
* @param mixed $value |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
2311 |
|
protected function isValidType($type, $value) |
|
96
|
|
|
{ |
|
97
|
2311 |
|
return !is_string($type) || is_null($value) || $this->isType($type, $value); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $type |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
* @return bool |
|
104
|
|
|
* @internal |
|
105
|
|
|
*/ |
|
106
|
2305 |
|
protected function isType($type, $value) |
|
107
|
|
|
{ |
|
108
|
2305 |
|
if ($typeFunction = $this->isPrimitive($type)) { |
|
109
|
2277 |
|
return $typeFunction($value); |
|
110
|
|
|
} |
|
111
|
732 |
|
return $value instanceof $type; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param $value |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function isDeserializable($value) |
|
119
|
|
|
{ |
|
120
|
|
|
return (is_object($value) && $this->hasInterface(get_class($value), static::JSON_DESERIALIZE_INTERFACE)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param $type |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
680 |
|
protected function isDeserializableType($type) |
|
128
|
|
|
{ |
|
129
|
680 |
|
if (!is_string($type) || empty($type)) { |
|
130
|
59 |
|
return false; |
|
131
|
|
|
} |
|
132
|
672 |
|
return $this->hasInterface($type, static::JSON_DESERIALIZE_INTERFACE); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
635 |
|
protected function isTypeableType($type) |
|
136
|
|
|
{ |
|
137
|
635 |
|
if (!is_string($type) || empty($type)) { |
|
138
|
59 |
|
return false; |
|
139
|
|
|
} |
|
140
|
604 |
|
return $this->hasInterface($type, static::TYPEABLE_INTERFACE); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1436 |
|
protected function getTyped($offset) |
|
144
|
|
|
{ |
|
145
|
1436 |
|
if (!isset($this->initialized[$offset])) { |
|
146
|
875 |
|
$this->initialize($offset); |
|
147
|
|
|
} |
|
148
|
1436 |
|
if (array_key_exists($offset, $this->typeData)) { |
|
149
|
1436 |
|
return $this->typeData[$offset]; |
|
150
|
|
|
} |
|
151
|
|
|
return $this->rawData[$offset]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param array $rawData |
|
156
|
|
|
* @internal |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
34 |
|
public function setRawData(array $rawData) |
|
160
|
|
|
{ |
|
161
|
34 |
|
$this->rawData = $this->typeData = $rawData; |
|
162
|
|
|
|
|
163
|
34 |
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param $field |
|
168
|
|
|
* @param $default |
|
169
|
|
|
* @return mixed |
|
170
|
|
|
*/ |
|
171
|
871 |
|
protected function getRaw($field, $default = null) |
|
172
|
|
|
{ |
|
173
|
871 |
|
return isset($this->rawData[$field])? $this->rawData[$field]: $default; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
#[\ReturnTypeWillChange] |
|
177
|
|
|
/** |
|
178
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
|
179
|
|
|
* Specify data which should be serialized to JSON |
|
180
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
181
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
182
|
|
|
* which is a value of any type other than a resource. |
|
183
|
740 |
|
*/ |
|
184
|
|
|
public function jsonSerialize() |
|
185
|
740 |
|
{ |
|
186
|
|
|
return $this->toJson(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return array |
|
191
|
740 |
|
*/ |
|
192
|
|
|
protected function toJson() |
|
193
|
740 |
|
{ |
|
194
|
738 |
|
$data = array_filter($this->typeData, function ($value) { |
|
195
|
740 |
|
return !is_null($value); |
|
196
|
|
|
}); |
|
197
|
740 |
|
|
|
198
|
|
|
return $data; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return array |
|
203
|
6 |
|
*/ |
|
204
|
|
|
public function toArray() |
|
205
|
6 |
|
{ |
|
206
|
5 |
|
$data = array_filter($this->typeData, function ($value) { |
|
207
|
6 |
|
return !is_null($value); |
|
208
|
6 |
|
}); |
|
209
|
6 |
|
$data = array_map( |
|
210
|
5 |
|
function ($value) { |
|
211
|
2 |
|
if ($value instanceof JsonDeserializeInterface) { |
|
212
|
|
|
return $value->toArray(); |
|
213
|
5 |
|
} |
|
214
|
6 |
|
return $value; |
|
215
|
|
|
}, |
|
216
|
|
|
$data |
|
217
|
6 |
|
); |
|
218
|
|
|
return $data; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param array $data |
|
223
|
|
|
* @param Context|callable $context |
|
224
|
|
|
* @return static |
|
225
|
1121 |
|
*/ |
|
226
|
|
|
public static function fromArray(array $data, $context = null) |
|
227
|
1121 |
|
{ |
|
228
|
|
|
return new static($data, $context); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param Context|callable $context |
|
233
|
|
|
* @return static |
|
234
|
2788 |
|
*/ |
|
235
|
|
|
final public static function of($context = null) |
|
236
|
2788 |
|
{ |
|
237
|
|
|
return new static([], $context); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|