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\Parser;

$p = new Parser();

// Define a schema
$userSchema = $p->object([
    'name' => $p->string()->minLength(1)->maxLength(100),
    'email' => $p->string()->email(),
    'age' => $p->int()->minimum(0)->maximum(150),
]);

// Parse and validate data
$user = $userSchema->parse([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'age' => 30,
]);

$schema->nullable();              // Allow null values
$schema->default($value);         // Provide default when input is null
$schema->preParse($fn);           // Transform input before parsing
$schema->postParse($fn);          // Transform output after parsing
$schema->parse($input);           // Parse and throw on error
$schema->safeParse($input);       // Parse and return Result object
$schema->catch($fn);              // Handle errors and provide fallback

use Chubbyphp\Parsing\ErrorsException;

try {
    $schema->parse($input);
} catch (ErrorsException $e) {
    $e->errors->jsonSerialize();                   // JSON structure
    $e->errors->toApiProblemInvalidParameters();   // RFC 7807 format
    $e->errors->toTree();                          // Hierarchical structure
}