Skip to content

Commit

Permalink
Add ability to cast array items with ArrayCast
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed Sep 4, 2023
1 parent 6cbbd15 commit ca430e6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,20 @@ protected function casts(): array
}
```

If you want to cast all the elements inside the array, you can pass a `Castable` to the `ArrayCast`
constructor. Let's say that you want to convert all the items inside the array into integers:

```php
protected function casts(): array
{
return [
'property' => new ArrayCast(new IntegerCast()),
];
}
```

This works with all `Castable`, including `DTOCast` and `ModelCast` for nested data.

### Boolean

For string values, this uses the `filter_var` function with the `FILTER_VALIDATE_BOOLEAN` flag.
Expand Down
10 changes: 9 additions & 1 deletion src/Casting/ArrayCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

final class ArrayCast implements Castable
{
public function __construct(private ?Castable $type = null)
{
}

public function cast(string $property, mixed $value): array
{
if (is_string($value)) {
Expand All @@ -14,6 +18,10 @@ public function cast(string $property, mixed $value): array
return is_array($jsonDecoded) ? $jsonDecoded : [$value];
}

return is_array($value) ? $value : [$value];
$result = is_array($value) ? $value : [$value];

return blank($this->type)
? $result
: array_map(fn ($item) => $this->type->cast($property, $item), $result);
}
}
33 changes: 33 additions & 0 deletions tests/Unit/ArrayCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
declare(strict_types=1);

use WendellAdriel\ValidatedDTO\Casting\ArrayCast;
use WendellAdriel\ValidatedDTO\Casting\BooleanCast;
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
use WendellAdriel\ValidatedDTO\Casting\IntegerCast;
use WendellAdriel\ValidatedDTO\Tests\Datasets\ValidatedDTOInstance;

it('properly casts from json string to array')
->expect(fn () => new ArrayCast())
Expand All @@ -23,3 +27,32 @@
->expect(fn () => new ArrayCast())
->cast(test_property(), ['a', 'A', 1])
->toBe(['a', 'A', 1]);

it('properly casts a BooleanCast to array')
->expect(fn () => new ArrayCast(new BooleanCast()))
->cast(test_property(), [1, 'true', 'yes'])
->toBe([true, true, true]);

it('properly casts an IntegerCast to array')
->expect(fn () => new ArrayCast(new IntegerCast()))
->cast(test_property(), ['1', '5', '10'])
->toBe([1, 5, 10]);

it('properly casts an DTOCast', function () {
$castable = new ArrayCast(new DTOCast(ValidatedDTOInstance::class));

$johnDto = new ValidatedDTOInstance(['name' => 'John Doe', 'age' => 30]);
$maryDto = new ValidatedDTOInstance(['name' => 'Mary Doe', 'age' => 25]);

$dataToCast = [
['name' => 'John Doe', 'age' => 30],
['name' => 'Mary Doe', 'age' => 25],
];

$result = $castable->cast(test_property(), $dataToCast);

expect($result)->each->toBeInstanceOf(ValidatedDTOInstance::class);

expect($result[0]->toArray())->toEqual($johnDto->toArray())
->and($result[1]->toArray())->toEqual($maryDto->toArray());
});

0 comments on commit ca430e6

Please sign in to comment.