Coder Social home page Coder Social logo

geekwolverine / valinor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cuyz/valinor

1.0 1.0 0.0 1.57 MB

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

Home Page: https://valinor.cuyz.io

License: MIT License

PHP 100.00%

valinor's Introduction

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.

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

valinor's People

Contributors

agustingomes avatar aurimasniekis avatar bcremer avatar boesing avatar brandonsavage avatar danog avatar davidbadura avatar dependabot[bot] avatar edudobay avatar fred-jan avatar jdreesen avatar lookyman avatar lucian-olariu avatar magnetik avatar marek-mikula avatar mopolo avatar mtouellette avatar ocramius avatar pscheit avatar rguennichi avatar romm avatar samsonasik avatar scuben avatar sergkash7 avatar slamdunk avatar szepeviktor avatar timwolla avatar vvoody- avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.