Skip to content

Commit ad35d37

Browse files
committed
Add MixedDataSerializer to handle mixed property types
1 parent f84d04e commit ad35d37

File tree

7 files changed

+79
-18
lines changed

7 files changed

+79
-18
lines changed

src/Contexts/TypeContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static function getInstances(PropertyContext $property): array
109109
);
110110
break;
111111
default:
112-
throw UnsupportedTypeException::unknownType($type);
112+
$instances[] = new static(Type::MIXED);
113113
}
114114
}
115115

src/Enums/Property/Type.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum Type: string
1212
case DATETIME = 'datetime';
1313
case DATA = 'data';
1414
case ARRAY = 'array';
15+
case MIXED = 'mixed';
1516

1617
/** @var list<Type> */
1718
public const array SCALAR_TYPES = [

src/Exceptions/UnsupportedTypeException.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,22 @@
66

77
class UnsupportedTypeException extends Exception
88
{
9-
protected const int UNKNOWN_TYPE = 0;
109
protected const int EMPTY_TYPE = 1;
1110
protected const int INVALID_REFLECTION = 2;
12-
protected const int INVALID_TYPE = 3;
13-
14-
public static function unknownType(?string $type = null): self
15-
{
16-
return new self(
17-
'Unknown type' . ($type ? " '{$type}'" : ''),
18-
);
19-
}
2011

2112
public static function emptyType(): self
2213
{
2314
return new self(
2415
'Got empty type',
16+
self::EMPTY_TYPE
2517
);
2618
}
2719

2820
public static function invalidReflection(): self
2921
{
3022
return new self(
3123
'Invalid reflection for type',
32-
);
33-
}
34-
35-
public static function invalidType(): self
36-
{
37-
return new self(
38-
'Invalid type',
24+
self::INVALID_REFLECTION
3925
);
4026
}
4127
}

src/Serializers/DataSerializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function supportedTypes(): array
3232
protected function serializeItem(mixed $item, PropertyContext $property, object $object): ?array
3333
{
3434
return match (true) {
35-
$item === null && $property->isNullable => null,
35+
is_null($item) && $property->isNullable => null,
3636

3737
$item instanceof BaseDataContract => $item->jsonSerialize(),
3838

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Serializers;
4+
5+
use Nuxtifyts\PhpDto\Contracts\SerializesArrayOfItems as SerializesArrayOfItemsContract;
6+
use Nuxtifyts\PhpDto\Concerns\SerializesArrayOfItems;
7+
use Nuxtifyts\PhpDto\Contexts\PropertyContext;
8+
use Nuxtifyts\PhpDto\Enums\Property\Type;
9+
use Nuxtifyts\PhpDto\Exceptions\DeserializeException;
10+
use Nuxtifyts\PhpDto\Exceptions\SerializeException;
11+
12+
class MixedDataSerializer extends Serializer implements SerializesArrayOfItemsContract
13+
{
14+
use SerializesArrayOfItems;
15+
16+
/**
17+
* @return list<Type>
18+
*/
19+
public static function supportedTypes(): array
20+
{
21+
return [
22+
Type::MIXED,
23+
];
24+
}
25+
26+
/**
27+
* @throws SerializeException
28+
*/
29+
protected function serializeItem(mixed $item, PropertyContext $property, object $object): mixed
30+
{
31+
return match(true) {
32+
is_null($item) && $property->isNullable => null,
33+
34+
default => is_null($item)
35+
? throw new SerializeException('Could not serialize array of BaseDataContract items')
36+
: $item,
37+
};
38+
}
39+
40+
/**
41+
* @throws DeserializeException
42+
*/
43+
protected function deserializeItem(mixed $item, PropertyContext $property): mixed
44+
{
45+
return match(true) {
46+
is_null($item) && $property->isNullable => null,
47+
48+
default => is_null($item)
49+
? throw new DeserializeException('Could not deserialize array of BaseDataContract items')
50+
: $item,
51+
};
52+
}
53+
}

src/Support/Traits/HasSerializers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Nuxtifyts\PhpDto\Serializers\DateTimeSerializer;
1313
use Nuxtifyts\PhpDto\Serializers\ScalarTypeSerializer;
1414
use Nuxtifyts\PhpDto\Serializers\Serializer;
15+
use Nuxtifyts\PhpDto\Serializers\MixedDataSerializer;
1516

1617
trait HasSerializers
1718
{
@@ -69,6 +70,7 @@ protected static function serializersList(): array
6970
DateTimeSerializer::class,
7071
BackedEnumSerializer::class,
7172
ScalarTypeSerializer::class,
73+
MixedDataSerializer::class
7274
];
7375
}
7476

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Serializers;
4+
5+
use Nuxtifyts\PhpDto\Contexts\PropertyContext;
6+
use Nuxtifyts\PhpDto\Contexts\TypeContext;
7+
use Nuxtifyts\PhpDto\Serializers\MixedDataSerializer;
8+
use Nuxtifyts\PhpDto\Serializers\Serializer;
9+
use Nuxtifyts\PhpDto\Tests\Unit\UnitCase;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
12+
#[CoversClass(Serializer::class)]
13+
#[CoversClass(MixedDataSerializer::class)]
14+
#[CoversClass(PropertyContext::class)]
15+
#[CoversClass(TypeContext::class)]
16+
final class MixedDataSerializerTest extends UnitCase
17+
{
18+
19+
}

0 commit comments

Comments
 (0)