Skip to content

Commit

Permalink
Make Format::serializable recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed Jan 29, 2025
1 parent 5c43aea commit d10188a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function map(iterable $items, callable $callback): array
{
$result = [];

foreach($items as $key => $value) {
foreach ($items as $key => $value) {
$result[$key] = $callback($value, $key);
}

Expand All @@ -36,7 +36,7 @@ public static function mapRecursive(iterable $items, callable $callback): array
{
$result = [];

foreach($items as $key => $value) {
foreach ($items as $key => $value) {
$result[$key] = match (true) {
$value instanceof Html,
!is_iterable($value) => $callback($value, $key),
Expand Down
18 changes: 11 additions & 7 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,18 @@ public static function scalarize(mixed $value): mixed


/**
* @return scalar|stdClass|mixed[]|null
* @return scalar|mixed[]|null
*/
public static function serializable(mixed $value): mixed
{
if (is_scalar($value) || is_array($value)) {
if (is_iterable($value) || $value instanceof stdClass) {
return Arrays::map(
is_iterable($value) ? $value : (array) $value,
fn($v) => static::serializable($v),
);
}

if (is_scalar($value)) {
return $value;
}

Expand All @@ -111,10 +118,10 @@ public static function serializable(mixed $value): mixed
$value instanceof Stringable => $value->__toString(),
$value instanceof UnitEnum => $value->value ?? $value->name,
$value instanceof Identified => $value->getId(),
$value instanceof stdClass => $value,

default => match (true) {
method_exists($value, 'getId') => (int) $value->getId(),

default => null,
},
};
Expand All @@ -125,13 +132,10 @@ public static function stringify(mixed $value): string
{
$value = static::serializable($value);

if ($value instanceof stdClass) {
$value = (array) $value;
}

return match (true) {
is_array($value) => json_encode($value) ?: '',
is_bool($value) => $value ? 'true' : 'false',

default => (string) $value,
};
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Basics/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public function testUnflatten(): void

public function testTokenize(): void
{
$class = (object) ['alpha' => 'A', 'beta' => 'B', 'gamma' => 'G'];
$items = ['first' => ['second' => true, 'third' => 'fourth'], 'fifth' => $class];
$class = ['alpha' => 'A', 'beta' => 'B', 'gamma' => 'G'];
$items = ['first' => ['second' => true, 'third' => 'fourth'], 'fifth' => (object) $class];

Assert::same(
['{first.second}' => true, '{first.third}' => 'fourth', '{fifth}' => $class],
Expand Down
6 changes: 5 additions & 1 deletion tests/Basics/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ public function testSerializable(): void
$items = [
'stdClass' => [
'actual' => $this->object,
'expect' => $this->object,
'expect' => ['type' => 'stdClass'],
],
'datetime' => [
'actual' => DateTime::createFromFormat('Y-m-d H:i:s', '2024-01-01 00:00:00'),
'expect' => '2024-01-01T00:00:00+00:00',
],
'array' => [
'actual' => ['date' => DateTime::createFromFormat('Y-m-d H:i:s', '2024-01-01 00:00:00')],
'expect' => ['date' => '2024-01-01T00:00:00+00:00'],
],
'jsonSerialize' => [
'actual' => $this->jsonSerialize,
'expect' => ['type' => 'class'],
Expand Down

0 comments on commit d10188a

Please sign in to comment.