2021-11-28 17:43:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace CuyZ\Valinor\Tests\Unit\Mapper\Tree;
|
|
|
|
|
|
|
|
use CuyZ\Valinor\Mapper\Tree\Exception\CannotGetInvalidNodeValue;
|
|
|
|
use CuyZ\Valinor\Mapper\Tree\Exception\DuplicatedNodeChild;
|
|
|
|
use CuyZ\Valinor\Mapper\Tree\Exception\InvalidNodeValue;
|
|
|
|
use CuyZ\Valinor\Tests\Fake\Definition\FakeAttributes;
|
2021-12-30 19:23:13 +01:00
|
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\FakeNode;
|
2021-11-28 17:43:02 +01:00
|
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeErrorMessage;
|
|
|
|
use CuyZ\Valinor\Tests\Fake\Mapper\Tree\Message\FakeMessage;
|
2022-04-04 23:36:28 +02:00
|
|
|
use CuyZ\Valinor\Tests\Fake\Type\FakeObjectType;
|
2021-11-28 17:43:02 +01:00
|
|
|
use CuyZ\Valinor\Tests\Fake\Type\FakeType;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-04-04 23:36:28 +02:00
|
|
|
use stdClass;
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
final class NodeTest extends TestCase
|
|
|
|
{
|
|
|
|
public function test_node_leaf_values_can_be_retrieved(): void
|
|
|
|
{
|
2021-12-30 19:23:13 +01:00
|
|
|
$type = FakeType::permissive();
|
2021-11-28 17:43:02 +01:00
|
|
|
$value = 'some node value';
|
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
$node = FakeNode::leaf($type, $value);
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
self::assertSame($type, $node->type());
|
|
|
|
self::assertSame($value, $node->value());
|
|
|
|
self::assertTrue($node->isRoot());
|
|
|
|
self::assertTrue($node->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_leaf_with_incorrect_value_throws_exception(): void
|
|
|
|
{
|
|
|
|
$type = new FakeType();
|
|
|
|
|
|
|
|
$this->expectException(InvalidNodeValue::class);
|
|
|
|
$this->expectExceptionCode(1630678334);
|
feat!: improve message customization with formatters
The way messages can be customized has been totally revisited, requiring
several breaking changes. All existing error messages have been
rewritten to better fit the actual meaning of the error.
The content of a message can be changed to fit custom use cases; it can
contain placeholders that will be replaced with useful information.
The placeholders below are always available; even more may be used
depending on the original message.
- `{message_code}` — the code of the message
- `{node_name}` — name of the node to which the message is bound
- `{node_path}` — path of the node to which the message is bound
- `{node_type}` — type of the node to which the message is bound
- `{original_value}` — the source value that was given to the node
- `{original_message}` — the original message before being customized
```php
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(SomeClass::class, [/* … */]);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$messages = new MessagesFlattener($error->node());
foreach ($messages as $message) {
if ($message->code() === 'some_code') {
$message = $message->withBody('new / {original_message}');
}
echo $message;
}
}
```
The messages are formatted using the ICU library, enabling the
placeholders to use advanced syntax to perform proper translations, for
instance currency support.
```php
try {
(new MapperBuilder())->mapper()->map('int<0, 100>', 1337);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$message = $error->node()->messages()[0];
if (is_numeric($message->value())) {
$message = $message->withBody(
'Invalid amount {original_value, number, currency}'
);
}
// Invalid amount: $1,337.00
echo $message->withLocale('en_US');
// Invalid amount: £1,337.00
echo $message->withLocale('en_GB');
// Invalid amount: 1 337,00 €
echo $message->withLocale('fr_FR');
}
```
If the `intl` extension is not installed, a shim will be available to
replace the placeholders, but it won't handle advanced syntax as
described above.
---
The new formatter `TranslationMessageFormatter` can be used to translate
the content of messages.
The library provides a list of all messages that can be returned; this
list can be filled or modified with custom translations.
```php
TranslationMessageFormatter::default()
// Create/override a single entry…
->withTranslation(
'fr',
'some custom message',
'un message personnalisé'
)
// …or several entries.
->withTranslations([
'some custom message' => [
'en' => 'Some custom message',
'fr' => 'Un message personnalisé',
'es' => 'Un mensaje personalizado',
],
'some other message' => [
// …
],
])
->format($message);
```
It is possible to join several formatters into one formatter by using
the `AggregateMessageFormatter`. This instance can then easily be
injected in a service that will handle messages.
The formatters will be called in the same order they are given to the
aggregate.
```php
(new AggregateMessageFormatter(
new LocaleMessageFormatter('fr'),
new MessageMapFormatter([
// …
],
TranslationMessageFormatter::default(),
))->format($message)
```
BREAKING CHANGE: The method `NodeMessage::format` has been removed,
message formatters should be used instead. If needed, the old behaviour
can be retrieved with the formatter `PlaceHolderMessageFormatter`,
although it is strongly advised to use the new placeholders feature.
BREAKING CHANGE: The signature of the method `MessageFormatter::format`
has changed.
2022-05-18 23:15:08 +02:00
|
|
|
$this->expectExceptionMessage("Value 'foo' does not match type `$type`.");
|
2021-11-28 17:43:02 +01:00
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
FakeNode::leaf($type, 'foo');
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_branch_values_can_be_retrieved(): void
|
|
|
|
{
|
2021-12-30 19:23:13 +01:00
|
|
|
$typeChildA = FakeType::permissive();
|
|
|
|
$typeChildB = FakeType::permissive();
|
2021-11-28 17:43:02 +01:00
|
|
|
$attributesChildA = new FakeAttributes();
|
|
|
|
$attributesChildB = new FakeAttributes();
|
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
$children = FakeNode::branch([
|
|
|
|
'foo' => ['type' => $typeChildA, 'value' => 'foo', 'attributes' => $attributesChildA],
|
|
|
|
'bar' => ['type' => $typeChildB, 'value' => 'bar', 'attributes' => $attributesChildB],
|
|
|
|
])->children();
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
self::assertSame('foo', $children['foo']->name());
|
|
|
|
self::assertSame('foo', $children['foo']->path());
|
|
|
|
self::assertFalse($children['foo']->isRoot());
|
|
|
|
self::assertSame($attributesChildA, $children['foo']->attributes());
|
|
|
|
self::assertSame('bar', $children['bar']->name());
|
|
|
|
self::assertSame('bar', $children['bar']->path());
|
|
|
|
self::assertFalse($children['bar']->isRoot());
|
|
|
|
self::assertSame($attributesChildB, $children['bar']->attributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_branch_with_duplicated_child_name_throws_exception(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DuplicatedNodeChild::class);
|
|
|
|
$this->expectExceptionCode(1634045114);
|
|
|
|
$this->expectExceptionMessage('The child `foo` is duplicated in the branch.');
|
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
FakeNode::branch([
|
|
|
|
['name' => 'foo'],
|
|
|
|
['name' => 'foo'],
|
|
|
|
]);
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_branch_with_incorrect_value_throws_exception(): void
|
|
|
|
{
|
|
|
|
$type = new FakeType();
|
|
|
|
|
|
|
|
$this->expectException(InvalidNodeValue::class);
|
|
|
|
$this->expectExceptionCode(1630678334);
|
feat!: improve message customization with formatters
The way messages can be customized has been totally revisited, requiring
several breaking changes. All existing error messages have been
rewritten to better fit the actual meaning of the error.
The content of a message can be changed to fit custom use cases; it can
contain placeholders that will be replaced with useful information.
The placeholders below are always available; even more may be used
depending on the original message.
- `{message_code}` — the code of the message
- `{node_name}` — name of the node to which the message is bound
- `{node_path}` — path of the node to which the message is bound
- `{node_type}` — type of the node to which the message is bound
- `{original_value}` — the source value that was given to the node
- `{original_message}` — the original message before being customized
```php
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(SomeClass::class, [/* … */]);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$messages = new MessagesFlattener($error->node());
foreach ($messages as $message) {
if ($message->code() === 'some_code') {
$message = $message->withBody('new / {original_message}');
}
echo $message;
}
}
```
The messages are formatted using the ICU library, enabling the
placeholders to use advanced syntax to perform proper translations, for
instance currency support.
```php
try {
(new MapperBuilder())->mapper()->map('int<0, 100>', 1337);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$message = $error->node()->messages()[0];
if (is_numeric($message->value())) {
$message = $message->withBody(
'Invalid amount {original_value, number, currency}'
);
}
// Invalid amount: $1,337.00
echo $message->withLocale('en_US');
// Invalid amount: £1,337.00
echo $message->withLocale('en_GB');
// Invalid amount: 1 337,00 €
echo $message->withLocale('fr_FR');
}
```
If the `intl` extension is not installed, a shim will be available to
replace the placeholders, but it won't handle advanced syntax as
described above.
---
The new formatter `TranslationMessageFormatter` can be used to translate
the content of messages.
The library provides a list of all messages that can be returned; this
list can be filled or modified with custom translations.
```php
TranslationMessageFormatter::default()
// Create/override a single entry…
->withTranslation(
'fr',
'some custom message',
'un message personnalisé'
)
// …or several entries.
->withTranslations([
'some custom message' => [
'en' => 'Some custom message',
'fr' => 'Un message personnalisé',
'es' => 'Un mensaje personalizado',
],
'some other message' => [
// …
],
])
->format($message);
```
It is possible to join several formatters into one formatter by using
the `AggregateMessageFormatter`. This instance can then easily be
injected in a service that will handle messages.
The formatters will be called in the same order they are given to the
aggregate.
```php
(new AggregateMessageFormatter(
new LocaleMessageFormatter('fr'),
new MessageMapFormatter([
// …
],
TranslationMessageFormatter::default(),
))->format($message)
```
BREAKING CHANGE: The method `NodeMessage::format` has been removed,
message formatters should be used instead. If needed, the old behaviour
can be retrieved with the formatter `PlaceHolderMessageFormatter`,
although it is strongly advised to use the new placeholders feature.
BREAKING CHANGE: The signature of the method `MessageFormatter::format`
has changed.
2022-05-18 23:15:08 +02:00
|
|
|
$this->expectExceptionMessage("Value 'foo' does not match type `$type`.");
|
2021-11-28 17:43:02 +01:00
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
FakeNode::branch([], $type, 'foo');
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_error_values_can_be_retrieved(): void
|
|
|
|
{
|
|
|
|
$message = new FakeErrorMessage();
|
2021-12-30 19:23:13 +01:00
|
|
|
$node = FakeNode::error($message);
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
self::assertFalse($node->isValid());
|
feat!: improve message customization with formatters
The way messages can be customized has been totally revisited, requiring
several breaking changes. All existing error messages have been
rewritten to better fit the actual meaning of the error.
The content of a message can be changed to fit custom use cases; it can
contain placeholders that will be replaced with useful information.
The placeholders below are always available; even more may be used
depending on the original message.
- `{message_code}` — the code of the message
- `{node_name}` — name of the node to which the message is bound
- `{node_path}` — path of the node to which the message is bound
- `{node_type}` — type of the node to which the message is bound
- `{original_value}` — the source value that was given to the node
- `{original_message}` — the original message before being customized
```php
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(SomeClass::class, [/* … */]);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$messages = new MessagesFlattener($error->node());
foreach ($messages as $message) {
if ($message->code() === 'some_code') {
$message = $message->withBody('new / {original_message}');
}
echo $message;
}
}
```
The messages are formatted using the ICU library, enabling the
placeholders to use advanced syntax to perform proper translations, for
instance currency support.
```php
try {
(new MapperBuilder())->mapper()->map('int<0, 100>', 1337);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$message = $error->node()->messages()[0];
if (is_numeric($message->value())) {
$message = $message->withBody(
'Invalid amount {original_value, number, currency}'
);
}
// Invalid amount: $1,337.00
echo $message->withLocale('en_US');
// Invalid amount: £1,337.00
echo $message->withLocale('en_GB');
// Invalid amount: 1 337,00 €
echo $message->withLocale('fr_FR');
}
```
If the `intl` extension is not installed, a shim will be available to
replace the placeholders, but it won't handle advanced syntax as
described above.
---
The new formatter `TranslationMessageFormatter` can be used to translate
the content of messages.
The library provides a list of all messages that can be returned; this
list can be filled or modified with custom translations.
```php
TranslationMessageFormatter::default()
// Create/override a single entry…
->withTranslation(
'fr',
'some custom message',
'un message personnalisé'
)
// …or several entries.
->withTranslations([
'some custom message' => [
'en' => 'Some custom message',
'fr' => 'Un message personnalisé',
'es' => 'Un mensaje personalizado',
],
'some other message' => [
// …
],
])
->format($message);
```
It is possible to join several formatters into one formatter by using
the `AggregateMessageFormatter`. This instance can then easily be
injected in a service that will handle messages.
The formatters will be called in the same order they are given to the
aggregate.
```php
(new AggregateMessageFormatter(
new LocaleMessageFormatter('fr'),
new MessageMapFormatter([
// …
],
TranslationMessageFormatter::default(),
))->format($message)
```
BREAKING CHANGE: The method `NodeMessage::format` has been removed,
message formatters should be used instead. If needed, the old behaviour
can be retrieved with the formatter `PlaceHolderMessageFormatter`,
although it is strongly advised to use the new placeholders feature.
BREAKING CHANGE: The signature of the method `MessageFormatter::format`
has changed.
2022-05-18 23:15:08 +02:00
|
|
|
self::assertSame('some error message', (string)$node->messages()[0]);
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_get_value_from_invalid_node_throws_exception(): void
|
|
|
|
{
|
2021-12-30 19:23:13 +01:00
|
|
|
$node = FakeNode::error();
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
$this->expectException(CannotGetInvalidNodeValue::class);
|
|
|
|
$this->expectExceptionCode(1630680246);
|
|
|
|
$this->expectExceptionMessage('Trying to get value of an invalid node at path ``.');
|
|
|
|
|
|
|
|
$node->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_branch_node_with_invalid_child_is_invalid(): void
|
|
|
|
{
|
2021-12-30 19:23:13 +01:00
|
|
|
$node = FakeNode::branch([
|
|
|
|
'foo' => [],
|
|
|
|
'bar' => ['message' => new FakeErrorMessage()],
|
|
|
|
]);
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
self::assertFalse($node->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_with_value_returns_node_with_value(): void
|
|
|
|
{
|
2021-12-30 19:23:13 +01:00
|
|
|
$nodeA = FakeNode::any();
|
2021-11-28 17:43:02 +01:00
|
|
|
$nodeB = $nodeA->withValue('bar');
|
|
|
|
|
|
|
|
self::assertNotSame($nodeA, $nodeB);
|
|
|
|
self::assertSame('bar', $nodeB->value());
|
|
|
|
self::assertTrue($nodeB->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_with_invalid_value_returns_invalid_node(): void
|
|
|
|
{
|
2021-12-30 19:23:13 +01:00
|
|
|
$type = FakeType::accepting('foo');
|
2021-11-28 17:43:02 +01:00
|
|
|
|
|
|
|
$this->expectException(InvalidNodeValue::class);
|
|
|
|
$this->expectExceptionCode(1630678334);
|
feat!: improve message customization with formatters
The way messages can be customized has been totally revisited, requiring
several breaking changes. All existing error messages have been
rewritten to better fit the actual meaning of the error.
The content of a message can be changed to fit custom use cases; it can
contain placeholders that will be replaced with useful information.
The placeholders below are always available; even more may be used
depending on the original message.
- `{message_code}` — the code of the message
- `{node_name}` — name of the node to which the message is bound
- `{node_path}` — path of the node to which the message is bound
- `{node_type}` — type of the node to which the message is bound
- `{original_value}` — the source value that was given to the node
- `{original_message}` — the original message before being customized
```php
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(SomeClass::class, [/* … */]);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$messages = new MessagesFlattener($error->node());
foreach ($messages as $message) {
if ($message->code() === 'some_code') {
$message = $message->withBody('new / {original_message}');
}
echo $message;
}
}
```
The messages are formatted using the ICU library, enabling the
placeholders to use advanced syntax to perform proper translations, for
instance currency support.
```php
try {
(new MapperBuilder())->mapper()->map('int<0, 100>', 1337);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$message = $error->node()->messages()[0];
if (is_numeric($message->value())) {
$message = $message->withBody(
'Invalid amount {original_value, number, currency}'
);
}
// Invalid amount: $1,337.00
echo $message->withLocale('en_US');
// Invalid amount: £1,337.00
echo $message->withLocale('en_GB');
// Invalid amount: 1 337,00 €
echo $message->withLocale('fr_FR');
}
```
If the `intl` extension is not installed, a shim will be available to
replace the placeholders, but it won't handle advanced syntax as
described above.
---
The new formatter `TranslationMessageFormatter` can be used to translate
the content of messages.
The library provides a list of all messages that can be returned; this
list can be filled or modified with custom translations.
```php
TranslationMessageFormatter::default()
// Create/override a single entry…
->withTranslation(
'fr',
'some custom message',
'un message personnalisé'
)
// …or several entries.
->withTranslations([
'some custom message' => [
'en' => 'Some custom message',
'fr' => 'Un message personnalisé',
'es' => 'Un mensaje personalizado',
],
'some other message' => [
// …
],
])
->format($message);
```
It is possible to join several formatters into one formatter by using
the `AggregateMessageFormatter`. This instance can then easily be
injected in a service that will handle messages.
The formatters will be called in the same order they are given to the
aggregate.
```php
(new AggregateMessageFormatter(
new LocaleMessageFormatter('fr'),
new MessageMapFormatter([
// …
],
TranslationMessageFormatter::default(),
))->format($message)
```
BREAKING CHANGE: The method `NodeMessage::format` has been removed,
message formatters should be used instead. If needed, the old behaviour
can be retrieved with the formatter `PlaceHolderMessageFormatter`,
although it is strongly advised to use the new placeholders feature.
BREAKING CHANGE: The signature of the method `MessageFormatter::format`
has changed.
2022-05-18 23:15:08 +02:00
|
|
|
$this->expectExceptionMessage("Value 1337 does not match type `$type`.");
|
2021-11-28 17:43:02 +01:00
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
FakeNode::leaf($type, 'foo')->withValue(1337);
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
2022-04-04 23:36:28 +02:00
|
|
|
public function test_node_with_invalid_value_for_object_type_returns_invalid_node(): void
|
|
|
|
{
|
|
|
|
$object = new stdClass();
|
|
|
|
$type = FakeObjectType::accepting($object);
|
|
|
|
|
|
|
|
$this->expectException(InvalidNodeValue::class);
|
|
|
|
$this->expectExceptionCode(1630678334);
|
feat!: improve message customization with formatters
The way messages can be customized has been totally revisited, requiring
several breaking changes. All existing error messages have been
rewritten to better fit the actual meaning of the error.
The content of a message can be changed to fit custom use cases; it can
contain placeholders that will be replaced with useful information.
The placeholders below are always available; even more may be used
depending on the original message.
- `{message_code}` — the code of the message
- `{node_name}` — name of the node to which the message is bound
- `{node_path}` — path of the node to which the message is bound
- `{node_type}` — type of the node to which the message is bound
- `{original_value}` — the source value that was given to the node
- `{original_message}` — the original message before being customized
```php
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(SomeClass::class, [/* … */]);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$messages = new MessagesFlattener($error->node());
foreach ($messages as $message) {
if ($message->code() === 'some_code') {
$message = $message->withBody('new / {original_message}');
}
echo $message;
}
}
```
The messages are formatted using the ICU library, enabling the
placeholders to use advanced syntax to perform proper translations, for
instance currency support.
```php
try {
(new MapperBuilder())->mapper()->map('int<0, 100>', 1337);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$message = $error->node()->messages()[0];
if (is_numeric($message->value())) {
$message = $message->withBody(
'Invalid amount {original_value, number, currency}'
);
}
// Invalid amount: $1,337.00
echo $message->withLocale('en_US');
// Invalid amount: £1,337.00
echo $message->withLocale('en_GB');
// Invalid amount: 1 337,00 €
echo $message->withLocale('fr_FR');
}
```
If the `intl` extension is not installed, a shim will be available to
replace the placeholders, but it won't handle advanced syntax as
described above.
---
The new formatter `TranslationMessageFormatter` can be used to translate
the content of messages.
The library provides a list of all messages that can be returned; this
list can be filled or modified with custom translations.
```php
TranslationMessageFormatter::default()
// Create/override a single entry…
->withTranslation(
'fr',
'some custom message',
'un message personnalisé'
)
// …or several entries.
->withTranslations([
'some custom message' => [
'en' => 'Some custom message',
'fr' => 'Un message personnalisé',
'es' => 'Un mensaje personalizado',
],
'some other message' => [
// …
],
])
->format($message);
```
It is possible to join several formatters into one formatter by using
the `AggregateMessageFormatter`. This instance can then easily be
injected in a service that will handle messages.
The formatters will be called in the same order they are given to the
aggregate.
```php
(new AggregateMessageFormatter(
new LocaleMessageFormatter('fr'),
new MessageMapFormatter([
// …
],
TranslationMessageFormatter::default(),
))->format($message)
```
BREAKING CHANGE: The method `NodeMessage::format` has been removed,
message formatters should be used instead. If needed, the old behaviour
can be retrieved with the formatter `PlaceHolderMessageFormatter`,
although it is strongly advised to use the new placeholders feature.
BREAKING CHANGE: The signature of the method `MessageFormatter::format`
has changed.
2022-05-18 23:15:08 +02:00
|
|
|
$this->expectExceptionMessage('Invalid value 1337.');
|
2022-04-04 23:36:28 +02:00
|
|
|
|
|
|
|
FakeNode::leaf($type, $object)->withValue(1337);
|
|
|
|
}
|
|
|
|
|
2021-11-28 17:43:02 +01:00
|
|
|
public function test_node_with_messages_returns_node_with_messages(): void
|
|
|
|
{
|
2021-12-30 19:23:13 +01:00
|
|
|
$messageA = new FakeMessage('some message A');
|
|
|
|
$messageB = new FakeMessage('some message B');
|
2021-11-28 17:43:02 +01:00
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
$nodeA = FakeNode::any();
|
2021-11-28 17:43:02 +01:00
|
|
|
$nodeB = $nodeA->withMessage($messageA)->withMessage($messageB);
|
|
|
|
|
|
|
|
self::assertNotSame($nodeA, $nodeB);
|
|
|
|
self::assertTrue($nodeB->isValid());
|
2022-05-06 13:56:16 +02:00
|
|
|
self::assertSame('some message A', (string)$nodeB->messages()[0]);
|
|
|
|
self::assertSame('some message B', (string)$nodeB->messages()[1]);
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_node_with_error_message_returns_invalid_node(): void
|
|
|
|
{
|
|
|
|
$message = new FakeMessage();
|
|
|
|
$errorMessage = new FakeErrorMessage();
|
|
|
|
|
2021-12-30 19:23:13 +01:00
|
|
|
$nodeA = FakeNode::any();
|
2021-11-28 17:43:02 +01:00
|
|
|
$nodeB = $nodeA->withMessage($message)->withMessage($errorMessage);
|
|
|
|
|
|
|
|
self::assertNotSame($nodeA, $nodeB);
|
|
|
|
self::assertFalse($nodeB->isValid());
|
feat!: improve message customization with formatters
The way messages can be customized has been totally revisited, requiring
several breaking changes. All existing error messages have been
rewritten to better fit the actual meaning of the error.
The content of a message can be changed to fit custom use cases; it can
contain placeholders that will be replaced with useful information.
The placeholders below are always available; even more may be used
depending on the original message.
- `{message_code}` — the code of the message
- `{node_name}` — name of the node to which the message is bound
- `{node_path}` — path of the node to which the message is bound
- `{node_type}` — type of the node to which the message is bound
- `{original_value}` — the source value that was given to the node
- `{original_message}` — the original message before being customized
```php
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(SomeClass::class, [/* … */]);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$messages = new MessagesFlattener($error->node());
foreach ($messages as $message) {
if ($message->code() === 'some_code') {
$message = $message->withBody('new / {original_message}');
}
echo $message;
}
}
```
The messages are formatted using the ICU library, enabling the
placeholders to use advanced syntax to perform proper translations, for
instance currency support.
```php
try {
(new MapperBuilder())->mapper()->map('int<0, 100>', 1337);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
$message = $error->node()->messages()[0];
if (is_numeric($message->value())) {
$message = $message->withBody(
'Invalid amount {original_value, number, currency}'
);
}
// Invalid amount: $1,337.00
echo $message->withLocale('en_US');
// Invalid amount: £1,337.00
echo $message->withLocale('en_GB');
// Invalid amount: 1 337,00 €
echo $message->withLocale('fr_FR');
}
```
If the `intl` extension is not installed, a shim will be available to
replace the placeholders, but it won't handle advanced syntax as
described above.
---
The new formatter `TranslationMessageFormatter` can be used to translate
the content of messages.
The library provides a list of all messages that can be returned; this
list can be filled or modified with custom translations.
```php
TranslationMessageFormatter::default()
// Create/override a single entry…
->withTranslation(
'fr',
'some custom message',
'un message personnalisé'
)
// …or several entries.
->withTranslations([
'some custom message' => [
'en' => 'Some custom message',
'fr' => 'Un message personnalisé',
'es' => 'Un mensaje personalizado',
],
'some other message' => [
// …
],
])
->format($message);
```
It is possible to join several formatters into one formatter by using
the `AggregateMessageFormatter`. This instance can then easily be
injected in a service that will handle messages.
The formatters will be called in the same order they are given to the
aggregate.
```php
(new AggregateMessageFormatter(
new LocaleMessageFormatter('fr'),
new MessageMapFormatter([
// …
],
TranslationMessageFormatter::default(),
))->format($message)
```
BREAKING CHANGE: The method `NodeMessage::format` has been removed,
message formatters should be used instead. If needed, the old behaviour
can be retrieved with the formatter `PlaceHolderMessageFormatter`,
although it is strongly advised to use the new placeholders feature.
BREAKING CHANGE: The signature of the method `MessageFormatter::format`
has changed.
2022-05-18 23:15:08 +02:00
|
|
|
self::assertSame('some message', (string)$nodeB->messages()[0]);
|
|
|
|
self::assertSame('some error message', (string)$nodeB->messages()[1]);
|
2021-11-28 17:43:02 +01:00
|
|
|
}
|
|
|
|
}
|