Skip to content

Commit

Permalink
Merge pull request #84 from felixbessler/add-support-for-arrayable
Browse files Browse the repository at this point in the history
Added support for Arrayable
  • Loading branch information
WendellAdriel authored Jul 24, 2024
2 parents eceb7b7 + 7987576 commit 18e4521
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -434,6 +435,7 @@ private function getMappedProperties(array $mapping, string $key): array
private function isArrayable(mixed $value): bool
{
return is_array($value) ||
$value instanceof Arrayable ||
$value instanceof Collection ||
$value instanceof ValidatedDTO ||
$value instanceof Model ||
Expand All @@ -450,6 +452,7 @@ private function formatArrayableValue(mixed $value): array|int|string
$value instanceof Collection => $this->transformCollectionToArray($value),
$value instanceof Model => $this->transformModelToArray($value),
$value instanceof SimpleDTO => $this->transformDTOToArray($value),
$value instanceof Arrayable => $value->toArray(),
is_object($value) => (array) $value,
default => [],
};
Expand Down
22 changes: 22 additions & 0 deletions tests/Datasets/ArrayableObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\ValidatedDTO\Tests\Datasets;

use Illuminate\Contracts\Support\Arrayable;

class ArrayableObject implements Arrayable
{
public function key(): string
{
return 'arrayable-object-key';
}

public function toArray(): array
{
return [
'key' => $this->key(),
];
}
}
16 changes: 16 additions & 0 deletions tests/Datasets/ArrayableObjectCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\ValidatedDTO\Tests\Datasets;

use Illuminate\Contracts\Support\Arrayable;
use WendellAdriel\ValidatedDTO\Casting\Castable;

class ArrayableObjectCast implements Castable
{
public function cast(string $property, mixed $value): Arrayable
{
return app(ArrayableObject::class);
}
}
22 changes: 22 additions & 0 deletions tests/Datasets/ArrayableObjectDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\ValidatedDTO\Tests\Datasets;

use Illuminate\Contracts\Support\Arrayable;
use WendellAdriel\ValidatedDTO\Attributes\Cast;
use WendellAdriel\ValidatedDTO\Attributes\Rules;
use WendellAdriel\ValidatedDTO\Concerns\EmptyCasts;
use WendellAdriel\ValidatedDTO\Concerns\EmptyDefaults;
use WendellAdriel\ValidatedDTO\Concerns\EmptyRules;
use WendellAdriel\ValidatedDTO\ValidatedDTO;

class ArrayableObjectDTO extends ValidatedDTO
{
use EmptyCasts, EmptyDefaults, EmptyRules;

#[Rules(['required'])]
#[Cast(type: ArrayableObjectCast::class)]
public Arrayable $object;
}
13 changes: 13 additions & 0 deletions tests/Unit/ArrayCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WendellAdriel\ValidatedDTO\Casting\BooleanCast;
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
use WendellAdriel\ValidatedDTO\Casting\IntegerCast;
use WendellAdriel\ValidatedDTO\Tests\Datasets\ArrayableObjectDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedDTOInstance;

it('properly casts from json string to array')
Expand Down Expand Up @@ -56,3 +57,15 @@
expect($result[0]->toArray())->toEqual($johnDto->toArray())
->and($result[1]->toArray())->toEqual($maryDto->toArray());
});

it('properly casts an Arrayable object to array', function () {
$dto = ArrayableObjectDTO::fromArray([
'object' => 'arrayable-object-key',
]);

expect($dto->toArray())->toBe([
'object' => [
'key' => 'arrayable-object-key',
],
]);
});

0 comments on commit 18e4521

Please sign in to comment.