Skip to content

Commit c74ead9

Browse files
committed
Add BackedEnumSerializer to serialize enums
1 parent d524680 commit c74ead9

File tree

6 files changed

+197
-1
lines changed

6 files changed

+197
-1
lines changed

src/BackedEnumSerializer.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Aternos\Serializer;
4+
5+
use Aternos\Serializer\Exceptions\UnsupportedInputObjectException;
6+
use BackedEnum;
7+
use UnitEnum;
8+
9+
class BackedEnumSerializer implements SerializerInterface
10+
{
11+
/**
12+
* @param BackedEnum|UnitEnum|mixed $item
13+
* @return string
14+
* @noinspection PhpUnhandledExceptionInspection,PhpDocMissingThrowsInspection
15+
*/
16+
public function serialize(object $item): string
17+
{
18+
if (!$item instanceof BackedEnum) {
19+
throw new UnsupportedInputObjectException(get_debug_type($item),
20+
"Only BackedEnum and UnitEnum are supported by EnumSerializer."
21+
);
22+
}
23+
return $item->value;
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Aternos\Serializer\Exceptions;
4+
5+
/**
6+
* An exception that is thrown when the object used as input to a serializer is not supported by the serializer.
7+
*/
8+
class UnsupportedInputObjectException extends SerializationException
9+
{
10+
public function __construct(
11+
protected string $type,
12+
string $reason = null,
13+
)
14+
{
15+
$message = "Unsupported input object '" . $type . "'";
16+
if ($reason !== null) {
17+
$message .= ": " . $reason;
18+
}
19+
parent::__construct($message);
20+
}
21+
22+
public function getType(): string
23+
{
24+
return $this->type;
25+
}
26+
}

tests/src/ArraySerializeTests.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Aternos\Serializer\Test\Src;
4+
5+
use Aternos\Serializer\ArraySerializer;
6+
use Aternos\Serializer\BackedEnumSerializer;
7+
use Aternos\Serializer\Serialize;
8+
9+
class ArraySerializeTests
10+
{
11+
/** @noinspection PhpMissingFieldTypeInspection */
12+
#[Serialize(itemType: BuiltInTypeTestClass::class, itemSerializer: new ArraySerializer())]
13+
public $untypedArray = [];
14+
15+
#[Serialize(itemSerializer: new ArraySerializer())]
16+
public array $array = [];
17+
18+
#[Serialize(itemType: BuiltInTypeTestClass::class, itemSerializer: new ArraySerializer())]
19+
public array $typedArray = [];
20+
21+
#[Serialize(itemSerializer: new BackedEnumSerializer())]
22+
protected array $backedEnumArray = [
23+
TestBackedEnum::A,
24+
];
25+
26+
#[Serialize]
27+
protected array $stringArray = [
28+
"",
29+
];
30+
31+
#[Serialize]
32+
protected array $intArray = [
33+
0,
34+
];
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Aternos\Serializer\Test\Tests;
4+
5+
use Aternos\Serializer\BackedEnumSerializer;
6+
use Aternos\Serializer\Exceptions\UnsupportedInputObjectException;
7+
use Aternos\Serializer\Test\Src\TestBackedEnum;
8+
use Aternos\Serializer\Test\Src\TestClass;
9+
use PHPUnit\Framework\Attributes\CoversClass;
10+
use PHPUnit\Framework\Attributes\UsesClass;
11+
use PHPUnit\Framework\TestCase;
12+
13+
#[CoversClass(BackedEnumSerializer::class)]
14+
#[UsesClass(UnsupportedInputObjectException::class)]
15+
class BackedEnumSerializerTest extends TestCase
16+
{
17+
public function testSerialize()
18+
{
19+
$serializer = new BackedEnumSerializer();
20+
21+
$this->assertSame("a", $serializer->serialize(TestBackedEnum::A));
22+
$this->assertSame("b", $serializer->serialize(TestBackedEnum::B));
23+
$this->assertSame("c", $serializer->serialize(TestBackedEnum::C));
24+
}
25+
26+
public function testSerializeInvalidInput()
27+
{
28+
$this->expectException(UnsupportedInputObjectException::class);
29+
$this->expectExceptionMessage("Unsupported input object 'Aternos\Serializer\Test\Src\TestClass': Only BackedEnum and UnitEnum are supported by EnumSerializer.");
30+
$serializer = new BackedEnumSerializer();
31+
$serializer->serialize(new TestClass());
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Aternos\Serializer\Test\Tests\Exceptions;
4+
5+
use Aternos\Serializer\Exceptions\UnsupportedInputObjectException;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\TestCase;
8+
9+
#[CoversClass(UnsupportedInputObjectException::class)]
10+
class UnsupportedInputObjectExceptionTest extends TestCase
11+
{
12+
public function testConstruct(): void
13+
{
14+
$exception = new UnsupportedInputObjectException("TestClass");
15+
$this->assertSame("Unsupported input object 'TestClass'", $exception->getMessage());
16+
}
17+
18+
public function testConstructWithReason(): void
19+
{
20+
$exception = new UnsupportedInputObjectException("TestClass", "test reason");
21+
$this->assertSame("Unsupported input object 'TestClass': test reason", $exception->getMessage());
22+
}
23+
24+
public function testGetType(): void
25+
{
26+
$exception = new UnsupportedInputObjectException("TestClass");
27+
$this->assertSame("TestClass", $exception->getType());
28+
}
29+
}

tests/tests/SerializerTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
namespace Aternos\Serializer\Test\Tests;
44

55
use Aternos\Serializer\ArraySerializer;
6+
use Aternos\Serializer\BackedEnumSerializer;
67
use Aternos\Serializer\Exceptions\IncorrectTypeException;
78
use Aternos\Serializer\Exceptions\MissingPropertyException;
89
use Aternos\Serializer\Json\PropertyJsonSerializer;
910
use Aternos\Serializer\Serialize;
11+
use Aternos\Serializer\Test\Src\ArraySerializeTests;
1012
use Aternos\Serializer\Test\Src\BackedEnumTestClass;
1113
use Aternos\Serializer\Test\Src\BuiltInTypeTestClass;
1214
use Aternos\Serializer\Test\Src\CustomSerializerTestClass;
1315
use Aternos\Serializer\Test\Src\DefaultValueTestClass;
1416
use Aternos\Serializer\Test\Src\EnumTestClass;
1517
use Aternos\Serializer\Test\Src\SecondTestClass;
1618
use Aternos\Serializer\Test\Src\SerializerTestClass;
17-
use Aternos\Serializer\Test\Src\TestBackedEnum;
1819
use Aternos\Serializer\Test\Src\TestClass;
1920
use PHPUnit\Framework\Attributes\CoversClass;
2021
use PHPUnit\Framework\Attributes\UsesClass;
@@ -25,6 +26,7 @@
2526
#[UsesClass(Serialize::class)]
2627
#[UsesClass(IncorrectTypeException::class)]
2728
#[UsesClass(MissingPropertyException::class)]
29+
#[UsesClass(BackedEnumSerializer::class)]
2830
class SerializerTest extends TestCase
2931
{
3032
public function testSerialize(): void
@@ -156,4 +158,50 @@ public function testSerializeUnbackedEnum(): void
156158
$this->expectExceptionMessage("Expected 'enum' to be 'BackedEnum' found: \Aternos\Serializer\Test\Src\TestEnum::A");
157159
$serializer->serialize(new EnumTestClass());
158160
}
161+
162+
public function testSerializeArrayItems(): void
163+
{
164+
$serializer = new ArraySerializer();
165+
$testClass = new ArraySerializeTests();
166+
167+
$testClass->untypedArray = [new BuiltInTypeTestClass()];
168+
$testClass->typedArray = [new BuiltInTypeTestClass()];
169+
$testClass->array = [new BuiltInTypeTestClass()];
170+
171+
$this->assertEquals([
172+
"untypedArray" => [[
173+
"int" => null,
174+
"float" => null,
175+
"string" => null,
176+
"array" => null,
177+
"object" => null,
178+
"self" => null,
179+
"false" => null,
180+
"true" => null,
181+
]],
182+
"array" => [[
183+
"int" => null,
184+
"float" => null,
185+
"string" => null,
186+
"array" => null,
187+
"object" => null,
188+
"self" => null,
189+
"false" => null,
190+
"true" => null,
191+
]],
192+
"typedArray" => [[
193+
"int" => null,
194+
"float" => null,
195+
"string" => null,
196+
"array" => null,
197+
"object" => null,
198+
"self" => null,
199+
"false" => null,
200+
"true" => null,
201+
]],
202+
"backedEnumArray" => ["a"],
203+
"stringArray" => [""],
204+
"intArray" => [0],
205+
], $serializer->serialize($testClass));
206+
}
159207
}

0 commit comments

Comments
 (0)