Skip to content

Commit

Permalink
refactor: DRY on CanHaveSubFields
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Mar 25, 2024
1 parent ce0fbf1 commit 7da5b2b
Show file tree
Hide file tree
Showing 6 changed files with 49 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);

Check warning on line 73 in src/Services/Forms/Columns.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Columns.php#L71-L73

Added lines #L71 - L73 were not covered by tests
}
}
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);

Check warning on line 83 in src/Services/Forms/Fieldset.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Fieldset.php#L83

Added line #L83 was not covered by tests
}
}
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 @@ -209,13 +211,7 @@ 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);

Check warning on line 215 in src/Services/Forms/InlineRepeater.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/InlineRepeater.php#L214-L215

Added lines #L214 - L215 were not covered by tests
}
}
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

Check warning on line 10 in src/Services/Forms/Traits/HasSubFields.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Traits/HasSubFields.php#L10

Added line #L10 was not covered by tests
{
if ($this instanceof Collection) {
$this->registerDynamicRepeatersFor($this);

Check warning on line 13 in src/Services/Forms/Traits/HasSubFields.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Traits/HasSubFields.php#L12-L13

Added lines #L12 - L13 were not covered by tests
}
}

protected function registerDynamicRepeatersFor(?iterable $fields): void
{
if ($fields) {
foreach ($fields as $field) {
if ($field instanceof CanHaveSubfields) {
$field->registerDynamicRepeaters();

Check warning on line 22 in src/Services/Forms/Traits/HasSubFields.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Forms/Traits/HasSubFields.php#L22

Added line #L22 was not covered by tests
}
}
}
}
}

0 comments on commit 7da5b2b

Please sign in to comment.