Skip to content

Commit

Permalink
fix: properly handle ArrayObject normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Mar 17, 2024
1 parent f731586 commit 4f555d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Normalizer/Transformer/RecursiveTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CuyZ\Valinor\Normalizer\Transformer;

use ArrayObject;
use BackedEnum;
use Closure;
use CuyZ\Valinor\Definition\AttributeDefinition;
Expand Down Expand Up @@ -111,7 +112,7 @@ private function defaultTransformer(mixed $value, WeakMap $references): mixed
return $value->getName();
}

if ($value::class === stdClass::class) {
if ($value::class === stdClass::class || $value instanceof ArrayObject) {
return array_map(
fn (mixed $value) => $this->doTransform($value, $references),
(array)$value
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/Normalizer/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CuyZ\Valinor\Tests\Integration\Normalizer;

use ArrayObject;
use Attribute;
use CuyZ\Valinor\Normalizer\Exception\CircularReferenceFoundDuringNormalization;
use CuyZ\Valinor\Normalizer\Exception\KeyTransformerHasTooManyParameters;
Expand Down Expand Up @@ -178,6 +179,24 @@ public static function normalize_basic_values_yields_expected_output_data_provid
'expected json' => '{"foo":"foo","bar":"bar"}',
];

yield 'ArrayObject' => [
'input' => new ArrayObject(['foo' => 'foo', 'bar' => 'bar']),
'expected array' => [
'foo' => 'foo',
'bar' => 'bar',
],
'expected json' => '{"foo":"foo","bar":"bar"}',
];

yield 'class inheriting ArrayObject' => [
'input' => new class (['foo' => 'foo', 'bar' => 'bar']) extends ArrayObject {},
'expected array' => [
'foo' => 'foo',
'bar' => 'bar',
],
'expected json' => '{"foo":"foo","bar":"bar"}',
];

yield 'array of object' => [
'input' => [
'foo' => new BasicObject('foo'),
Expand Down

0 comments on commit 4f555d9

Please sign in to comment.