Skip to content

Commit

Permalink
Merge pull request #2 from andreypostal/mixed-attributes-usage
Browse files Browse the repository at this point in the history
Allow mixed attributes usage (both object and item)
  • Loading branch information
andreypostal authored Aug 18, 2024
2 parents 49c6dae + a9b08cd commit 72dc219
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/JsonHydratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSerializerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
17 changes: 17 additions & 0 deletions tests/HydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public function testSerializeWithObjectAttr(): void
$this->assertSimpleSerializedObject(new SimpleTestWithObjectAttr());
}

/**
* @throws JsonException
*/
public function testSerializeWithMixedAttrs(): void
{
$this->assertSimpleSerializedObject(new MixedAttributesObject());
}

/**
* @throws JsonException
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
14 changes: 14 additions & 0 deletions tests/utils/MixedAttributesObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use Andrey\JsonHandler\Attributes\JsonItemAttribute;
use Andrey\JsonHandler\Attributes\JsonObjectAttribute;

#[JsonObjectAttribute]
class MixedAttributesObject
{
public string $string = 'string';
public ?int $int = 11;
public ?float $float = 11.50;
#[JsonItemAttribute(required: true)]
public ?bool $bool = true;
}

0 comments on commit 72dc219

Please # to comment.