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