|
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; |
| 19 | +use function method_exists; |
| 20 | +use function substr; |
12 | 21 |
|
13 | 22 | /** |
14 | 23 | * Used for .out files generation |
@@ -54,4 +63,94 @@ protected function extractObjectData($value, $ref, $properties): array |
54 | 63 |
|
55 | 64 | return $data; |
56 | 65 | } |
| 66 | + |
| 67 | + /** |
| 68 | + * Convert the serialized array into an object |
| 69 | + * |
| 70 | + * @param array<mixed> $value |
| 71 | + * |
| 72 | + * @throws JsonSerializerException |
| 73 | + */ |
| 74 | + // phpcs:ignore SlevomatCodingStandard.TypeHints |
| 75 | + protected function unserializeObject($value): object |
| 76 | + { |
| 77 | + $className = $value[self::CLASS_IDENTIFIER_KEY]; |
| 78 | + unset($value[self::CLASS_IDENTIFIER_KEY]); |
| 79 | + |
| 80 | + if ($className[0] === '@') { |
| 81 | + $index = substr($className, 1); |
| 82 | + |
| 83 | + return $this->objectMapping[$index]; |
| 84 | + } |
| 85 | + |
| 86 | + if (array_key_exists($className, $this->customObjectSerializerMap)) { |
| 87 | + $obj = $this->customObjectSerializerMap[$className]->unserialize($value); |
| 88 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 89 | + |
| 90 | + return $obj; |
| 91 | + } |
| 92 | + |
| 93 | + if (! class_exists($className)) { |
| 94 | + throw new JsonSerializerException('Unable to find class ' . $className); |
| 95 | + } |
| 96 | + |
| 97 | + if ($this->allowedClasses !== null && ! in_array($className, $this->allowedClasses, true)) { |
| 98 | + throw new JsonSerializerException( |
| 99 | + 'Class ' . $className . ' is not allowed for deserialization. ' . |
| 100 | + 'Use setAllowedClasses() to configure the list of allowed classes.', |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + if ($className === 'DateTime' || $className === 'DateTimeImmutable') { |
| 105 | + $obj = $this->restoreUsingUnserialize($className, $value); |
| 106 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 107 | + |
| 108 | + return $obj; |
| 109 | + } |
| 110 | + |
| 111 | + if (is_subclass_of($className, UnitEnum::class)) { |
| 112 | + $obj = constant($className . '::' . $value['name']); |
| 113 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 114 | + |
| 115 | + return $obj; |
| 116 | + } |
| 117 | + |
| 118 | + if (! $this->isSplList($className)) { |
| 119 | + $ref = new ReflectionClass($className); |
| 120 | + $obj = $ref->newInstanceWithoutConstructor(); |
| 121 | + } else { |
| 122 | + $obj = new $className(); |
| 123 | + } |
| 124 | + |
| 125 | + if ($obj instanceof SplDoublyLinkedList) { |
| 126 | + $obj->unserialize($value['value']); |
| 127 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 128 | + |
| 129 | + return $obj; |
| 130 | + } |
| 131 | + |
| 132 | + $this->objectMapping[$this->objectMappingIndex++] = $obj; |
| 133 | + foreach ($value as $property => $propertyValue) { |
| 134 | + try { |
| 135 | + $propRef = $this->getReflectionProperty($ref, $property); |
| 136 | + $propRef->setValue($obj, $this->unserializeData($propertyValue)); |
| 137 | + } catch (ReflectionException) { |
| 138 | + switch ($this->undefinedAttributeMode) { |
| 139 | + case self::UNDECLARED_PROPERTY_MODE_SET: |
| 140 | + $obj->$property = $this->unserializeData($propertyValue); |
| 141 | + break; |
| 142 | + case self::UNDECLARED_PROPERTY_MODE_IGNORE: |
| 143 | + break; |
| 144 | + case self::UNDECLARED_PROPERTY_MODE_EXCEPTION: |
| 145 | + throw new JsonSerializerException('Undefined attribute detected during unserialization'); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (method_exists($obj, '__wakeup')) { |
| 151 | + $obj->__wakeup(); |
| 152 | + } |
| 153 | + |
| 154 | + return $obj; |
| 155 | + } |
57 | 156 | } |
0 commit comments