diff --git a/src/FormElement/Collection.php b/src/FormElement/Collection.php index 4cc3540e..f28e6b14 100644 --- a/src/FormElement/Collection.php +++ b/src/FormElement/Collection.php @@ -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; @@ -13,10 +14,10 @@ class Collection extends FieldsetElement /** @var callable */ protected $onAssembleGroup; - /** @var array */ + /** @var FormElement */ protected $addElement; - /** @var array */ + /** @var FormElement */ protected $removeElement; /** @var string[] */ @@ -24,47 +25,47 @@ class Collection extends FieldsetElement '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)) { @@ -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; } @@ -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;