Skip to content

Commit

Permalink
new "JsonComplete" attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
peterGdot committed Nov 7, 2024
1 parent cab09f1 commit 3cc7167
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ composer.lock
vendor/
.vscode/
notes.md
demos2/
3 changes: 2 additions & 1 deletion src/Attributes/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Attribute that allows overwriting individual property names.
*/
#[\Attribute]
class Json {
class Json
{
public function __construct(public string $name) {}
}
11 changes: 11 additions & 0 deletions src/Attributes/JsonComplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Pyther\Json\Attributes;

/**
* Attribute that allows pre- and post processing.
*/
#[\Attribute]
class JsonComplete
{
public function __construct() {}
}
3 changes: 2 additions & 1 deletion src/Attributes/JsonDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Attribute that allows overwriting individual datetime formats.
*/
#[\Attribute]
class JsonDateTime {
class JsonDateTime
{
public function __construct(public string $format) {}
}
3 changes: 2 additions & 1 deletion src/Attributes/JsonEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Attribute that allows overwriting individual enum formats.
*/
#[\Attribute]
class JsonEnum {
class JsonEnum
{
public function __construct(public EnumFormat $format = EnumFormat::Value) {}
}
3 changes: 2 additions & 1 deletion src/Attributes/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Attribute that allows overwriting individual property types. This is especially useful for array types.
*/
#[\Attribute]
class JsonType {
class JsonType
{
public function __construct(public ?string $type) {}
}
30 changes: 29 additions & 1 deletion src/JsonDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Pyther\Json;

use Exception;
use Pyther\Json\Attributes\JsonComplete;
use Pyther\Json\Attributes\JsonDateTime;
use Pyther\Json\Exceptions\JsonException;

Expand Down Expand Up @@ -136,6 +137,8 @@ private function fillObject(object $object, array $data): ?object
$this->setValue($object, $value, $prop);
}

$this->finalize($object);

return $object;
}

Expand Down Expand Up @@ -205,7 +208,32 @@ private function getEnum(string $enumClass, mixed $value, ?string $propertyName
} catch (Exception $ex) {
throw new JsonException($ex->getMessage(), $propertyName);
}

}

/**
* Find and invoke the objects #[JsonFinalize] method (if any).
*
* @param [type] $object
* @return void
*/
public function finalize($object)
{
$reflObject = new \ReflectionObject($object);
$methods = $reflObject->getMethods();

foreach ($methods as $method)
{
$attributes = $method->getAttributes(JsonComplete::class);
if (count($attributes) > 0) {
$methodName = $method->getName();
$method = $reflObject->getMethod($methodName);
if (version_compare(PHP_VERSION, '8.1.0') < 0) {
$method->setAccessible(true);
}
$method->invoke($object);
break;
}
}
}

}

0 comments on commit 3cc7167

Please sign in to comment.