Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Sep 23, 2024
1 parent 8a70133 commit 9d5f848
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function from(array|object $data): static
$value = $rule['mutate']($value);
}

if (isset($rule['array_of'])) {
if (isset($rule['array_of']) && isset($value)) {
$arrayOf = $rule['array_of'];
foreach ($value as &$item) {
if ($item instanceof $arrayOf) {
Expand Down Expand Up @@ -101,7 +101,15 @@ public static function from(array|object $data): static
*/
public function toArray(): array
{
return $this->resolveToArray((array) $this, static::getRules(new \ReflectionClass(static::class)));
$result = $this->resolveToArray((array) $this, static::getRules(new \ReflectionClass(static::class)));

foreach ((new \ReflectionClass(static::class))->getAttributes() as $attribute) {
if ($attribute->getName() == \AnourValar\LaravelAtom\Mapper\Jsonb::class) {
$result = $this->sort($result);
}
}

return $result;
}

/**
Expand Down Expand Up @@ -210,6 +218,11 @@ public function set(Model $model, string $key, mixed $value, array $attributes)

return json_encode(($this->class)::from($value)->toArray(), JSON_UNESCAPED_UNICODE);
}

public function serialize(Model $model, string $key, mixed $value, array $attributes): array
{
return $value->toArray();
}
};
}

Expand Down Expand Up @@ -342,4 +355,33 @@ protected static function getArg(\ReflectionAttribute $attribute)

return array_shift($args);
}

/**
* @param mixed $value
* @return mixed
*/
protected function sort(mixed $value): mixed
{
if (is_array($value) && ! array_is_list($value)) {
uksort($value, function ($a, $b) {
$strlenA = mb_strlen($a);
$strlenB = mb_strlen($b);

if ($strlenA == $strlenB) {
return $a <=> $b;
}

return $strlenA <=> $strlenB;
});
}

if (is_array($value)) {
foreach ($value as &$item) {
$item = $this->sort($item);
}
unset($item);
}

return $value;
}
}
10 changes: 10 additions & 0 deletions src/Mapper/Jsonb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace AnourValar\LaravelAtom\Mapper;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class Jsonb
{
}
3 changes: 3 additions & 0 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,8 @@ public function test_model()
$post->forceFill(['data' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => '4']]);
$this->assertInstanceOf(SimpleMapper::class, $post->data);
$this->assertSame(['a' => '1', 'b' => 2, 'c' => '3', 'd' => 4], $post->data->toArray());

$this->assertSame(['data' => ['a' => '1', 'b' => 2, 'c' => '3', 'd' => 4]], $post->toArray());
$this->assertSame(['data' => json_encode(['a' => '1', 'b' => 2, 'c' => '3', 'd' => 4])], $post->getAttributes());
}
}
2 changes: 2 additions & 0 deletions tests/Mappers/SimpleMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use AnourValar\LaravelAtom\Mapper;
use AnourValar\LaravelAtom\Mapper\Mapping;
use AnourValar\LaravelAtom\Mapper\MappingSnakeCase;
use AnourValar\LaravelAtom\Mapper\Jsonb;

#[Jsonb]
class SimpleMapper extends Mapper
{
public function __construct(
Expand Down

0 comments on commit 9d5f848

Please sign in to comment.