1. Go to this page and download the library: Download facile-it/php-codec library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
facile-it / php-codec example snippets
use Facile\PhpCodec\Validation\Validation;
/**
* @psalm-template I
* @psalm-template A
*/
interface Decoder {
/**
* @psalm-param I $i
* @psalm-return Validation<A>
*/
public function decode($i): Validation;
/** ... */
}
/**
* @psalm-template A
* @psalm-template O
*/
interface Encoder
{
/**
* @psalm-param A $a
* @psalm-return O
*/
public function encode($a);
}
use Facile\PhpCodec\Decoders;
use Facile\PhpCodec\Decoder;
use Facile\PhpCodec\Validation\Validation;
use Facile\PhpCodec\Validation\ValidationFailures;
use Facile\PhpCodec\Validation\ValidationSuccess;
/** @var Decoder<string, int> $decoder */
$decoder = Decoders::intFromString();
/** @var Validation<int> $v1 */
$v1 = $decoder->decode('123');
// Since '123' is a numeric string which represents an integer,
// then we can expect the decoding to be successful.
// Hence, $v1 will be an instance of ValidationSuccess
if($v1 instanceof ValidationSuccess) {
var_dump($v1->getValue());
}
/** @var Validation<int> $v2 */
$v2 = $decoder->decode('hello');
// Similarly, since 'hello' is not a numeric string, we expect
// the decoding fail. $v2 will be an instance of ValidationError
if($v2 instanceof ValidationFailures) {
var_dump($v2->getErrors());
}
use Facile\PhpCodec\Decoders;
use Facile\PhpCodec\Decoder;
use Facile\PhpCodec\Validation\Validation;
/** @var Decoder<string, int> $decoder */
$decoder = Decoders::intFromString();
Validation::fold(
function (\Facile\PhpCodec\Validation\ValidationFailures $failures): int {
// I may not care about the error.
// Here I want to give a default value when the deconding fails.
return 0;
},
function (\Facile\PhpCodec\Validation\ValidationSuccess $success): int {
return $success->getValue();
},
$decoder->decode($input)
);
use Facile\PhpCodec\Decoders;
$decoder = Decoders::intFromString();
$v = $decoder->decode('hello');
$msgs = \Facile\PhpCodec\Reporters::path()->report($v);
var_dump($msgs);
/* This will print
array(1) {
[0] =>
string(49) "Invalid value "hello" supplied to : IntFromString"
}
*/
$d = Decoders::arrayProps([
'a' => Decoders::arrayProps([
'a1' => Decoders::int(),
'a2' => Decoders::string(),
]),
'b' => Decoders::arrayProps(['b1' => Decoders::bool()])
]);
$v = $d->decode(['a'=> ['a1' => 'str', 'a2' => 1], 'b' => 2]);
$x = \Facile\PhpCodec\Reporters::path()->report($v);
// $x will be
// ['Invalid value "str" supplied to : {a: {a1: int, a2: string}, b: {b1: bool}}/a: {a1: int, a2: string}/a1: int',
// 'Invalid value 1 supplied to : {a: {a1: int, a2: string}, b: {b1: bool}}/a: {a1: int, a2: string}/a2: string',
// 'Invalid value undefined supplied to : {a: {a1: int, a2: string}, b: {b1: bool}}/b: {b1: bool}/b1: bool']
$y = \Facile\PhpCodec\Reporters::simplePath()->report($v);
// $y will be
// ['/a/a1: Invalid value "str" supplied to decoder "int"',
// '/a/a2: Invalid value 1 supplied to decoder "string"',
// '/b/b1: Invalid value undefined supplied to decoder "bool"']
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.