forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce helper class
MessagesFlattener
Will recursively flatten messages of a node and all its children. This helper can for instance be used when errors occurred during a mapping to flatten all caught errors into a basic array of string that can then easily be used to inform the user of what is wrong. ``` try { // … } catch(MappingError $error) { $messages = (new MessagesFlattener($error->node()))->errors(); foreach ($messages as $message) { echo $message; } } ```
- Loading branch information
Showing
3 changed files
with
187 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Tree\Message; | ||
|
||
use CuyZ\Valinor\Mapper\Tree\Node; | ||
use CuyZ\Valinor\Mapper\Tree\NodeTraverser; | ||
use IteratorAggregate; | ||
use Traversable; | ||
|
||
use function array_filter; | ||
|
||
/** | ||
* Will recursively flatten messages of a node and all its children. | ||
* | ||
* This helper can for instance be used when errors occurred during a mapping to | ||
* flatten all caught errors into a basic array of string that can then easily | ||
* be used to inform the user of what is wrong. | ||
* | ||
* ``` | ||
* try { | ||
* // … | ||
* } catch(MappingError $error) { | ||
* $messages = (new MessagesFlattener($error->node()))->errors(); | ||
* | ||
* foreach ($messages as $message) { | ||
* echo $message; | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @implements IteratorAggregate<NodeMessage> | ||
*/ | ||
final class MessagesFlattener implements IteratorAggregate | ||
{ | ||
/** @var array<NodeMessage> */ | ||
private array $messages = []; | ||
|
||
public function __construct(Node $node) | ||
{ | ||
$grouped = (new NodeTraverser( | ||
fn (Node $node) => $node->messages() | ||
))->traverse($node); | ||
|
||
foreach ($grouped as $messages) { | ||
$this->messages = [...$this->messages, ...$messages]; | ||
} | ||
} | ||
|
||
public function errors(): self | ||
{ | ||
$clone = clone $this; | ||
$clone->messages = array_filter($clone->messages, fn (NodeMessage $message) => $message->isError()); | ||
|
||
return $clone; | ||
} | ||
|
||
/** | ||
* @return Traversable<NodeMessage> | ||
*/ | ||
public function getIterator(): Traversable | ||
{ | ||
yield from $this->messages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree\Message; | ||
|
||
use CuyZ\Valinor\Mapper\Tree\Message\MessagesFlattener; | ||
use CuyZ\Valinor\Tests\Fake\Mapper\FakeNode; | ||
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage; | ||
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeMessage; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MessagesFlattenerTest extends TestCase | ||
{ | ||
public function test_messages_are_filtered_and_can_be_iterated_through(): void | ||
{ | ||
$messageA = new FakeMessage(); | ||
$errorA = new FakeErrorMessage('some error message A'); | ||
$errorB = new FakeErrorMessage('some error message B'); | ||
|
||
$node = FakeNode::branch([ | ||
'foo' => ['message' => $messageA], | ||
'bar' => ['message' => $errorA], | ||
])->withMessage($errorB); | ||
|
||
$messages = [...(new MessagesFlattener($node))->errors()]; | ||
|
||
self::assertSame('some error message B', (string)$messages[0]); | ||
self::assertSame('some error message A', (string)$messages[1]); | ||
} | ||
} |