Skip to content

Add MixedDataSerializer to handle mixed property types #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Contexts/TypeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function getInstances(PropertyContext $property): array
);
break;
default:
throw UnsupportedTypeException::unknownType($type);
$instances[] = new static(Type::MIXED);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Enums/Property/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum Type: string
case DATETIME = 'datetime';
case DATA = 'data';
case ARRAY = 'array';
case MIXED = 'mixed';

/** @var list<Type> */
public const array SCALAR_TYPES = [
Expand Down
18 changes: 2 additions & 16 deletions src/Exceptions/UnsupportedTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,22 @@

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
);
}

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
);
}
}
2 changes: 1 addition & 1 deletion src/Serializers/DataSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
53 changes: 53 additions & 0 deletions src/Serializers/MixedDataSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Nuxtifyts\PhpDto\Serializers;

use Nuxtifyts\PhpDto\Contracts\SerializesArrayOfItems as SerializesArrayOfItemsContract;
use Nuxtifyts\PhpDto\Concerns\SerializesArrayOfItems;
use Nuxtifyts\PhpDto\Contexts\PropertyContext;
use Nuxtifyts\PhpDto\Enums\Property\Type;
use Nuxtifyts\PhpDto\Exceptions\DeserializeException;
use Nuxtifyts\PhpDto\Exceptions\SerializeException;

class MixedDataSerializer extends Serializer implements SerializesArrayOfItemsContract
{
use SerializesArrayOfItems;

/**
* @return list<Type>
*/
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,
};
}
}
2 changes: 2 additions & 0 deletions src/Support/Traits/HasSerializers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -69,6 +70,7 @@ protected static function serializersList(): array
DateTimeSerializer::class,
BackedEnumSerializer::class,
ScalarTypeSerializer::class,
MixedDataSerializer::class
];
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Serializers/MixedDataSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Nuxtifyts\PhpDto\Tests\Unit\Serializers;

use Nuxtifyts\PhpDto\Contexts\PropertyContext;
use Nuxtifyts\PhpDto\Contexts\TypeContext;
use Nuxtifyts\PhpDto\Serializers\MixedDataSerializer;
use Nuxtifyts\PhpDto\Serializers\Serializer;
use Nuxtifyts\PhpDto\Tests\Unit\UnitCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(Serializer::class)]
#[CoversClass(MixedDataSerializer::class)]
#[CoversClass(PropertyContext::class)]
#[CoversClass(TypeContext::class)]
final class MixedDataSerializerTest extends UnitCase
{

}
Loading