Library that helps to map any input into a strongly-typed value object structure.

Fund package maintenance!

Installs: 276 485

Dependents: 35

Suggesters: 0

Security: 2

Stars: 1 039

Watchers: 17

Forks: 56

Open Issues: 37

1.4.0 2023-04-17 11:25 UTC


Valinor banner

— From boring old arrays to shiny typed objects —

Latest Stable Version PHP Version Require Total Downloads Mutation testing badge

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.


composer require cuyz/valinor

📔 Read more on the online documentation


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"}

try {
    $country = (new \CuyZ\Valinor\MapperBuilder())
        ->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…


The full documentation is available on

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-logo Blackfire for providing a license of their awesome tool, leading to notable performance gains when using this library.