Skip to content

Commit

Permalink
Container::getComponents() returns array when $deep is false (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 8, 2024
1 parent f204540 commit 7f613ee
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
20 changes: 11 additions & 9 deletions src/ComponentModel/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* ComponentContainer is default implementation of IContainer.
*
* @property-read \Iterator $components
* @property-read IComponent[] $components
*/
class Container extends Component implements IContainer
{
Expand Down Expand Up @@ -179,20 +179,22 @@ protected function createComponent(string $name): ?IComponent

/**
* Iterates over descendants components.
* @return \Iterator<int|string,IComponent>
* @return iterable<int|string,IComponent>
*/
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;
}


Expand Down
4 changes: 2 additions & 2 deletions src/ComponentModel/IContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function getComponent(string $name): ?IComponent;

/**
* Iterates over descendants components.
* @return \Iterator<int|string,IComponent>
* @return iterable<int|string,IComponent>
*/
function getComponents(): \Iterator;
function getComponents(): iterable;
}
2 changes: 1 addition & 1 deletion src/ComponentModel/RecursiveComponentIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function hasChildren(): bool
*/
public function getChildren(): self
{
return $this->current()->getComponents();
return new self($this->current()->getComponents());
}


Expand Down
17 changes: 2 additions & 15 deletions tests/ComponentModel/Container.getComponents.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ComponentModel/Container.zeroname.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

0 comments on commit 7f613ee

Please sign in to comment.