adrema/app/Form/Data/FormConfigData.php

33 lines
847 B
PHP
Raw Permalink Normal View History

2024-03-07 00:58:14 +01:00
<?php
namespace App\Form\Data;
use App\Form\Casts\CollectionCast;
use App\Form\Transformers\CollectionTransformer;
use Illuminate\Support\Collection;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Attributes\WithTransformer;
class FormConfigData extends Data
{
/**
* @param Collection<int, SectionData> $sections
*/
public function __construct(
#[WithCast(CollectionCast::class, target: SectionData::class)]
#[WithTransformer(CollectionTransformer::class, target: SectionData::class)]
public Collection $sections
) {
}
public function fields(): FieldCollection
{
return $this->sections->reduce(
fn ($carry, $current) => $carry->merge($current->fields->all()),
new FieldCollection([])
);
}
}