|
2 | 2 |
|
3 | 3 | namespace Nuxtifyts\PhpDto\Contexts; |
4 | 4 |
|
| 5 | +use BackedEnum; |
| 6 | +use Nuxtifyts\PhpDto\Exceptions\DataCreationException; |
| 7 | +use UnitEnum; |
5 | 8 | use Nuxtifyts\PhpDto\Attributes\Property\Aliases; |
6 | 9 | use Nuxtifyts\PhpDto\Attributes\Property\CipherTarget; |
7 | 10 | use Nuxtifyts\PhpDto\Attributes\Property\Computed; |
8 | 11 | use Nuxtifyts\PhpDto\Attributes\Property\DefaultsTo; |
9 | 12 | use Nuxtifyts\PhpDto\Attributes\Property\WithRefiner; |
| 13 | +use Nuxtifyts\PhpDto\Data; |
10 | 14 | use Nuxtifyts\PhpDto\DataCiphers\CipherConfig; |
11 | 15 | use Nuxtifyts\PhpDto\DataRefiners\DataRefiner; |
12 | 16 | use Nuxtifyts\PhpDto\Enums\Property\Type; |
|
18 | 22 | use Nuxtifyts\PhpDto\Serializers\Serializer; |
19 | 23 | use Nuxtifyts\PhpDto\Support\Traits\HasSerializers; |
20 | 24 | use Nuxtifyts\PhpDto\Support\Traits\HasTypes; |
| 25 | +use DateTimeInterface; |
| 26 | +use ReflectionEnum; |
| 27 | +use ReflectionException; |
21 | 28 | use ReflectionProperty; |
22 | 29 | use ReflectionAttribute; |
| 30 | +use ReflectionClass; |
23 | 31 | use Exception; |
24 | 32 |
|
25 | 33 | class PropertyContext |
@@ -225,4 +233,61 @@ public function serializeFrom(object $object): array |
225 | 233 | throw new SerializeException('Could not serialize value for property: ' . $this->propertyName); |
226 | 234 | } |
227 | 235 | } |
| 236 | + |
| 237 | + /** |
| 238 | + * @throws UnsupportedTypeException |
| 239 | + * @throws ReflectionException |
| 240 | + * @throws DataCreationException |
| 241 | + */ |
| 242 | + public function emptyValue(): mixed |
| 243 | + { |
| 244 | + if ($this->isNullable) { |
| 245 | + return null; |
| 246 | + } |
| 247 | + |
| 248 | + if (! $typeContext = $this->typeContexts[0] ?? null) { |
| 249 | + throw UnsupportedTypeException::emptyType(); |
| 250 | + } |
| 251 | + |
| 252 | + switch (true) { |
| 253 | + case $typeContext->type === Type::STRING: |
| 254 | + return ''; |
| 255 | + |
| 256 | + case $typeContext->type === Type::INT: |
| 257 | + return 0; |
| 258 | + |
| 259 | + case $typeContext->type === Type::FLOAT: |
| 260 | + return 0.0; |
| 261 | + |
| 262 | + case $typeContext->type === Type::BOOLEAN: |
| 263 | + return false; |
| 264 | + |
| 265 | + case $typeContext->type === Type::ARRAY: |
| 266 | + return []; |
| 267 | + |
| 268 | + case $typeContext->type === Type::DATA: |
| 269 | + /** @var null|ReflectionClass<Data> $reflection */ |
| 270 | + $reflection = $typeContext->reflection; |
| 271 | + |
| 272 | + return !$reflection |
| 273 | + ? throw UnsupportedTypeException::invalidReflection() |
| 274 | + : ClassContext::getInstance($reflection)->emptyValue(); |
| 275 | + |
| 276 | + case $typeContext->type === Type::BACKED_ENUM: |
| 277 | + /** @var null|ReflectionEnum<UnitEnum|BackedEnum> $reflection */ |
| 278 | + $reflection = $typeContext->reflection; |
| 279 | + |
| 280 | + return $reflection instanceof ReflectionEnum && $reflection->isBacked() |
| 281 | + ? $reflection->getCases()[0]->getValue() |
| 282 | + : throw UnsupportedTypeException::invalidReflection(); |
| 283 | + |
| 284 | + default: |
| 285 | + /** @var null|DateTimeInterface $dateTime */ |
| 286 | + $dateTime = $typeContext->reflection?->newInstance(); |
| 287 | + |
| 288 | + return $dateTime instanceof DateTimeInterface |
| 289 | + ? $dateTime |
| 290 | + : throw UnsupportedTypeException::invalidReflection(); |
| 291 | + } |
| 292 | + } |
228 | 293 | } |
0 commit comments