Php-codec is a partial porting of io-ts in PHP.
composer require pybatt/php-codec
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 typeA
.
I strongly recomend the reading of The Idea section from the io-ts documentation.
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) |
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
) {
// [...]
}
}