Valinor/README.md

45 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2021-11-28 17:43:02 +01:00
Valinor • PHP object mapper with strong type support
====================================================
2022-06-10 19:14:04 +02:00
[![Latest Stable Version](https://poser.pugx.org/cuyz/valinor/v)][link-packagist]
[![PHP Version Require](https://poser.pugx.org/cuyz/valinor/require/php)][link-packagist]
2022-06-12 17:57:38 +02:00
[![Total Downloads](https://poser.pugx.org/cuyz/valinor/downloads)][link-packagist]
2021-11-28 17:43:02 +01:00
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FCuyZ%2FValinor%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/CuyZ/Valinor/master)
2022-06-12 17:57:38 +02:00
---
2022-06-10 18:57:14 +02:00
<img src="docs/pages/img/valinor-logo.svg" align="left" width="100"/>
2021-11-28 17:43:02 +01:00
Valinor is a PHP library that helps to map any input into a strongly-typed value
object structure.
The conversion can handle native PHP types as well as other well-known advanced
type annotations like array shapes, generics and more.
## Installation
2021-11-28 17:43:02 +01:00
```bash
composer require cuyz/valinor
```
## Documentation
2021-11-28 17:43:02 +01:00
2022-06-10 18:57:14 +02:00
Documentation can be found at [valinor.cuyz.io](https://valinor.cuyz.io).
2021-11-28 17:43:02 +01:00
## Credits & thank you
2022-06-07 15:03:57 +02:00
The development of this library is mainly motivated by the kind words and the
help of many people. I am grateful to everyone, especially to the [contributors]
of this repository who directly help to push the project forward.
I also want to thank
2022-06-07 15:03:57 +02:00
[![blackfire-logo] Blackfire](https://www.blackfire.io/?utm_source=valinor&utm_medium=readme&utm_campaign=free-open-source)
for providing a license of their awesome tool, leading to notable performance
2022-06-07 15:03:57 +02:00
gains when using this library.
2021-11-28 17:43:02 +01:00
[link-packagist]: https://packagist.org/packages/cuyz/valinor
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
2022-06-07 15:03:57 +02:00
[contributors]: https://github.com/CuyZ/Valinor/graphs/contributors
[blackfire-logo]: docs/pages/img/blackfire-logo.svg "Blackfire logo"