Skip to content

Commit

Permalink
json force object
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Dec 10, 2024
1 parent fc788e8 commit 41631fc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/orm/src/Attributes/ForceObjectJsonSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Windwalker\ORM\Attributes;

use Windwalker\Utilities\TypeCast;

/**
* The ForceObjectSerializer class.
*/
#[\Attribute]
class ForceObjectJsonSerializer implements JsonSerializerInterface
{
public function __construct(
public bool $deep = false,
public bool $nullable = false,
public string $class = \stdClass::class
) {
}

public function serialize(mixed $data): mixed
{
if ($this->nullable && $data === null) {
return null;
}

return TypeCast::toObject($data, $this->deep, $this->class);
}
}
11 changes: 11 additions & 0 deletions packages/orm/src/Attributes/JsonObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Windwalker\ORM\Attributes;

#[\Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_PROPERTY)]
class JsonObject extends Cast
{

}
4 changes: 2 additions & 2 deletions packages/orm/src/Attributes/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* The JsonSerializer class.
*/
#[\Attribute]
class JsonSerializer
class JsonSerializer implements JsonSerializerInterface
{
public function __construct(public mixed $handler)
{
Expand All @@ -20,7 +20,7 @@ public function serialize(mixed $data): mixed
{
$handler = $this->handler;

if (is_a($handler, CastInterface::class)) {
if (is_a($handler, CastInterface::class, true)) {
$handler = new ($this->handler)();
}

Expand Down
10 changes: 10 additions & 0 deletions packages/orm/src/Attributes/JsonSerializerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Windwalker\ORM\Attributes;

interface JsonSerializerInterface
{
public function serialize(mixed $data): mixed;
}
3 changes: 2 additions & 1 deletion packages/orm/src/EntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Windwalker\Attributes\AttributesAccessor;
use Windwalker\Data\Collection;
use Windwalker\ORM\Attributes\JsonSerializer;
use Windwalker\ORM\Attributes\JsonSerializerInterface;
use Windwalker\ORM\Attributes\Table;
use Windwalker\ORM\Relation\RelationCollection;
use Windwalker\ORM\Relation\RelationProxies;
Expand Down Expand Up @@ -112,7 +113,7 @@ public function jsonSerialize(): array
$prop = new ReflectionProperty($this, $key);
$attrs = AttributesAccessor::getAttributesFromAny(
$prop,
JsonSerializer::class,
JsonSerializerInterface::class,
ReflectionAttribute::IS_INSTANCEOF
);

Expand Down

0 comments on commit 41631fc

Please sign in to comment.