Skip to content

Commit

Permalink
Refactor form services to avoid repeating code (#2553)
Browse files Browse the repository at this point in the history
* refactor: DRY on CanHaveSubFields

* Add tests
  • Loading branch information
Tofandel authored May 28, 2024
1 parent 9a75e13 commit 2768019
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 71 deletions.
37 changes: 5 additions & 32 deletions src/Services/Forms/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use A17\Twill\Services\Forms\Contracts\CanRenderForBlocks;
use A17\Twill\Services\Forms\Traits\HasSubFields;
use A17\Twill\Services\Forms\Traits\RenderForBlocks;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
Expand All @@ -12,6 +13,7 @@
class Columns implements CanHaveSubfields, CanRenderForBlocks
{
use RenderForBlocks;
use HasSubFields;

protected function __construct(
public ?Collection $left = null,
Expand Down Expand Up @@ -66,37 +68,8 @@ public function render(): View

public function registerDynamicRepeaters(): void
{
if ($this->left) {
foreach ($this->left as $field) {
if ($field instanceof InlineRepeater) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
}

if ($this->middle) {
foreach ($this->middle as $field) {
if ($field instanceof InlineRepeater) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
}

if ($this->right) {
foreach ($this->right as $field) {
if ($field instanceof InlineRepeater) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
}
$this->registerDynamicRepeatersFor($this->left);
$this->registerDynamicRepeatersFor($this->middle);
$this->registerDynamicRepeatersFor($this->right);
}
}
12 changes: 4 additions & 8 deletions src/Services/Forms/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace A17\Twill\Services\Forms;

use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use A17\Twill\Services\Forms\Traits\HasSubFields;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class Fieldset implements CanHaveSubfields
{
use HasSubFields;

protected function __construct(
public ?string $title = null,
public ?Collection $fields = null,
Expand Down Expand Up @@ -77,13 +80,6 @@ public function fields(array $fields): static

public function registerDynamicRepeaters(): void
{
foreach ($this as $field) {
if ($field instanceof InlineRepeater) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
$this->registerDynamicRepeatersFor($this->fields);
}
}
5 changes: 4 additions & 1 deletion src/Services/Forms/Fieldsets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace A17\Twill\Services\Forms;

use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use A17\Twill\Services\Forms\Traits\HasSubFields;
use Illuminate\Support\Collection;

class Fieldsets extends Collection
class Fieldsets extends Collection implements CanHaveSubfields
{
use HasSubFields;
}
27 changes: 5 additions & 22 deletions src/Services/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace A17\Twill\Services\Forms;

use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use A17\Twill\Services\Forms\Traits\HasSubFields;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;

class Form extends Collection implements CanHaveSubfields
{
use HasSubFields;

public ?Fieldsets $fieldsets = null;

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

public function registerDynamicRepeaters(): void
{
// We have to loop over all of the fields/fieldsets.
foreach ($this as $field) {
if ($field instanceof InlineRepeater) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}

if ($this->fieldsets) {
foreach ($this->fieldsets as $fieldset) {
foreach ($fieldset->fields as $field) {
if ($field instanceof InlineRepeater) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
}
}
$this->registerDynamicRepeatersFor($this);
$this->fieldsets?->registerDynamicRepeaters();
}
}
12 changes: 4 additions & 8 deletions src/Services/Forms/InlineRepeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use A17\Twill\Services\Forms\Contracts\CanRenderForBlocks;
use A17\Twill\Services\Forms\Fields\Repeater;
use A17\Twill\Services\Forms\Traits\HasSubFields;
use A17\Twill\Services\Forms\Traits\RenderForBlocks;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
Expand All @@ -15,6 +16,7 @@
class InlineRepeater implements CanHaveSubfields, CanRenderForBlocks
{
use RenderForBlocks;
use HasSubFields;

protected function __construct(
private ?string $name = null,
Expand Down Expand Up @@ -225,14 +227,8 @@ public function render(): View

public function registerDynamicRepeaters(): void
{
foreach ($this->fields as $field) {
if ($field instanceof self) {
$field->register();
}
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
$this->register();
$this->registerDynamicRepeatersFor($this->fields);
}


Expand Down
27 changes: 27 additions & 0 deletions src/Services/Forms/Traits/HasSubFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace A17\Twill\Services\Forms\Traits;

use A17\Twill\Services\Forms\Contracts\CanHaveSubfields;
use Illuminate\Support\Collection;

trait HasSubFields
{
public function registerDynamicRepeaters(): void
{
if ($this instanceof Collection) {
$this->registerDynamicRepeatersFor($this);
}
}

protected function registerDynamicRepeatersFor(?iterable $fields): void
{
if ($fields) {
foreach ($fields as $field) {
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();
}
}
}
}
}
40 changes: 40 additions & 0 deletions tests/unit/Services/Forms/NestingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace A17\Twill\Tests\Unit\Services\Forms;

use A17\Twill\Services\Forms\Columns;
use A17\Twill\Services\Forms\Fieldset;
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Forms\InlineRepeater;
use A17\Twill\Tests\Unit\TestCase;
use A17\Twill\TwillBlocks;

class NestingTest extends TestCase
{
public function testRegisterDynamicRepeaters()
{
TwillBlocks::$dynamicRepeaters = [];
$form = Form::make([
Columns::make()->middle([
InlineRepeater::make()->name('repeater1')
]),
InlineRepeater::make()->name('repeater2'),
])->addFieldset(
Fieldset::make()->fields([
Columns::make()->left([
InlineRepeater::make()->name('set-repeater1')->fields([
InlineRepeater::make()->name('nested1')
])
])->right([
InlineRepeater::make()->name('set-repeater2')
])
])
);
$form->registerDynamicRepeaters();
$this->assertArrayHasKey('repeater1', TwillBlocks::$dynamicRepeaters);
$this->assertArrayHasKey('repeater2', TwillBlocks::$dynamicRepeaters);
$this->assertArrayHasKey('set-repeater1', TwillBlocks::$dynamicRepeaters);
$this->assertArrayHasKey('nested1', TwillBlocks::$dynamicRepeaters);
$this->assertArrayHasKey('set-repeater2', TwillBlocks::$dynamicRepeaters);
}
}

0 comments on commit 2768019

Please sign in to comment.