cuyz / valinor
Library that helps to map any input into a strongly-typed value object structure.
Fund package maintenance!
romm
Installs: 1 718 657
Dependents: 69
Suggesters: 1
Security: 2
Stars: 1 313
Watchers: 17
Forks: 75
Open Issues: 32
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
- composer-runtime-api: ^2.0
- psr/simple-cache: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- infection/infection: ^0.27
- marcocesarato/php-conventional-changelog: ^1.12
- mikey179/vfsstream: ^1.6.10
- phpstan/phpstan: ^1.3
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^10.5
- rector/rector: ^1.0
- vimeo/psalm: ^5.0
This package is auto-updated.
Last update: 2024-11-06 07:46:58 UTC
README
— From boring old arrays to shiny typed objects —Valinor takes care of the construction and validation of raw inputs (JSON, plain arrays, etc.) into objects, ensuring a perfectly valid state. It allows the objects to be used without having to worry about their integrity during the whole application lifecycle.
The validation system will detect any incorrect value and help the developers by providing precise and human-readable error messages.
The mapper can handle native PHP types as well as other advanced types supported by PHPStan and Psalm like shaped arrays, generics, integer ranges and more.
The library also provides a normalization mechanism that can help transform any input into a data format (JSON, CSV, …), while preserving the original structure.
Installation
composer require cuyz/valinor
📔 Read more on the online documentation
Example
final class Country { public function __construct( /** @var non-empty-string */ public readonly string $name, /** @var list<City> */ public readonly array $cities, ) {} } final class City { public function __construct( /** @var non-empty-string */ public readonly string $name, public readonly DateTimeZone $timeZone, ) {} } $json = <<<JSON { "name": "France", "cities": [ {"name": "Paris", "timeZone": "Europe/Paris"}, {"name": "Lyon", "timeZone": "Europe/Paris"} ] } JSON; try { $country = (new \CuyZ\Valinor\MapperBuilder()) ->mapper() ->map(Country::class, \CuyZ\Valinor\Mapper\Source\Source::json($json)); echo $country->name; // France echo $country->cities[0]->name; // Paris } catch (\CuyZ\Valinor\Mapper\MappingError $error) { // Handle the error… }
Documentation
The full documentation is available on valinor.cuyz.io.
Credits & thank you
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 Blackfire for providing a license of their awesome tool, leading to notable performance gains when using this library.