|
2 | 2 |
|
3 | 3 | namespace Aerni\Factory\Factories; |
4 | 4 |
|
| 5 | +use Statamic\Fields\Field; |
| 6 | +use Statamic\Fields\Fields; |
| 7 | +use Statamic\Fields\Blueprint; |
5 | 8 | use Aerni\Factory\Support\Utils; |
| 9 | +use Illuminate\Support\Collection; |
6 | 10 | use Illuminate\Contracts\Support\Arrayable; |
7 | | -use Statamic\Fields\Blueprint; |
8 | 11 |
|
9 | 12 | class DefinitionGenerator implements Arrayable |
10 | 13 | { |
11 | | - public function __construct(protected Blueprint $blueprint) {} |
| 14 | + public function __construct(protected Blueprint $blueprint) |
| 15 | + { |
| 16 | + } |
12 | 17 |
|
13 | 18 | public function toArray(): array |
14 | 19 | { |
15 | | - return $this->processBlueprint(); |
| 20 | + return $this->processFields($this->blueprint->fields()->all()); |
16 | 21 | } |
17 | 22 |
|
18 | 23 | public function __toString(): string |
19 | 24 | { |
20 | 25 | return Utils::arrayToString($this->toArray()); |
21 | 26 | } |
22 | 27 |
|
23 | | - protected function processBlueprint(): array |
24 | | - { |
25 | | - return $this->blueprint->fields()->all() |
26 | | - ->reject(fn ($field) => $field->visibility() === 'computed') |
27 | | - ->map(fn ($field) => ['handle' => $field->handle(), 'field' => $field->config()]) |
28 | | - ->flatMap($this->processFields(...)) |
29 | | - ->all(); |
30 | | - } |
31 | | - |
32 | | - protected function processNestedFields(array $fields): array |
33 | | - { |
34 | | - return collect($fields) |
35 | | - ->flatMap($this->processFields(...)) |
36 | | - ->all(); |
37 | | - } |
38 | | - |
39 | | - protected function processFields(array $config): array |
| 28 | + protected function processFields(Collection $fields): array |
40 | 29 | { |
41 | | - return match (true) { |
42 | | - $config['field']['type'] === 'bard' => $this->processBardAndReplicator($config), |
43 | | - $config['field']['type'] === 'replicator' => $this->processBardAndReplicator($config), |
44 | | - $config['field']['type'] === 'grid' => $this->processGrid($config), |
45 | | - default => [$config['handle'] => null] |
46 | | - }; |
| 30 | + return $fields->map(fn (Field $field) => match ($field->type()) { |
| 31 | + 'bard' => $this->processBardAndReplicator($field), |
| 32 | + 'replicator' => $this->processBardAndReplicator($field), |
| 33 | + 'grid' => $this->processGrid($field), |
| 34 | + default => null, |
| 35 | + })->all(); |
47 | 36 | } |
48 | 37 |
|
49 | | - protected function processBardAndReplicator(array $config): array |
| 38 | + protected function processBardAndReplicator(Field $field): array |
50 | 39 | { |
51 | | - $fields = collect($config['field']['sets'])->flatMap(function ($group) { |
52 | | - return collect($group['sets'])->map(function ($set, $key) { |
53 | | - return array_merge($this->processNestedFields($set['fields']), [ |
54 | | - 'type' => $key, |
55 | | - 'enabled' => true, |
56 | | - ]); |
57 | | - }); |
58 | | - })->values()->all(); |
59 | | - |
60 | | - return [$config['handle'] => $fields]; |
| 40 | + return collect($field->toArray()['sets']) |
| 41 | + ->flatMap(function ($setGroup) { |
| 42 | + return collect($setGroup['sets'])->map(function ($set, $type) { |
| 43 | + return array_merge( |
| 44 | + $this->processFields((new Fields($set['fields']))->all()), |
| 45 | + ['type' => $type, 'enabled' => true] |
| 46 | + ); |
| 47 | + }); |
| 48 | + })->values()->toArray(); |
61 | 49 | } |
62 | 50 |
|
63 | | - protected function processGrid(array $config): array |
| 51 | + protected function processGrid(Field $field): array |
64 | 52 | { |
65 | | - $fields = collect($config['field']['fields']) |
66 | | - ->flatMap(fn ($config) => $this->processNestedFields([$config])) |
67 | | - ->toArray(); |
68 | | - |
69 | | - return [$config['handle'] => $fields]; |
| 53 | + return (new Fields($field->toArray()['fields'])) |
| 54 | + ->all() |
| 55 | + ->pipe($this->processFields(...)); |
70 | 56 | } |
71 | 57 | } |
0 commit comments