-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSerializer.php
531 lines (456 loc) · 13.7 KB
/
Serializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<?php
namespace NilPortugues\Serializer;
use Closure;
use NilPortugues\Serializer\Serializer\InternalClasses\SplFixedArraySerializer;
use NilPortugues\Serializer\Strategy\StrategyInterface;
use ReflectionClass;
use ReflectionException;
use SplObjectStorage;
class Serializer
{
const CLASS_IDENTIFIER_KEY = '@type';
const CLASS_PARENT_KEY = '@parent';
const SCALAR_TYPE = '@scalar';
const SCALAR_VALUE = '@value';
const NULL_VAR = null;
const MAP_TYPE = '@map';
/**
* Storage for object.
*
* Used for recursion
*
* @var SplObjectStorage
*/
protected static $objectStorage;
/**
* Object mapping for recursion.
*
* @var array
*/
protected static $objectMapping = [];
/**
* Object mapping index.
*
* @var int
*/
protected static $objectMappingIndex = 0;
/**
* @var \NilPortugues\Serializer\Strategy\StrategyInterface|\NilPortugues\Serializer\Strategy\JsonStrategy
*/
protected $serializationStrategy;
/**
* @var array
*/
private $dateTimeClassType = ['DateTime', 'DateTimeImmutable', 'DateTimeZone', 'DateInterval', 'DatePeriod'];
/**
* @var array
*/
protected $serializationMap = [
'array' => 'serializeArray',
'integer' => 'serializeScalar',
'double' => 'serializeScalar',
'boolean' => 'serializeScalar',
'string' => 'serializeScalar',
];
/**
* Hack specific serialization classes.
*
* @var array
*/
protected $unserializationMapHHVM = [];
/**
* @param StrategyInterface $strategy
*/
public function __construct(StrategyInterface $strategy)
{
$this->serializationStrategy = $strategy;
}
/**
* This is handly specially in order to add additional data before the
* serialization process takes place using the transformer public methods, if any.
*
* @return StrategyInterface
*/
public function getTransformer()
{
return $this->serializationStrategy;
}
/**
* Serialize the value in JSON.
*
* @param mixed $value
*
* @return string JSON encoded
*
* @throws SerializerException
*/
public function serialize($value)
{
$this->reset();
return $this->serializationStrategy->serialize($this->serializeData($value));
}
/**
* Reset variables.
*/
protected function reset()
{
self::$objectStorage = new SplObjectStorage();
self::$objectMapping = [];
self::$objectMappingIndex = 0;
}
/**
* Parse the data to be json encoded.
*
* @param mixed $value
*
* @return mixed
*
* @throws SerializerException
*/
protected function serializeData($value)
{
$this->guardForUnsupportedValues($value);
if ($this->isInstanceOf($value, 'SplFixedArray')) {
return SplFixedArraySerializer::serialize($this, $value);
}
if (\is_object($value)) {
return $this->serializeObject($value);
}
$type = (\gettype($value) && $value !== null) ? \gettype($value) : 'string';
$func = $this->serializationMap[$type];
return $this->$func($value);
}
/**
* Check if a class is instance or extends from the expected instance.
*
* @param mixed $value
* @param string $classFQN
*
* @return bool
*/
private function isInstanceOf($value, $classFQN)
{
return is_object($value)
&& (strtolower(get_class($value)) === strtolower($classFQN) || \is_subclass_of($value, $classFQN, true));
}
/**
* @param mixed $value
*
* @throws SerializerException
*/
protected function guardForUnsupportedValues($value)
{
if ($value instanceof Closure) {
throw new SerializerException('Closures are not supported in Serializer');
}
if ($value instanceof \DatePeriod) {
throw new SerializerException(
'DatePeriod is not supported in Serializer. Loop through it and serialize the output.'
);
}
if (\is_resource($value)) {
throw new SerializerException('Resource is not supported in Serializer');
}
}
/**
* Unserialize the value from string.
*
* @param mixed $value
*
* @return mixed
*/
public function unserialize($value)
{
if (\is_array($value) && isset($value[self::SCALAR_TYPE])) {
return $this->unserializeData($value);
}
$this->reset();
return $this->unserializeData($this->serializationStrategy->unserialize($value));
}
/**
* Parse the json decode to convert to objects again.
*
* @param mixed $value
*
* @return mixed
*/
protected function unserializeData($value)
{
if ($value === null || !is_array($value)) {
return $value;
}
if (isset($value[self::MAP_TYPE]) && !isset($value[self::CLASS_IDENTIFIER_KEY])) {
$value = $value[self::SCALAR_VALUE];
return $this->unserializeData($value);
}
if (isset($value[self::SCALAR_TYPE])) {
return $this->getScalarValue($value);
}
if (isset($value[self::CLASS_PARENT_KEY]) && 0 === strcmp($value[self::CLASS_PARENT_KEY], 'SplFixedArray')) {
return SplFixedArraySerializer::unserialize($this, $value[self::CLASS_IDENTIFIER_KEY], $value);
}
if (isset($value[self::CLASS_IDENTIFIER_KEY])) {
return $this->unserializeObject($value);
}
return \array_map([$this, __FUNCTION__], $value);
}
/**
* @param $value
*
* @return float|int|null|bool
*/
protected function getScalarValue($value)
{
switch ($value[self::SCALAR_TYPE]) {
case 'integer':
return \intval($value[self::SCALAR_VALUE]);
case 'float':
return \floatval($value[self::SCALAR_VALUE]);
case 'boolean':
return $value[self::SCALAR_VALUE];
case 'NULL':
return self::NULL_VAR;
}
return $value[self::SCALAR_VALUE];
}
/**
* Convert the serialized array into an object.
*
* @param array $value
*
* @return object
*
* @throws SerializerException
*/
protected function unserializeObject(array $value)
{
$className = $value[self::CLASS_IDENTIFIER_KEY];
unset($value[self::CLASS_IDENTIFIER_KEY]);
if (isset($value[self::MAP_TYPE])) {
unset($value[self::MAP_TYPE]);
unset($value[self::SCALAR_VALUE]);
}
if ($className[0] === '@') {
return self::$objectMapping[substr($className, 1)];
}
if (!class_exists($className)) {
throw new SerializerException('Unable to find class '.$className);
}
return (null === ($obj = $this->unserializeDateTimeFamilyObject($value, $className)))
? $this->unserializeUserDefinedObject($value, $className) : $obj;
}
/**
* @param array $value
* @param string $className
*
* @return mixed
*/
protected function unserializeDateTimeFamilyObject(array $value, $className)
{
$obj = null;
if ($this->isDateTimeFamilyObject($className)) {
$obj = $this->restoreUsingUnserialize($className, $value);
self::$objectMapping[self::$objectMappingIndex++] = $obj;
}
return $obj;
}
/**
* @param string $className
*
* @return bool
*/
protected function isDateTimeFamilyObject($className)
{
$isDateTime = false;
foreach ($this->dateTimeClassType as $class) {
$isDateTime = $isDateTime || \is_subclass_of($className, $class, true) || $class === $className;
}
return $isDateTime;
}
/**
* @param string $className
* @param array $attributes
*
* @return mixed
*/
protected function restoreUsingUnserialize($className, array $attributes)
{
foreach ($attributes as &$attribute) {
$attribute = $this->unserializeData($attribute);
}
$obj = (object) $attributes;
$serialized = \preg_replace(
'|^O:\d+:"\w+":|',
'O:'.strlen($className).':"'.$className.'":',
\serialize($obj)
);
return \unserialize($serialized);
}
/**
* @param array $value
* @param string $className
*
* @return object
*/
protected function unserializeUserDefinedObject(array $value, $className)
{
$ref = new ReflectionClass($className);
$obj = $ref->newInstanceWithoutConstructor();
self::$objectMapping[self::$objectMappingIndex++] = $obj;
$this->setUnserializedObjectProperties($value, $ref, $obj);
if (\method_exists($obj, '__wakeup')) {
$obj->__wakeup();
}
return $obj;
}
/**
* @param array $value
* @param ReflectionClass $ref
* @param mixed $obj
*
* @return mixed
*/
protected function setUnserializedObjectProperties(array $value, ReflectionClass $ref, $obj)
{
foreach ($value as $property => $propertyValue) {
try {
$propRef = $ref->getProperty($property);
$propRef->setAccessible(true);
$propRef->setValue($obj, $this->unserializeData($propertyValue));
} catch (ReflectionException $e) {
$obj->$property = $this->unserializeData($propertyValue);
}
}
return $obj;
}
/**
* @param $value
*
* @return string
*/
protected function serializeScalar($value)
{
$type = \gettype($value);
if ($type === 'double') {
$type = 'float';
}
return [
self::SCALAR_TYPE => $type,
self::SCALAR_VALUE => $value,
];
}
/**
* @param array $value
*
* @return array
*/
protected function serializeArray(array $value)
{
if (\array_key_exists(self::MAP_TYPE, $value)) {
return $value;
}
$toArray = [self::MAP_TYPE => 'array', self::SCALAR_VALUE => []];
foreach ($value as $key => $field) {
$toArray[self::SCALAR_VALUE][$key] = $this->serializeData($field);
}
return $this->serializeData($toArray);
}
/**
* Extract the data from an object.
*
* @param mixed $value
*
* @return array
*/
protected function serializeObject($value)
{
if (self::$objectStorage->contains($value)) {
return [self::CLASS_IDENTIFIER_KEY => '@'.self::$objectStorage[$value]];
}
self::$objectStorage->attach($value, self::$objectMappingIndex++);
$reflection = new ReflectionClass($value);
$className = $reflection->getName();
return $this->serializeInternalClass($value, $className, $reflection);
}
/**
* @param mixed $value
* @param string $className
* @param ReflectionClass $ref
*
* @return array
*/
protected function serializeInternalClass($value, $className, ReflectionClass $ref)
{
$paramsToSerialize = $this->getObjectProperties($ref, $value);
$data = [self::CLASS_IDENTIFIER_KEY => $className];
$data += \array_map([$this, 'serializeData'], $this->extractObjectData($value, $ref, $paramsToSerialize));
return $data;
}
/**
* Return the list of properties to be serialized.
*
* @param ReflectionClass $ref
* @param $value
*
* @return array
*/
protected function getObjectProperties(ReflectionClass $ref, $value)
{
$props = [];
foreach ($ref->getProperties() as $prop) {
$props[] = $prop->getName();
}
return \array_unique(\array_merge($props, \array_keys(\get_object_vars($value))));
}
/**
* Extract the object data.
*
* @param mixed $value
* @param \ReflectionClass $rc
* @param array $properties
*
* @return array
*/
protected function extractObjectData($value, ReflectionClass $rc, array $properties)
{
$data = [];
$this->extractCurrentObjectProperties($value, $rc, $properties, $data);
$this->extractAllInhertitedProperties($value, $rc, $data);
return $data;
}
/**
* @param mixed $value
* @param ReflectionClass $rc
* @param array $properties
* @param array $data
*/
protected function extractCurrentObjectProperties($value, ReflectionClass $rc, array $properties, array &$data)
{
foreach ($properties as $propertyName) {
try {
$propRef = $rc->getProperty($propertyName);
$propRef->setAccessible(true);
$data[$propertyName] = $propRef->getValue($value);
} catch (ReflectionException $e) {
$data[$propertyName] = $value->$propertyName;
}
}
}
/**
* @param mixed $value
* @param ReflectionClass $rc
* @param array $data
*/
protected function extractAllInhertitedProperties($value, ReflectionClass $rc, array &$data)
{
do {
$rp = array();
/* @var $property \ReflectionProperty */
foreach ($rc->getProperties() as $property) {
$property->setAccessible(true);
$rp[$property->getName()] = $property->getValue($value);
}
$data = \array_merge($rp, $data);
} while ($rc = $rc->getParentClass());
}
}