Skip to content

Commit 7da5b2b

Browse files
committed
refactor: DRY on CanHaveSubFields
1 parent ce0fbf1 commit 7da5b2b

File tree

6 files changed

+49
-71
lines changed

6 files changed

+49
-71
lines changed

src/Services/Forms/Columns.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
66
use A17\Twill\Services\Forms\Contracts\CanRenderForBlocks;
7+
use A17\Twill\Services\Forms\Traits\HasSubFields;
78
use A17\Twill\Services\Forms\Traits\RenderForBlocks;
89
use Illuminate\Contracts\View\View;
910
use Illuminate\Support\Collection;
@@ -12,6 +13,7 @@
1213
class Columns implements CanHaveSubfields, CanRenderForBlocks
1314
{
1415
use RenderForBlocks;
16+
use HasSubFields;
1517

1618
protected function __construct(
1719
public ?Collection $left = null,
@@ -66,37 +68,8 @@ public function render(): View
6668

6769
public function registerDynamicRepeaters(): void
6870
{
69-
if ($this->left) {
70-
foreach ($this->left as $field) {
71-
if ($field instanceof InlineRepeater) {
72-
$field->register();
73-
}
74-
if ($field instanceof CanHaveSubfields) {
75-
$field->registerDynamicRepeaters();
76-
}
77-
}
78-
}
79-
80-
if ($this->middle) {
81-
foreach ($this->middle as $field) {
82-
if ($field instanceof InlineRepeater) {
83-
$field->register();
84-
}
85-
if ($field instanceof CanHaveSubfields) {
86-
$field->registerDynamicRepeaters();
87-
}
88-
}
89-
}
90-
91-
if ($this->right) {
92-
foreach ($this->right as $field) {
93-
if ($field instanceof InlineRepeater) {
94-
$field->register();
95-
}
96-
if ($field instanceof CanHaveSubfields) {
97-
$field->registerDynamicRepeaters();
98-
}
99-
}
100-
}
71+
$this->registerDynamicRepeatersFor($this->left);
72+
$this->registerDynamicRepeatersFor($this->middle);
73+
$this->registerDynamicRepeatersFor($this->right);
10174
}
10275
}

src/Services/Forms/Fieldset.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace A17\Twill\Services\Forms;
44

55
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
6+
use A17\Twill\Services\Forms\Traits\HasSubFields;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Str;
89

910
class Fieldset implements CanHaveSubfields
1011
{
12+
use HasSubFields;
13+
1114
protected function __construct(
1215
public ?string $title = null,
1316
public ?Collection $fields = null,
@@ -77,13 +80,6 @@ public function fields(array $fields): static
7780

7881
public function registerDynamicRepeaters(): void
7982
{
80-
foreach ($this as $field) {
81-
if ($field instanceof InlineRepeater) {
82-
$field->register();
83-
}
84-
if ($field instanceof CanHaveSubfields) {
85-
$field->registerDynamicRepeaters();
86-
}
87-
}
83+
$this->registerDynamicRepeatersFor($this->fields);
8884
}
8985
}

src/Services/Forms/Fieldsets.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace A17\Twill\Services\Forms;
44

5+
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
6+
use A17\Twill\Services\Forms\Traits\HasSubFields;
57
use Illuminate\Support\Collection;
68

7-
class Fieldsets extends Collection
9+
class Fieldsets extends Collection implements CanHaveSubfields
810
{
11+
use HasSubFields;
912
}

src/Services/Forms/Form.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace A17\Twill\Services\Forms;
44

55
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
6+
use A17\Twill\Services\Forms\Traits\HasSubFields;
67
use Illuminate\Contracts\View\View;
78
use Illuminate\Support\Collection;
89

910
class Form extends Collection implements CanHaveSubfields
1011
{
12+
use HasSubFields;
13+
1114
public ?Fieldsets $fieldsets = null;
1215

1316
private ?Form $sideForm = null;
@@ -111,27 +114,7 @@ public function renderSideForm(): View
111114

112115
public function registerDynamicRepeaters(): void
113116
{
114-
// We have to loop over all of the fields/fieldsets.
115-
foreach ($this as $field) {
116-
if ($field instanceof InlineRepeater) {
117-
$field->register();
118-
}
119-
if ($field instanceof CanHaveSubfields) {
120-
$field->registerDynamicRepeaters();
121-
}
122-
}
123-
124-
if ($this->fieldsets) {
125-
foreach ($this->fieldsets as $fieldset) {
126-
foreach ($fieldset->fields as $field) {
127-
if ($field instanceof InlineRepeater) {
128-
$field->register();
129-
}
130-
if ($field instanceof CanHaveSubfields) {
131-
$field->registerDynamicRepeaters();
132-
}
133-
}
134-
}
135-
}
117+
$this->registerDynamicRepeatersFor($this);
118+
$this->fieldsets?->registerDynamicRepeaters();
136119
}
137120
}

src/Services/Forms/InlineRepeater.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
88
use A17\Twill\Services\Forms\Contracts\CanRenderForBlocks;
99
use A17\Twill\Services\Forms\Fields\Repeater;
10+
use A17\Twill\Services\Forms\Traits\HasSubFields;
1011
use A17\Twill\Services\Forms\Traits\RenderForBlocks;
1112
use Illuminate\Contracts\View\View;
1213
use Illuminate\Support\Collection;
@@ -15,6 +16,7 @@
1516
class InlineRepeater implements CanHaveSubfields, CanRenderForBlocks
1617
{
1718
use RenderForBlocks;
19+
use HasSubFields;
1820

1921
protected function __construct(
2022
private ?string $name = null,
@@ -209,13 +211,7 @@ public function render(): View
209211

210212
public function registerDynamicRepeaters(): void
211213
{
212-
foreach ($this->fields as $field) {
213-
if ($field instanceof self) {
214-
$field->register();
215-
}
216-
if ($field instanceof CanHaveSubfields) {
217-
$field->registerDynamicRepeaters();
218-
}
219-
}
214+
$this->register();
215+
$this->registerDynamicRepeatersFor($this->fields);
220216
}
221217
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace A17\Twill\Services\Forms\Traits;
4+
5+
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
6+
use Illuminate\Support\Collection;
7+
8+
trait HasSubFields
9+
{
10+
public function registerDynamicRepeaters(): void
11+
{
12+
if ($this instanceof Collection) {
13+
$this->registerDynamicRepeatersFor($this);
14+
}
15+
}
16+
17+
protected function registerDynamicRepeatersFor(?iterable $fields): void
18+
{
19+
if ($fields) {
20+
foreach ($fields as $field) {
21+
if ($field instanceof CanHaveSubfields) {
22+
$field->registerDynamicRepeaters();
23+
}
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)