Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

ilario-pierbattista/php-codec

Repository files navigation

Php-codec

Php-codec is a partial porting of io-ts in PHP.

Minimum PHP Version

CI Static analysis codecov

Installation

composer require pybatt/php-codec

Introduction

This is a partial porting of the fantastic io-ts library for Typescript.

A value of type Type<A, O, I> (called "codec") is the runtime representation of the static type A.

I strongly recomend the reading of The Idea section from the io-ts documentation.

Types and combinators

All the implemented codecs and combinators are exposed through methods of the class Pybatt\Codec\Codecs.

Typescript Type Psalm Type Codec
unknown mixed TODO
null null Codecs::null()
bool bool Codecs::bool()
number int Codecs::int()
number float Codecs::float()
string string Codecs::string()
's' 's' Codecs::litteral('s')
Array<T> list<T> Codecs::listt(Type $item)
- A::class Codecs::classFromArray(Type[] $props, callable $factory, A::class)

Examples

For further examples take a look to the examples:

use Pybatt\Codec\Codecs;

$codec = Codecs::classFromArray(
    [
        'a' => Codecs::string(),
        'b' => Codecs::int(),
        'c' => Codecs::bool(),
        'd' => Codecs::float()
    ],
    function (string $a, int $b, bool $c, float $d): Foo {
        return new Foo($a, $b, $c, $d);
    },
    Foo::class
);

// Gives an instance of ValidationSuccess<Foo>
$validation = $codec->decode(['a' => 'hey', 'b' => 123, 'c' => false, 'd' => 1.23]);

// Gives an instance of ValidationFailures
$failures = $codec->decode(['a' => 'hey', 'b' => 123, 'c' => 'a random string', 'd' => 1.23]);

class Foo { 
    public function __construct(
        string $a,
        int $b,
        bool $c,
        float $d
    ) {
    // [...]
    }
}

About

A partial porting of io-ts in PHP.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages