From 9d2b0f00f6f32896ef9ebbad27ec6166849d4e26 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 5 Oct 2023 02:14:36 +0200 Subject: [PATCH] ControlGroup uses WeakMap --- src/Forms/ControlGroup.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Forms/ControlGroup.php b/src/Forms/ControlGroup.php index 174bb9601..b7949cc38 100644 --- a/src/Forms/ControlGroup.php +++ b/src/Forms/ControlGroup.php @@ -17,13 +17,13 @@ */ final class ControlGroup { - protected \SplObjectStorage $controls; + protected \WeakMap $controls; private array $options = []; public function __construct() { - $this->controls = new \SplObjectStorage; + $this->controls = new \WeakMap; } @@ -31,7 +31,7 @@ public function add(...$items): static { foreach ($items as $item) { if ($item instanceof Control) { - $this->controls->attach($item); + $this->controls[$item] = null; } elseif ($item instanceof Container) { foreach ($item->getComponents() as $component) { @@ -52,15 +52,15 @@ public function add(...$items): static public function remove(Control $control): void { - $this->controls->detach($control); + unset($this->controls[$control]); } public function removeOrphans(): void { - foreach ($this->controls as $control) { + foreach ($this->controls as $control => $foo) { if (!$control->getForm(false)) { - $this->controls->detach($control); + unset($this->controls[$control]); } } } @@ -69,7 +69,11 @@ public function removeOrphans(): void /** @return Control[] */ public function getControls(): array { - return iterator_to_array($this->controls); + $res = []; + foreach ($this->controls as $control => $foo) { + $res[] = $control; + } + return $res; }