Skip to content

Commit

Permalink
feat: make MessagesFlattener countable
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Jul 26, 2022
1 parent 0b37b48 commit 2c1c7cf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Mapper/Tree/Message/MessagesFlattener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace CuyZ\Valinor\Mapper\Tree\Message;

use Countable;
use CuyZ\Valinor\Mapper\Tree\Node;
use CuyZ\Valinor\Mapper\Tree\NodeTraverser;
use IteratorAggregate;
use Traversable;

use function array_filter;
use function count;

/**
* Will recursively flatten messages of a node and all its children.
Expand All @@ -34,7 +36,7 @@
*
* @implements IteratorAggregate<NodeMessage>
*/
final class MessagesFlattener implements IteratorAggregate
final class MessagesFlattener implements IteratorAggregate, Countable
{
/** @var array<NodeMessage> */
private array $messages = [];
Expand All @@ -58,6 +60,11 @@ public function errors(): self
return $clone;
}

public function count(): int
{
return count($this->messages);
}

/**
* @return Traversable<NodeMessage>
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Mapper/Tree/Message/MessagesFlattenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function test_messages_are_filtered_and_can_be_iterated_through(): void

$messages = [...(new MessagesFlattener($node))->errors()];

self::assertCount(2, $messages);

self::assertSame('some error message B', (string)$messages[0]);
self::assertSame('some error message A', (string)$messages[1]);
}
Expand Down

0 comments on commit 2c1c7cf

Please sign in to comment.