From 9de69581b3b513e9e4e4fee74c84ff6cb0f1c7d7 Mon Sep 17 00:00:00 2001 From: Andrey Postal Date: Tue, 20 Aug 2024 01:02:27 -0300 Subject: [PATCH] Remove dead code and improve testing --- src/JsonSerializerTrait.php | 45 ++++++++++++++++++++++++++++++++++++- tests/SerializerTest.php | 28 +++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/JsonSerializerTrait.php b/src/JsonSerializerTrait.php index 4ca2fea..f490ae2 100644 --- a/src/JsonSerializerTrait.php +++ b/src/JsonSerializerTrait.php @@ -10,6 +10,7 @@ trait JsonSerializerTrait public function serialize(object $obj): array { $class = new ReflectionClass($obj); + $skipAttributeCheck = ($class->getAttributes(JsonObjectAttribute::class)[0] ?? null) !== null; $output = []; $properties = $class->getProperties(); @@ -24,11 +25,53 @@ public function serialize(object $obj): array $key = $item->key ?? $property->name; if ($property->getType()?->isBuiltin()) { - $output[$key] = $property->getValue($obj); + $output[$key] = $this->handleArray($item, $property->getValue($obj)); + continue; + } + + $class = new ReflectionClass($property->getValue($obj)); + if ($class->isEnum()) { + $output[$key] = $property->getValue($obj)->value; continue; } $output[$key] = $this->serialize($property->getValue($obj)); } return $output; } + + /** + * @param JsonItemAttribute $item + * @param mixed $value + * @return mixed + * @throws ReflectionException + * @throws JsonException + * + * @noinspection GetTypeMissUseInspection + */ + private function handleArray(JsonItemAttribute $item, mixed $value): mixed + { + if (gettype($value) !== 'array') { + return $value; + } + + if (!class_exists($item->type)) { + return $value; + } + $class = new ReflectionClass($item->type); + $isEnum = $class->isEnum(); + + return array_reduce( + array: $value, + callback: function(array $l, mixed $c) use ($isEnum): array { + if ($isEnum) { + $v = $c->value; + } else { + $v = $this->serialize($c); + } + $l[] = $v; + return $l; + }, + initial: [], + ); + } } diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 18e659b..02199db 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -96,6 +96,34 @@ public function testSerializeWithExtraItems(): void /** * @throws JsonException */ + public function testArrayOfChild(): void + { + $obj = new WithArrayOfChildObject(); + + $handler = new JsonHandler(); + $arr = $handler->serialize($obj); + + $this->assertArrayHasKey('id', $arr); + $this->assertArrayHasKey('children', $arr); + $this->assertIsArray($arr['children']); + $this->assertCount(2, $arr['children']); + $this->assertIsArray($arr['children'][0]); + + $this->assertArrayHasKey('string', $arr['children'][1]); + $this->assertArrayHasKey('int', $arr['children'][1]); + $this->assertIsInt($arr['children'][1]['int']); + $this->assertEquals(11, $arr['children'][1]['int']); + + $this->assertIsFloat($arr['children'][1]['float']); + $this->assertEquals(11.50, $arr['children'][1]['float']); + + JsonHandler::Encode($arr); + } + + /** + * @throws JsonException + * @throws ReflectionException + */ public function testSerializeWithObjectAttr(): void { $this->assertSimpleSerializedObject(new SimpleTestWithObjectAttr());