Download the PHP package facile-it/php-codec without Composer
On this page you can find all versions of the php package facile-it/php-codec. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-codec
PHP-codec
PHP-codec is a partial porting of io-ts in PHP.
Install it now. It only requires PHP >= 7.4.
composer require facile-it/php-codec
Disclaimer
This project is under active development: it's unstable and still poorly documented. The API is likely to change several times, and it won't be ready for production soon.
The project follows semantic versioning.
Introduction
This project is a partial porting of the fantastic io-ts library for Typescript. Everything rounds about the concept of decoder, encoder and codec.
Decoders are capable of transform values from one type to another one. This transformation may fail.
Encoders do a similar transformation but between types such that it cannot fail.
Codecs are a combination of a decoder and an encoder, putting together their features.
I recommend reading the The Idea section from the documentation of io-ts. It starts with a beautiful description of what codecs are.
A value of type
Type<A, O, I>
(called "codec") is the run time representation of the static typeA
.
Getting started
composer require facile-it/php-codec
Decoders
Decoders are objects with decoding capabilities.
A decoder of type Decoder<I, A>
takes an input of type I
and builds a result of type Validation<A>
.
The class Facile\PhpCodec\Decoders
provides factory methods for built-in decoders and combinators.
How to use decoders
Dealing with the validation result
We can use Validation::fold
to destruct the validation result while providing
a valid result in any case.
You can use the path reporter to build well-formatted error messages for failures.
Examples
Take a look to the examples folder.
Reporters
Reporters do create reports from Validation
objects.
Generally speaking, reporters are objects that implement the Reporter<T>
interface, given T
the type of the report that the generate.
One interesting group of reporters is the validation error reporters group.
They implements Reporter<list<string>>
.
Thus, given a Validation
object, they generate a list of error messages for each validation error.
PHP-Codec comes with two error reporters:
- PathReporter, which is a pretty straightforward porting of io-ts' PathReporter.
- SimplePathReporter, which is a simplified (read: shorter messages) version of the PathReporter.