Skip to content

Commit 78b2c85

Browse files
author
Timm Ortloff
committed
Use cloning to duplicate $addElement and $removeElement and update PHPDocs
1 parent 9bddf14 commit 78b2c85

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

src/FormElement/Collection.php

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ipl\Html\FormElement;
44

55
use ipl\Html\Attributes;
6+
use ipl\Html\Contract\FormElement;
67
use ipl\Html\HtmlElement;
78
use ipl\Html\Text;
89

@@ -13,58 +14,58 @@ class Collection extends FieldsetElement
1314
/** @var callable */
1415
protected $onAssembleGroup;
1516

16-
/** @var array */
17+
/** @var FormElement */
1718
protected $addElement;
1819

19-
/** @var array */
20+
/** @var FormElement */
2021
protected $removeElement;
2122

2223
/** @var string[] */
2324
protected $defaultAttributes = [
2425
'class' => 'collection'
2526
];
2627

28+
/**
29+
* @param callable $callback
30+
*
31+
* @return void
32+
*/
2733
public function onAssembleGroup(callable $callback): void
2834
{
2935
$this->onAssembleGroup = $callback;
3036
}
3137

3238
/**
33-
* @param string $type
34-
* @param string $name
35-
* @param array|null $attributes
39+
* @param FormElement $element
3640
*
3741
* @return $this
3842
*/
39-
public function setAddElement(string $type, string $name, array $attributes = null): self
43+
public function setAddElement(FormELement $element): self
4044
{
41-
$this->addElement = [
42-
'type' => $type,
43-
'name' => $name,
44-
'attributes' => $attributes
45-
];
45+
$this->addElement = clone $element;
4646

4747
return $this;
4848
}
4949

5050
/**
51-
* @param string $type
52-
* @param string $name
53-
* @param array|null $attributes
51+
* @param FormElement $element
5452
*
5553
* @return $this
5654
*/
57-
public function setRemoveElement(string $type, string $name, array $attributes = null): self
55+
public function setRemoveElement(FormElement $element): self
5856
{
59-
$this->removeElement = [
60-
'type' => $type,
61-
'name' => $name,
62-
'attributes' => $attributes
63-
];
57+
$this->removeElement = clone $element;
6458

6559
return $this;
6660
}
6761

62+
/**
63+
* @param $group
64+
* @param $addElement
65+
* @param $removeElement
66+
*
67+
* @return $this
68+
*/
6869
protected function assembleGroup($group, $addElement, $removeElement): self
6970
{
7071
if (is_callable($this->onAssembleGroup)) {
@@ -82,7 +83,7 @@ protected function assemble()
8283

8384
$valid = true;
8485
foreach ($values as $key => $items) {
85-
if ($this->removeElement !== null && isset($items[0][$this->removeElement['name']])) {
86+
if ($this->removeElement !== null && isset($items[0][$this->removeElement->getName()])) {
8687
continue;
8788
}
8889

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

109-
if ($this->addElement !== null) {
110-
$addElement = $this->createElement(
111-
$this->addElement['type'],
112-
$this->addElement['name'],
113-
$this->addElement['attributes']
114-
);
115-
}
116-
117-
if ($this->removeElement !== null) {
118-
$removeElement = $this->createElement(
119-
$this->removeElement['type'],
120-
$this->removeElement['name'],
121-
$this->removeElement['attributes']
122-
);
123-
}
124-
125110
$this
126111
->registerElement($group)
127-
->assembleGroup($group, $addElement ?? null, $removeElement ?? null)
112+
->assembleGroup(
113+
$group,
114+
$this->addElement ? clone $this->addElement : null,
115+
$this->removeElement ? clone $this->removeElement : null
116+
)
128117
->addHtml($group);
129118

130119
return $group;

0 commit comments

Comments
 (0)