PHP code example of chubbyphp / chubbyphp-parsing
1. Go to this page and download the library: Download chubbyphp/chubbyphp-parsing 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/ */
chubbyphp / chubbyphp-parsing example snippets
use Chubbyphp\Parsing\Schema\SchemaInterface;
/** @var SchemaInterface $schema */
$schema = ...;
$schema->nullable();
$schema->preParse(static fn ($input) => $input);
$schema->postParse(static fn (string $output) => $output);
$schema->parse('test');
$schema->safeParse('test');
$schema->catch(static fn (string $output, ParserErrorException $e) => $output);
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->array($p->int());
$data = $schema->parse([1, 2, 3, 4, 5]);
// validations
$schema->length(5);
$schema->minLength(5);
$schema->maxLength(5);
$schema->ions
$schema->reduce(static fn (int $sum, int $current) => $sum + $current, 0);
use Chubbyphp\Parsing\Parser;
enum BackedSuit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}
$p = new Parser();
$schema = $p->backedEnum(BackedSuit::class);
$data = $schema->parse('D');
// validations
// transformations
// conversions
$schema->toInt();
$schema->toString();
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->bool();
$data = $schema->parse(true);
// validations
// transformations
// conversions
$schema->toInt();
$schema->toString();
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->dateTime();
$data = $schema->parse(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));
// validations
$schema->from(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));
$schema->to(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));
// transformations
// conversions
$schema->toInt();
$schema->toString();
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->discriminatedUnion([
$p->object(['_type' => $p->literal('email'), 'address' => $p->string()]),
$p->object(['_type' => $p->literal('phone'), 'number' => $p->string()]),
]);
$data = $schema->parse(['_type' => 'phone', 'number' => '+41790000000']);
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->float();
$data = $schema->parse(4.2);
// validations
$schema->gt(5.0);
$schema->gte(5.0);
$schema->lt(5.0);
$schema->lte(5.0);
$schema->positive();
$schema->nonNegative();
$schema->negative();
$schema->nonPositive();
// transformations
// conversions
$schema->toInt();
$schema->toString();
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->int();
$data = $schema->parse(1337);
// validations
$schema->gt(5);
$schema->gte(5);
$schema->lt(5);
$schema->lte(5);
$schema->positive();
$schema->nonNegative();
$schema->negative();
$schema->nonPositive();
// transformations
// conversions
$schema->toDateTime();
$schema->toFloat();
$schema->toString();
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->lazy(static function () use ($p, &$schema) {
return $p->object([
'name' => $p->string(),
'child' => $schema,
])->nullable();
});
$data = $schema->parse([
'name' => 'name1',
'child' => [
'name' => 'name2',
'child' => null
],
]);
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->literal('email'); // supports string|float|int|bool
$data = $schema->parse('email');
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->object(['name' => $p->string()]);
// stdClass object
$data = $schema->parse(['name' => 'example']);
// SampleClass object
$data = $schema->parse(['name' => 'example'], SampleNamespace\SampleClass::class);
// validations
$schema->strict();
// transformations
// conversions
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->record($p->string());
$data = $schema->parse([
'key1' => 'value1',
'key2' => 'value2'
]);
use Chubbyphp\Parsing\Parser;
use Respect\Validation\Validator as v;
$p = new Parser();
$schema = $p->respectValidation(v::numericVal()->positive()->between(1, 255));
$data = $schema->parse(5);
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->string();
$data = $schema->parse('example');
// validations
$schema->length(5);
$schema->minLength(5);
$schema->maxLength(5);
$schema->ormations
$schema->trim();
$schema->trimStart();
$schema->trimEnd();
$schema->toLowerCase();
$schema->toUpperCase();
// conversions
$schema->toDateTime();
$schema->toFloat();
$schema->toInt();
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->tuple([$p->float(), $p->float()]);
$data = $schema->parse([47.1, 8.2]);
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->union([$p->string(), $p->int()]);
$data = $schema->parse('42');