adrema/app/Form/Casts/CollectionCast.php

32 lines
787 B
PHP
Raw Normal View History

2024-03-07 00:58:14 +01:00
<?php
namespace App\Form\Casts;
use App\Form\Fields\Field;
use Illuminate\Support\Collection;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Data;
2024-09-21 22:46:38 +02:00
use Spatie\LaravelData\Support\Creation\CreationContext;
2024-03-07 00:58:14 +01:00
use Spatie\LaravelData\Support\DataProperty;
class CollectionCast implements Cast
{
/**
* @param class-string<Data> $target
*/
public function __construct(public string $target)
{
}
/**
* @param array<int, array<string, mixed>> $value
* @param array<string, mixed> $context
* @return Collection<int, Data>
*/
2024-09-21 22:46:38 +02:00
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed
2024-03-07 00:58:14 +01:00
{
return collect($value)->map(fn ($item) => $this->target::from($item));
}
}