mirror of
https://github.com/danog/Valinor.git
synced 2024-11-26 20:24:40 +01:00
ddf69efaaa
Can be used to customize the content of messages added during a mapping. An implementation is provided by the library — `MessageMapFormatter`: The constructor parameter is an array where each key represents either: - The code of the message to be replaced - The content of the message to be replaced - The class name of the message to be replaced If none of those is found, the content of the message will stay unchanged unless a default one is given to this class. If one of these keys is found, the array entry will be used to replace the content of the message. This entry can be either a plain text or a callable that takes the message as a parameter and returns a string; it is for instance advised to use a callable in cases where a translation service is used — to avoid useless greedy operations. In any case, the content can contain placeholders that can be used the same way as `\CuyZ\Valinor\Mapper\Tree\Message\NodeMessage::format()`. See usage examples below: ``` $formatter = (new MessageMapFormatter([ // Will match if the given message has this exact code 'some_code' => 'new content / previous code was: %1$s', // Will match if the given message has this exact content 'Some message content' => 'new content / previous message: %2$s', // Will match if the given message is an instance of this class SomeError::class => ' - Original code of the message: %1$s - Original content of the message: %2$s - Node type: %3$s - Node name: %4$s - Node path: %5$s ', // A callback can be used to get access to the message instance OtherError::class => function (NodeMessage $message): string { if ((string)$message->type() === 'string|int') { // … } return 'Some message content'; }, // For greedy operation, it is advised to use a lazy-callback 'bar' => fn () => $this->translator->translate('foo.bar'), ])) ->defaultsTo('some default message') // …or… ->defaultsTo(fn () => $this->translator->translate('default')); $content = $formatter->format($message); ``` |
||
---|---|---|
.. | ||
Fake | ||
Fixture | ||
Functional | ||
Integration | ||
Traits | ||
Unit |