|
6 | 6 |
|
7 | 7 | use ReflectionClass; |
8 | 8 | use ReflectionException; |
| 9 | +use SplDoublyLinkedList; |
| 10 | +use UnitEnum; |
| 11 | +use Zumba\JsonSerializer\Exception\JsonSerializerException; |
9 | 12 | use Zumba\JsonSerializer\JsonSerializer; |
10 | 13 |
|
| 14 | +use function array_key_exists; |
| 15 | +use function class_exists; |
| 16 | +use function constant; |
11 | 17 | use function in_array; |
| 18 | +use function is_subclass_of; |
12 | 19 | use function method_exists; |
| 20 | +use function substr; |
13 | 21 |
|
14 | 22 | use const PHP_VERSION_ID; |
15 | 23 |
|
@@ -84,4 +92,99 @@ protected function extractObjectData($value, $ref, $properties) |
84 | 92 |
|
85 | 93 | return $data; |
86 | 94 | } |
| 95 | + |
| 96 | + /** |
| 97 | + * Convert the serialized array into an object |
| 98 | + * |
| 99 | + * @param array<mixed> $value |
| 100 | + * |
| 101 | + * @return object |
| 102 | + * |
| 103 | + * @throws JsonSerializerException |
| 104 | + */ |
| 105 | + protected function unserializeObject($value) |
| 106 | + { |
| 107 | + $className = $value[self::CLASS_IDENTIFIER_KEY]; |
| 108 | + unset($value[self::CLASS_IDENTIFIER_KEY]); |
| 109 | + |
| 110 | + if ($className[0] === '@') { |
| 111 | + $index = substr($className, 1); |
| 112 | + |
| 113 | + return $this->objectMapping[$index]; |
| 114 | + } |
| 115 | + |
| 116 | + if (array_key_exists($className, $this->customObjectSerializerMap)) { |
| 117 | + $obj = $this->customObjectSerializerMap[$className]->unserialize($value); |
| 118 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 119 | + |
| 120 | + return $obj; |
| 121 | + } |
| 122 | + |
| 123 | + if (! class_exists($className)) { |
| 124 | + throw new JsonSerializerException('Unable to find class ' . $className); |
| 125 | + } |
| 126 | + |
| 127 | + if ($this->allowedClasses !== null && ! in_array($className, $this->allowedClasses, true)) { |
| 128 | + throw new JsonSerializerException( |
| 129 | + 'Class ' . $className . ' is not allowed for deserialization. ' . |
| 130 | + 'Use setAllowedClasses() to configure the list of allowed classes.' |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + if ($className === 'DateTime' || $className === 'DateTimeImmutable') { |
| 135 | + $obj = $this->restoreUsingUnserialize($className, $value); |
| 136 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 137 | + |
| 138 | + return $obj; |
| 139 | + } |
| 140 | + |
| 141 | + if (is_subclass_of($className, UnitEnum::class)) { |
| 142 | + $obj = constant($className . '::' . $value['name']); |
| 143 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 144 | + |
| 145 | + return $obj; |
| 146 | + } |
| 147 | + |
| 148 | + if (! $this->isSplList($className)) { |
| 149 | + $ref = new ReflectionClass($className); |
| 150 | + $obj = $ref->newInstanceWithoutConstructor(); |
| 151 | + } else { |
| 152 | + $obj = new $className(); |
| 153 | + } |
| 154 | + |
| 155 | + if ($obj instanceof SplDoublyLinkedList) { |
| 156 | + $obj->unserialize($value['value']); |
| 157 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 158 | + |
| 159 | + return $obj; |
| 160 | + } |
| 161 | + |
| 162 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 163 | + foreach ($value as $property => $propertyValue) { |
| 164 | + try { |
| 165 | + $propRef = $this->getReflectionProperty($ref, $property); |
| 166 | + if (PHP_VERSION_ID < 80100) { |
| 167 | + $propRef->setAccessible(true); |
| 168 | + } |
| 169 | + |
| 170 | + $propRef->setValue($obj, $this->unserializeData($propertyValue)); |
| 171 | + } catch (ReflectionException $e) { |
| 172 | + switch ($this->undefinedAttributeMode) { |
| 173 | + case self::UNDECLARED_PROPERTY_MODE_SET: |
| 174 | + $obj->$property = $this->unserializeData($propertyValue); |
| 175 | + break; |
| 176 | + case self::UNDECLARED_PROPERTY_MODE_IGNORE: |
| 177 | + break; |
| 178 | + case self::UNDECLARED_PROPERTY_MODE_EXCEPTION: |
| 179 | + throw new JsonSerializerException('Undefined attribute detected during unserialization'); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if (method_exists($obj, '__wakeup')) { |
| 185 | + $obj->__wakeup(); |
| 186 | + } |
| 187 | + |
| 188 | + return $obj; |
| 189 | + } |
87 | 190 | } |
0 commit comments