1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Serializer; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Component\Serializer\Exception\ExceptionInterface; |
9
|
|
|
use Symfony\Component\Serializer\Serializer; |
10
|
|
|
|
11
|
|
|
use function class_exists; |
12
|
|
|
use function gettype; |
13
|
|
|
use function is_array; |
14
|
|
|
use function is_object; |
15
|
|
|
use function sprintf; |
16
|
|
|
|
17
|
|
|
trait ObjectSerializerTrait |
18
|
|
|
{ |
19
|
|
|
private Serializer $serializer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Restores object from the decoded data. |
23
|
|
|
* |
24
|
|
|
* @param mixed $data The decoded data for restoring the object. |
25
|
|
|
* @param string $class The class name of the object to be restored. |
26
|
|
|
* @return object The restored object. |
27
|
|
|
* @throws InvalidArgumentException If the data to restore the object is incorrect. |
28
|
|
|
* @psalm-suppress InvalidReturnType |
29
|
|
|
*/ |
30
|
6 |
|
private function denormalizeObject($data, string $class): object |
31
|
|
|
{ |
32
|
|
|
try { |
33
|
6 |
|
return $this->serializer->denormalize($data, $class); |
|
|
|
|
34
|
2 |
|
} catch (ExceptionInterface $e) { |
35
|
2 |
|
throw new InvalidArgumentException(sprintf( |
36
|
2 |
|
'The data for restoring the object is incorrect. %s', |
37
|
2 |
|
$e->getMessage() |
38
|
|
|
)); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Restores array objects from the decoded data. |
44
|
|
|
* |
45
|
|
|
* @param mixed[] $data The decoded data for restoring the object. |
46
|
|
|
* @param string $class The class name of the objects to be restored. |
47
|
|
|
* @return object[] The restored array objects. |
48
|
|
|
* @throws InvalidArgumentException If the data to restore the objects is incorrect. |
49
|
|
|
* @psalm-suppress InvalidReturnType |
50
|
|
|
*/ |
51
|
3 |
|
private function denormalizeObjects(array $data, string $class): array |
52
|
|
|
{ |
53
|
3 |
|
foreach ($data as $key => $value) { |
54
|
3 |
|
$data[$key] = $this->denormalizeObject($value, $class); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return $data; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks and decodes data from its serialized representations. |
62
|
|
|
* |
63
|
|
|
* @param string $data The serialized string. |
64
|
|
|
* @param string $class The name of the object class to be restored. |
65
|
|
|
* @param string $format The format of the sterilized data. |
66
|
|
|
* @return array The decoded data. |
67
|
|
|
* @throws InvalidArgumentException if class does not exist or data to restore the object is incorrect. |
68
|
|
|
*/ |
69
|
12 |
|
private function checkAndDecodeData(string $data, string $class, string $format): array |
70
|
|
|
{ |
71
|
12 |
|
if (!class_exists($class)) { |
72
|
4 |
|
throw new InvalidArgumentException(sprintf( |
73
|
4 |
|
'The "%s" class does not exist.', |
74
|
4 |
|
$class |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
$decodedData = $this->serializer->decode($data, $format); |
79
|
|
|
|
80
|
8 |
|
if (!is_array($decodedData)) { |
81
|
2 |
|
throw new InvalidArgumentException('The data for restoring the object is incorrect.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
return $decodedData; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Checks that each item of the array is an object. |
89
|
|
|
* |
90
|
|
|
* @param array $objects |
91
|
|
|
* @throws InvalidArgumentException if the array of objects is incorrect. |
92
|
|
|
*/ |
93
|
4 |
|
private function checkArrayObjects(array $objects): void |
94
|
|
|
{ |
95
|
4 |
|
foreach ($objects as $object) { |
96
|
4 |
|
if (!is_object($object)) { |
97
|
2 |
|
throw new InvalidArgumentException(sprintf( |
98
|
2 |
|
'The array item must be an object, %s received.', |
99
|
2 |
|
gettype($object) |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
2 |
|
} |
104
|
|
|
} |
105
|
|
|
|