Skip to content

Commit ec8ecfd

Browse files
committed
ControlGroup uses WeakMap
1 parent 7dab8e3 commit ec8ecfd

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Forms/ControlGroup.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
*/
1818
final class ControlGroup
1919
{
20-
protected \SplObjectStorage $controls;
20+
protected \WeakMap $controls;
2121
private array $options = [];
2222

2323

2424
public function __construct()
2525
{
26-
$this->controls = new \SplObjectStorage;
26+
$this->controls = new \WeakMap;
2727
}
2828

2929

3030
public function add(...$items): static
3131
{
3232
foreach ($items as $item) {
3333
if ($item instanceof Control) {
34-
$this->controls->attach($item);
34+
$this->controls[$item] = null;
3535

3636
} elseif ($item instanceof Container) {
3737
foreach ($item->getComponents() as $component) {
@@ -52,15 +52,15 @@ public function add(...$items): static
5252

5353
public function remove(Control $control): void
5454
{
55-
$this->controls->detach($control);
55+
unset($this->controls[$control]);
5656
}
5757

5858

5959
public function removeOrphans(): void
6060
{
61-
foreach ($this->controls as $control) {
61+
foreach ($this->controls as $control => $foo) {
6262
if (!$control->getForm(false)) {
63-
$this->controls->detach($control);
63+
unset($this->controls[$control]);
6464
}
6565
}
6666
}
@@ -69,7 +69,11 @@ public function removeOrphans(): void
6969
/** @return Control[] */
7070
public function getControls(): array
7171
{
72-
return iterator_to_array($this->controls);
72+
$res = [];
73+
foreach ($this->controls as $control => $foo) {
74+
$res[] = $control;
75+
}
76+
return $res;
7377
}
7478

7579

0 commit comments

Comments
 (0)