From 7f613eed7f5e57b6bde2d0be1bfdbb7e161620b3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 8 Dec 2023 16:17:05 +0100 Subject: [PATCH] Container::getComponents() returns array when $deep is false (BC break) --- src/ComponentModel/Container.php | 20 ++++++++++--------- src/ComponentModel/IContainer.php | 4 ++-- .../RecursiveComponentIterator.php | 2 +- .../Container.getComponents.phpt | 17 ++-------------- tests/ComponentModel/Container.zeroname.phpt | 2 +- 5 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/ComponentModel/Container.php b/src/ComponentModel/Container.php index e0cda4a..df3af13 100644 --- a/src/ComponentModel/Container.php +++ b/src/ComponentModel/Container.php @@ -15,7 +15,7 @@ /** * ComponentContainer is default implementation of IContainer. * - * @property-read \Iterator $components + * @property-read IComponent[] $components */ class Container extends Component implements IContainer { @@ -179,20 +179,22 @@ protected function createComponent(string $name): ?IComponent /** * Iterates over descendants components. - * @return \Iterator + * @return iterable */ - final public function getComponents(bool $deep = false, ?string $filterType = null): \Iterator + final public function getComponents(bool $deep = false, ?string $filterType = null): iterable { - $iterator = new RecursiveComponentIterator($this->components); if ($deep) { + $iterator = new RecursiveComponentIterator($this->components); $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST); + if ($filterType) { + $iterator = new \CallbackFilterIterator($iterator, fn($item) => $item instanceof $filterType); + } + return $iterator; } - if ($filterType) { - $iterator = new \CallbackFilterIterator($iterator, fn($item) => $item instanceof $filterType); - } - - return $iterator; + return $filterType + ? array_filter($this->components, fn($item) => $item instanceof $filterType) + : $this->components; } diff --git a/src/ComponentModel/IContainer.php b/src/ComponentModel/IContainer.php index 2b2a28c..9ed605f 100644 --- a/src/ComponentModel/IContainer.php +++ b/src/ComponentModel/IContainer.php @@ -36,7 +36,7 @@ function getComponent(string $name): ?IComponent; /** * Iterates over descendants components. - * @return \Iterator + * @return iterable */ - function getComponents(): \Iterator; + function getComponents(): iterable; } diff --git a/src/ComponentModel/RecursiveComponentIterator.php b/src/ComponentModel/RecursiveComponentIterator.php index b161860..3c1c003 100644 --- a/src/ComponentModel/RecursiveComponentIterator.php +++ b/src/ComponentModel/RecursiveComponentIterator.php @@ -30,7 +30,7 @@ public function hasChildren(): bool */ public function getChildren(): self { - return $this->current()->getComponents(); + return new self($this->current()->getComponents()); } diff --git a/tests/ComponentModel/Container.getComponents.phpt b/tests/ComponentModel/Container.getComponents.phpt index a18ad69..f9c74b7 100644 --- a/tests/ComponentModel/Container.getComponents.phpt +++ b/tests/ComponentModel/Container.getComponents.phpt @@ -38,26 +38,13 @@ Assert::same([ 'one', 'two', 'button1', -], array_keys(iterator_to_array($list))); - +], array_keys($list)); // Filter $list = $c->getComponents(false, Button::class); Assert::same([ 'button1', -], array_keys(iterator_to_array($list))); - - -// RecursiveIteratorIterator -$list = new RecursiveIteratorIterator($c->getComponents(), 1); -Assert::same([ - 'one', - 'inner', - 'inner2', - 'button2', - 'two', - 'button1', -], array_keys(iterator_to_array($list))); +], array_keys($list)); // Recursive diff --git a/tests/ComponentModel/Container.zeroname.phpt b/tests/ComponentModel/Container.zeroname.phpt index a668511..953b2c6 100644 --- a/tests/ComponentModel/Container.zeroname.phpt +++ b/tests/ComponentModel/Container.zeroname.phpt @@ -21,5 +21,5 @@ Assert::same('0', $container->getComponent('0')->getName()); $container->addComponent($c1 = new Container, '1', '0'); Assert::same( [1 => $c1, 0 => $c0], - (array) $container->getComponents(), + $container->getComponents(), );