Skip to content

Commit

Permalink
Use cloning to duplicate $addElement and $removeElement and updat…
Browse files Browse the repository at this point in the history
…e PHPDocs
  • Loading branch information
Timm Ortloff committed Dec 13, 2022
1 parent cf1c6b9 commit c9802e3
Showing 1 changed file with 27 additions and 38 deletions.
65 changes: 27 additions & 38 deletions src/FormElement/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Html\FormElement;

use ipl\Html\Attributes;
use ipl\Html\Contract\FormElement;
use ipl\Html\HtmlElement;
use ipl\Html\Text;

Expand All @@ -13,58 +14,58 @@ class Collection extends FieldsetElement
/** @var callable */
protected $onAssembleGroup;

/** @var array */
/** @var FormElement */
protected $addElement;

/** @var array */
/** @var FormElement */
protected $removeElement;

/** @var string[] */
protected $defaultAttributes = [
'class' => 'collection'
];

/**
* @param callable $callback
*
* @return void
*/
public function onAssembleGroup(callable $callback): void
{
$this->onAssembleGroup = $callback;
}

/**
* @param string $type
* @param string $name
* @param array|null $attributes
* @param FormElement $element
*
* @return $this
*/
public function setAddElement(string $type, string $name, array $attributes = null): self
public function setAddElement(FormELement $element): self
{
$this->addElement = [
'type' => $type,
'name' => $name,
'attributes' => $attributes
];
$this->addElement = clone $element;

return $this;
}

/**
* @param string $type
* @param string $name
* @param array|null $attributes
* @param FormElement $element
*
* @return $this
*/
public function setRemoveElement(string $type, string $name, array $attributes = null): self
public function setRemoveElement(FormElement $element): self
{
$this->removeElement = [
'type' => $type,
'name' => $name,
'attributes' => $attributes
];
$this->removeElement = clone $element;

return $this;
}

/**
* @param $group
* @param $addElement
* @param $removeElement
*
* @return $this
*/
protected function assembleGroup($group, $addElement, $removeElement): self
{
if (is_callable($this->onAssembleGroup)) {
Expand All @@ -82,7 +83,7 @@ protected function assemble()

$valid = true;
foreach ($values as $key => $items) {
if ($this->removeElement !== null && isset($items[0][$this->removeElement['name']])) {
if ($this->removeElement !== null && isset($items[0][$this->removeElement->getName()])) {
continue;
}

Expand All @@ -106,25 +107,13 @@ protected function addGroup($key): FieldsetElement
Attributes::create(['class' => static::GROUP_CSS_CLASS])
);

if ($this->addElement !== null) {
$addElement = $this->createElement(
$this->addElement['type'],
$this->addElement['name'],
$this->addElement['attributes']
);
}

if ($this->removeElement !== null) {
$removeElement = $this->createElement(
$this->removeElement['type'],
$this->removeElement['name'],
$this->removeElement['attributes']
);
}

$this
->registerElement($group)
->assembleGroup($group, $addElement ?? null, $removeElement ?? null)
->assembleGroup(
$group,
$this->addElement ? clone $this->addElement : null,
$this->removeElement ? clone $this->removeElement : null
)
->addHtml($group);

return $group;
Expand Down

0 comments on commit c9802e3

Please sign in to comment.