use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Casts\IterableItemCast;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataProperty;
class ValueCast implements Cast, IterableItemCast
{
public function __construct(
protected ?string $value = null,
) {}
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed
{
return $this->castValue($value);
}
public function castIterableItem(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed {
return $this->castValue($value);
}
protected function castValue(mixed $value): ?string {
return data_get($value, $this->value);
}
}
class ProductData extends Data {
#[WithCast(ValueCast::class, 'value')]
public ?string $brand_property; // will return 'Nike'
}