diff --git a/README.md b/README.md index 021326c..ab69467 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,21 @@ class MyObject { } ``` +You can also combine both when need to add custom key or if you want to make an item required. +In the case of the entire object being a JsonObject with a direct 1:1 match (or perfect mirror of the keys), you can use the ``JsonObjectAttribute`` +```php +use \Andrey\JsonHandler\Attributes\JsonObjectAttribute; +use \Andrey\JsonHandler\Attributes\JsonItemAttribute; + +// { "id": 123, "custom_name": "my name" } +#[JsonObjectAttribute] +class MyObject { + public int $id; + #[JsonItemAttribute(key: 'custom_name')] + public string $name; +} +``` + If your **Value Object** has some property that **won't be present** in the JSON, you can just omit the attribute for it and the other ones will be processed normally. ```php diff --git a/src/JsonHydratorTrait.php b/src/JsonHydratorTrait.php index d5939d0..d0a5807 100644 --- a/src/JsonHydratorTrait.php +++ b/src/JsonHydratorTrait.php @@ -63,7 +63,7 @@ private function processProperty(ReflectionProperty $property, array $jsonArr, b } /** @var JsonItemAttribute $item */ - $item = $skipAttributeCheck ? new JsonItemAttribute() : $attr->newInstance(); + $item = $attr?->newInstance() ?? new JsonItemAttribute(); $key = $item->key ?? $property->getName(); if ($item->required && !array_key_exists($key, $jsonArr)) { throw new InvalidArgumentException(sprintf('required item <%s> not found', $key)); diff --git a/src/JsonSerializerTrait.php b/src/JsonSerializerTrait.php index 88844fc..4ca2fea 100644 --- a/src/JsonSerializerTrait.php +++ b/src/JsonSerializerTrait.php @@ -20,7 +20,7 @@ public function serialize(object $obj): array continue; } /** @var JsonItemAttribute $item */ - $item = $skipAttributeCheck ? new JsonItemAttribute() : $attr->newInstance(); + $item = $attr?->newInstance() ?? new JsonItemAttribute(); $key = $item->key ?? $property->name; if ($property->getType()?->isBuiltin()) { diff --git a/tests/HydratorTest.php b/tests/HydratorTest.php index e529ee7..f2f702b 100644 --- a/tests/HydratorTest.php +++ b/tests/HydratorTest.php @@ -169,4 +169,21 @@ public function testHydrateWithObjectAttr(): void $this->assertEquals(1.5, $obj->float); $this->assertFalse($obj->bool); } + + /** + * @throws JsonException + */ + public function testMixedObjectAndItemAttributes(): void + { + $json = '{"string": "str", "int": 1, "float": 1.50, "bool": false}'; + $obj = new MixedAttributesObject(); + + $handler = new JsonHandler(); + $handler->hydrateObject($json, $obj); + + $this->assertEquals('str', $obj->string); + $this->assertEquals(1, $obj->int); + $this->assertEquals(1.5, $obj->float); + $this->assertFalse($obj->bool); + } } diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 356e824..18e659b 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -101,6 +101,14 @@ public function testSerializeWithObjectAttr(): void $this->assertSimpleSerializedObject(new SimpleTestWithObjectAttr()); } + /** + * @throws JsonException + */ + public function testSerializeWithMixedAttrs(): void + { + $this->assertSimpleSerializedObject(new MixedAttributesObject()); + } + /** * @throws JsonException */ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 46fd167..1cb0c29 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -6,4 +6,4 @@ require_once __DIR__ . '/utils/SimpleTestWithArrayObject.php'; require_once __DIR__ . '/utils/WithArrayOfChildObject.php'; require_once __DIR__ . '/utils/SimpleTestWithObjectAttr.php'; - +require_once __DIR__ . '/utils/MixedAttributesObject.php'; diff --git a/tests/utils/MixedAttributesObject.php b/tests/utils/MixedAttributesObject.php new file mode 100644 index 0000000..8161a98 --- /dev/null +++ b/tests/utils/MixedAttributesObject.php @@ -0,0 +1,14 @@ +