1 | <?php |
||
8 | class ObjectNormalizer |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $classAnnotation; |
||
14 | |||
15 | /** |
||
16 | * Maps objects to their corresponding index. Used to deconstruct cyclic references when normalizing. |
||
17 | * |
||
18 | * @var \SplObjectStorage |
||
19 | */ |
||
20 | private $objectToIndex; |
||
21 | |||
22 | /** |
||
23 | * Maps indexes to their corresponding object. Used to reconstruct cyclic references when de-normalizing. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $indexToObject = []; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | private $objectIndex = 0; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * @param string $classAnnotation |
||
37 | */ |
||
38 | 21 | public function __construct($classAnnotation = '@type') |
|
43 | |||
44 | /** |
||
45 | * @param object $object |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | 11 | public function normalize($object) |
|
58 | |||
59 | /** |
||
60 | * @param array $data |
||
61 | * |
||
62 | * @return object |
||
63 | */ |
||
64 | 10 | public function denormalize(array $data) |
|
69 | |||
70 | 19 | private function reset() |
|
76 | |||
77 | /** |
||
78 | * Extract the data from an object |
||
79 | * |
||
80 | * @param object $object |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 10 | private function normalizeObject($object) |
|
101 | |||
102 | /** |
||
103 | * Parse the data to be json encoded |
||
104 | * |
||
105 | * @param mixed $value |
||
106 | * |
||
107 | * @return mixed |
||
108 | * @throws JsonSerializerException |
||
109 | */ |
||
110 | 7 | private function normalizeValue($value) |
|
130 | |||
131 | /** |
||
132 | * Returns an array containing the object's properties to values |
||
133 | * |
||
134 | * @param object $object |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | 9 | private function extractObjectProperties($object) |
|
168 | |||
169 | /** |
||
170 | * Parse the json decode to convert to objects again |
||
171 | * |
||
172 | * @param mixed $data |
||
173 | * |
||
174 | * @return mixed |
||
175 | */ |
||
176 | 10 | private function denormalizeData($data) |
|
188 | |||
189 | /** |
||
190 | * Convert the serialized array into an object |
||
191 | * |
||
192 | * @param array $data |
||
193 | * |
||
194 | * @return object |
||
195 | * @throws JsonSerializerException |
||
196 | */ |
||
197 | 10 | private function denormalizeObject(array $data) |
|
236 | |||
237 | /** |
||
238 | * @param string $className |
||
239 | * @param array $attributes |
||
240 | * |
||
241 | * @return \DateTime |
||
242 | */ |
||
243 | 1 | private function denormalizeDateTime($className, array $attributes) |
|
254 | } |
||
255 |