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!: wrap node messages in proper class
A new class `NodeMessage` is used to wrap messages added to a node during the mapping. This class will allow further features by giving access to useful data related to the bound node. BREAKING CHANGE: as of now every message is wrapped into a `NodeMessage` it is therefore not possible to check whether the message is an instance of `Throwable` — a new method `$message->isError()` is now to be used for such cases.
- Loading branch information
Showing
19 changed files
with
309 additions
and
107 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 was deleted.
Oops, something went wrong.
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,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Tree\Message; | ||
|
||
use CuyZ\Valinor\Definition\Attributes; | ||
use CuyZ\Valinor\Mapper\Tree\Node; | ||
use CuyZ\Valinor\Type\Type; | ||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
final class NodeMessage implements Message, HasCode | ||
{ | ||
private Node $node; | ||
|
||
private Message $message; | ||
|
||
public function __construct(Node $node, Message $message) | ||
{ | ||
$this->node = $node; | ||
$this->message = $message; | ||
} | ||
|
||
/** | ||
* Performs a placeholders replace operation on the given content. | ||
* | ||
* The values to be replaced will be the ones given as second argument; if | ||
* none is given these values will be used instead, in order: | ||
* | ||
* 1. The original code of this message | ||
* 2. The original content of this message | ||
* 3. A string representation of the node type | ||
* 4. The name of the node | ||
* 5. The path of the node | ||
* | ||
* See usage examples below: | ||
* | ||
* ``` | ||
* $content = $message->format('the previous code was: %1$s'); | ||
* | ||
* $content = $message->format( | ||
* '%1$s / new message content (type: %2$s)', | ||
* 'some parameter', | ||
* $message->type(), | ||
* ); | ||
* ``` | ||
*/ | ||
public function format(string $content, string ...$values): string | ||
{ | ||
return sprintf($content, ...$values ?: [ | ||
$this->code(), | ||
(string)$this, | ||
(string)$this->type(), | ||
$this->name(), | ||
$this->path(), | ||
]); | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->node->name(); | ||
} | ||
|
||
public function path(): string | ||
{ | ||
return $this->node->path(); | ||
} | ||
|
||
public function type(): Type | ||
{ | ||
return $this->node->type(); | ||
} | ||
|
||
public function attributes(): Attributes | ||
{ | ||
return $this->node->attributes(); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function value() | ||
{ | ||
if (! $this->node->isValid()) { | ||
return null; | ||
} | ||
|
||
return $this->node->value(); | ||
} | ||
|
||
public function originalMessage(): Message | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function isError(): bool | ||
{ | ||
return $this->message instanceof Throwable; | ||
} | ||
|
||
public function code(): string | ||
{ | ||
if ($this->message instanceof HasCode) { | ||
return $this->message->code(); | ||
} | ||
|
||
if ($this->message instanceof Throwable) { | ||
return (string)$this->message->getCode(); | ||
} | ||
|
||
return 'unknown'; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
if ($this->message instanceof Throwable) { | ||
return $this->message->getMessage(); | ||
} | ||
|
||
return (string)$this->message; | ||
} | ||
} |
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
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
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
Oops, something went wrong.