diff --git a/src/Data/Base/ToArrayObject.php b/src/Data/Base/ToArrayObject.php index dba550b2..e54c465e 100644 --- a/src/Data/Base/ToArrayObject.php +++ b/src/Data/Base/ToArrayObject.php @@ -51,7 +51,7 @@ private function mapPropertyValue( return match (true) { $has_mapper => $this->mapWithMapper($property, $value), $value instanceof Data => $this->mapWithData($case, $value), - $value instanceof TypedCollection => $value->toArrayObject(), + $value instanceof TypedCollection => $value->toArrayObject(case: $case), $value instanceof Collection => $this->mapWithCollection($case, $value), $value instanceof CarbonImmutable => $this->mapWithCarbonImmutable($value), $value instanceof BackedEnum => $value->value, @@ -74,7 +74,13 @@ private function mapWithData(string $case, Data $value): \ArrayObject private function mapWithCollection(string $case, Collection $value): array { return $value->map(function ($item) use ($case) { - return $this->toArrayObject($item, $case); + return match (true) { + $item instanceof TypedCollection => $item->toArrayObject(case: $case), + $item instanceof Collection => $item->toArrayObject(), + $item instanceof Data => $item->toArrayObject(case: $case), + $item instanceof BackedEnum => $item->value, + default => $item, + }; })->toArray(); } diff --git a/src/Data/Base/TypedCollection.php b/src/Data/Base/TypedCollection.php index f8308cc6..9c4bcbcf 100644 --- a/src/Data/Base/TypedCollection.php +++ b/src/Data/Base/TypedCollection.php @@ -56,13 +56,14 @@ public function reduce(callable $callback, $initial = null) return $this->toBase()->reduce($callback, $initial); } - public function toArrayObject(): \ArrayObject - { - return new \ArrayObject($this->map(function ($item) { + public function toArrayObject( + string $case = 'camel', + ): \ArrayObject { + return new \ArrayObject($this->map(function ($item) use ($case) { return match (true) { - $item instanceof TypedCollection => $item->toArrayObject(), + $item instanceof TypedCollection => $item->toArrayObject(case: $case), $item instanceof Collection => $item->toArrayObject(), - $item instanceof Data => $item->toArrayObject(), + $item instanceof Data => $item->toArrayObject(case: $case), $item instanceof BackedEnum => $item->value, default => $item, }; diff --git a/src/Data/Schemas/ListingsItems/AttributesListSchema.php b/src/Data/Schemas/ListingsItems/AttributesListSchema.php index 4e5b509a..dd91188d 100644 --- a/src/Data/Schemas/ListingsItems/AttributesListSchema.php +++ b/src/Data/Schemas/ListingsItems/AttributesListSchema.php @@ -11,7 +11,7 @@ class AttributesListSchema extends TypedCollection { public const ITEM_CLASS = AttributeSchema::class; - public function toArrayObject(): \ArrayObject + public function toArrayObject(string $case = 'camel'): \ArrayObject { $array_object = new \ArrayObject(); $attribute_names = $this->pluck('name')->unique()->toArray(); diff --git a/tests/Unit/Data/Base/ToArrayObjectTest.php b/tests/Unit/Data/Base/ToArrayObjectTest.php index 759f86a1..1005866a 100644 --- a/tests/Unit/Data/Base/ToArrayObjectTest.php +++ b/tests/Unit/Data/Base/ToArrayObjectTest.php @@ -2,9 +2,10 @@ namespace Jasara\AmznSPA\Tests\Unit\Data\Base; +use Illuminate\Support\Collection; +use Jasara\AmznSPA\Data\Base\Data; use Jasara\AmznSPA\Data\Base\ToArrayObject; use Jasara\AmznSPA\Data\Requests\FulfillmentInbound\PutTransportDetailsRequest; -use Jasara\AmznSPA\Data\Requests\ListingsItems\ListingsItemPatchRequest; use Jasara\AmznSPA\Data\Schemas\FulfillmentInbound\v0\PartneredLtlDataInputSchema; use Jasara\AmznSPA\Data\Schemas\MerchantFulfillment\AdditionalSellerInputSchema; use Jasara\AmznSPA\Data\Schemas\Notifications\ProcessingDirectiveSchema; @@ -27,22 +28,12 @@ public function testReturnsDataUsingMappers(): void public function testReturnsDataUsingCollection(): void { - $data = ListingsItemPatchRequest::from([ - 'product_type' => 'test', - 'patches' => [ - [ - 'op' => 'add', - 'path' => '/path', - ], - ], - ]); + $test = new Collection(['abc']); + $data = new CollectionTest($test); $array_object = $data->toArrayObject(); - $this->assertEquals('test', $array_object['productType']); - $this->assertCount(1, $array_object['patches']); - $this->assertEquals('add', $array_object['patches'][0]['op']); - $this->assertEquals('/path', $array_object['patches'][0]['path']); + $this->assertEquals('abc', $array_object['test'][0]); } public function testReturnsDataUsingCarbonImmutable(): void @@ -82,3 +73,11 @@ public function testReturnsDataUsingPascalCase(): void $this->assertEquals('LTL', $array_object['ShipmentType']); } } + +class CollectionTest extends Data +{ + public function __construct( + public Collection $test, + ) { + } +} diff --git a/tests/Unit/Data/Base/TypedCollectionTest.php b/tests/Unit/Data/Base/TypedCollectionTest.php index 008c5fb0..32d30052 100644 --- a/tests/Unit/Data/Base/TypedCollectionTest.php +++ b/tests/Unit/Data/Base/TypedCollectionTest.php @@ -99,4 +99,16 @@ public function testCanReduceValues(): void $this->assertEquals('GET', $collection->reduce(fn (string $carry, RestrictedResourceSchema $resource) => $carry . $resource->method, '')); } + + public function testToArrayObject(): void + { + $collection = RestrictedResourcesListSchema::make([ + new RestrictedResourceSchema(method: 'GET', path: '/path', data_elements: null), + ]); + + $array_object = $collection->toArrayObject(); + + $this->assertEquals('GET', $array_object[0]['method']); + $this->assertEquals('/path', $array_object[0]['path']); + } }