|
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 array $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
|
4 |
|
private function denormalizeObject(array $data, string $class): object |
|
31
|
|
|
{ |
|
32
|
|
|
try { |
|
33
|
4 |
|
return $this->serializer->denormalize($data, $class); |
|
|
|
|
|
|
34
|
|
|
} catch (ExceptionInterface $e) { |
|
35
|
|
|
throw new InvalidArgumentException(sprintf( |
|
36
|
|
|
'The data for restoring the object is incorrect. %s', |
|
37
|
|
|
$e->getMessage() |
|
38
|
|
|
)); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Restores array objects from the decoded data. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array[] $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
|
2 |
|
private function denormalizeObjects(array $data, string $class): array |
|
52
|
|
|
{ |
|
53
|
2 |
|
foreach ($data as $key => $value) { |
|
54
|
2 |
|
$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
|
4 |
|
private function checkAndDecodeData(string $data, string $class, string $format): array |
|
70
|
|
|
{ |
|
71
|
4 |
|
if (!class_exists($class)) { |
|
72
|
|
|
throw new InvalidArgumentException(sprintf( |
|
73
|
|
|
'The "%s" class does not exist.', |
|
74
|
|
|
$class |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
$decodedData = $this->serializer->decode($data, $format); |
|
79
|
|
|
|
|
80
|
4 |
|
if (!is_array($decodedData)) { |
|
81
|
|
|
throw new InvalidArgumentException('The data for restoring the object is incorrect.'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
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
|
2 |
|
private function checkArrayObjects(array $objects): void |
|
94
|
|
|
{ |
|
95
|
2 |
|
foreach ($objects as $object) { |
|
96
|
2 |
|
if (!is_object($object)) { |
|
97
|
|
|
throw new InvalidArgumentException(sprintf( |
|
98
|
|
|
'The array item must be an object, %s received.', |
|
99
|
|
|
gettype($object) |
|
100
|
|
|
)); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
2 |
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|