diff --git a/src/Contexts/TypeContext.php b/src/Contexts/TypeContext.php index 4e502a1..083ceb9 100644 --- a/src/Contexts/TypeContext.php +++ b/src/Contexts/TypeContext.php @@ -109,7 +109,7 @@ public static function getInstances(PropertyContext $property): array ); break; default: - throw UnsupportedTypeException::unknownType($type); + $instances[] = new static(Type::MIXED); } } diff --git a/src/Enums/Property/Type.php b/src/Enums/Property/Type.php index 3953d28..0ed8b95 100644 --- a/src/Enums/Property/Type.php +++ b/src/Enums/Property/Type.php @@ -12,6 +12,7 @@ enum Type: string case DATETIME = 'datetime'; case DATA = 'data'; case ARRAY = 'array'; + case MIXED = 'mixed'; /** @var list */ public const array SCALAR_TYPES = [ diff --git a/src/Exceptions/UnsupportedTypeException.php b/src/Exceptions/UnsupportedTypeException.php index 9604b18..52aeca1 100644 --- a/src/Exceptions/UnsupportedTypeException.php +++ b/src/Exceptions/UnsupportedTypeException.php @@ -6,22 +6,14 @@ class UnsupportedTypeException extends Exception { - protected const int UNKNOWN_TYPE = 0; protected const int EMPTY_TYPE = 1; protected const int INVALID_REFLECTION = 2; - protected const int INVALID_TYPE = 3; - - public static function unknownType(?string $type = null): self - { - return new self( - 'Unknown type' . ($type ? " '{$type}'" : ''), - ); - } public static function emptyType(): self { return new self( 'Got empty type', + self::EMPTY_TYPE ); } @@ -29,13 +21,7 @@ public static function invalidReflection(): self { return new self( 'Invalid reflection for type', - ); - } - - public static function invalidType(): self - { - return new self( - 'Invalid type', + self::INVALID_REFLECTION ); } } diff --git a/src/Serializers/DataSerializer.php b/src/Serializers/DataSerializer.php index 547c50e..99500bc 100644 --- a/src/Serializers/DataSerializer.php +++ b/src/Serializers/DataSerializer.php @@ -32,7 +32,7 @@ public static function supportedTypes(): array protected function serializeItem(mixed $item, PropertyContext $property, object $object): ?array { return match (true) { - $item === null && $property->isNullable => null, + is_null($item) && $property->isNullable => null, $item instanceof BaseDataContract => $item->jsonSerialize(), diff --git a/src/Serializers/MixedDataSerializer.php b/src/Serializers/MixedDataSerializer.php new file mode 100644 index 0000000..1856a0a --- /dev/null +++ b/src/Serializers/MixedDataSerializer.php @@ -0,0 +1,53 @@ + + */ + public static function supportedTypes(): array + { + return [ + Type::MIXED, + ]; + } + + /** + * @throws SerializeException + */ + protected function serializeItem(mixed $item, PropertyContext $property, object $object): mixed + { + return match(true) { + is_null($item) && $property->isNullable => null, + + default => is_null($item) + ? throw new SerializeException('Could not serialize array of BaseDataContract items') + : $item, + }; + } + + /** + * @throws DeserializeException + */ + protected function deserializeItem(mixed $item, PropertyContext $property): mixed + { + return match(true) { + is_null($item) && $property->isNullable => null, + + default => is_null($item) + ? throw new DeserializeException('Could not deserialize array of BaseDataContract items') + : $item, + }; + } +} diff --git a/src/Support/Traits/HasSerializers.php b/src/Support/Traits/HasSerializers.php index 0d94c68..89db69e 100644 --- a/src/Support/Traits/HasSerializers.php +++ b/src/Support/Traits/HasSerializers.php @@ -12,6 +12,7 @@ use Nuxtifyts\PhpDto\Serializers\DateTimeSerializer; use Nuxtifyts\PhpDto\Serializers\ScalarTypeSerializer; use Nuxtifyts\PhpDto\Serializers\Serializer; +use Nuxtifyts\PhpDto\Serializers\MixedDataSerializer; trait HasSerializers { @@ -69,6 +70,7 @@ protected static function serializersList(): array DateTimeSerializer::class, BackedEnumSerializer::class, ScalarTypeSerializer::class, + MixedDataSerializer::class ]; } diff --git a/tests/Unit/Serializers/MixedDataSerializerTest.php b/tests/Unit/Serializers/MixedDataSerializerTest.php new file mode 100644 index 0000000..b8ac3f2 --- /dev/null +++ b/tests/Unit/Serializers/MixedDataSerializerTest.php @@ -0,0 +1,19 @@ +