From 1fa23e66b4806dbb6a54c7c5e2332b94d8caaf8b Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Mon, 30 Sep 2024 20:38:42 +0200 Subject: [PATCH] refactor: treat binding collection as iterable Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- src/Binding/Binder/CompositeBinder.php | 7 +++++++ .../Definition/DefinitionCollection.php | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Binding/Binder/CompositeBinder.php b/src/Binding/Binder/CompositeBinder.php index 8b88634..4df27e4 100644 --- a/src/Binding/Binder/CompositeBinder.php +++ b/src/Binding/Binder/CompositeBinder.php @@ -22,6 +22,13 @@ final class CompositeBinder implements Binder { } public function bind( Definition $def ): void { + if ( is_iterable( $def ) ) { + foreach ( $def as $d ) { + $this->bind( $d ); + } + return; + } + foreach ( $this->binders as $binder ) { if ( $binder->can_bind( $def ) ) { $binder->bind( $def ); diff --git a/src/Binding/Definition/DefinitionCollection.php b/src/Binding/Definition/DefinitionCollection.php index ed67b92..99b82ba 100644 --- a/src/Binding/Definition/DefinitionCollection.php +++ b/src/Binding/Definition/DefinitionCollection.php @@ -4,16 +4,20 @@ declare(strict_types=1); namespace WPDesk\Init\Binding\Definition; +use IteratorAggregate; +use Traversable; use WPDesk\Init\Binding\Definition; -/** @implements Definition<mixed> */ -class DefinitionCollection implements Definition { +/** + * @implements Definition<mixed> + * @implements IteratorAggregate<mixed,Definition<mixed>> + */ +class DefinitionCollection implements Definition, \IteratorAggregate { - /** @var ?string */ - private $hook; + private ?string $hook; /** @var Definition[] */ - private $defs; + private array $defs; /** @var array<string, mixed> */ private array $options; @@ -41,4 +45,8 @@ class DefinitionCollection implements Definition { public function option( string $name ) { return $this->options[ $name ] ?? null; } + + public function getIterator(): Traversable { + yield from $this->defs; + } } -- GitLab