Skip to content

Commit

Permalink
fix array object issues
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbrink committed Nov 14, 2024
1 parent 504b9c0 commit a104c38
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
10 changes: 8 additions & 2 deletions src/Data/Base/ToArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}

Expand Down
11 changes: 6 additions & 5 deletions src/Data/Base/TypedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Schemas/ListingsItems/AttributesListSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 13 additions & 14 deletions tests/Unit/Data/Base/ToArrayObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -82,3 +73,11 @@ public function testReturnsDataUsingPascalCase(): void
$this->assertEquals('LTL', $array_object['ShipmentType']);
}
}

class CollectionTest extends Data
{
public function __construct(
public Collection $test,
) {
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Data/Base/TypedCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit a104c38

Please sign in to comment.