PHP code example of trebel / schematic

1. Go to this page and download the library: Download trebel/schematic 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/ */

    

trebel / schematic example snippets


namespace Example\Fields\User;

use Trebel\Schematic\Field;

class ID extends Field {
    function __construct($value) {
        parent::__construct($value);
    }

    public function validate($value): bool {
        return ctype_digit($value) && $value > 0;
    }
}

class Name extends Field {
    function __construct($value) {
        parent::__construct($value);
    }

    public function validate($value): bool {
        return is_string($value) && strlen($value) > 0 && strlen($value) < 255;
    }
}

namespace Example;

use Trebel\Schematic\Schema;

class Person extends Schema {
    function __construct(...$args){
        parent::__construct(...$args);
    }

    public static $schema = [
        'Example\Fields\User\ID',
        'Example\Fields\User\Name',
    ];
}

$person = new Example\Person([
    new Example\UserId(1234),
    new Example\UserName('Jhone'),
])

echo $person->id . "\n"; // Ourput: "1234"
echo $person->id->innerItem() . "\n"; // Ourput: 1234 // absolute value

namespace Example;

use Trebel\Schematic\Collection;

class Persons extends Collection {
    protected static $type = 'Example\Person'; // Required

    function __construct(...$arg) {
        parent::__construct(...$arg);
    }
}

// To use Lists

$persons = new Examples\Persons([
    new Persons( ... ),
    new Persons( ... )
])


$carTwo = new Schemas\Car([
    new Fields\Car\ID(31),
    new Fields\Car\Price(
        new Operators\Increase(123),
    )
]);

class Cars extends Collection {
    public static $type = 'Examples\Schemas\Car';
    public static $operators = [
        'Trebel\Schematic\Operators\Push',
        'Trebel\Schematic\Operators\Pull',
    ];

    function __construct(...$arg) {
        parent::__construct(...$arg);
    }
}

// Example
new Lists\Cars([
    new Operators\Push(
        new Schemas\Car([
            new Fields\Car\ID(33),
            new Fields\Car\Name('Ford'),
            new Fields\Car\Price(15000)
        ])
    ),
    // Pay attention that Pull operator can be not completed schema
    new Operators\Pull( new Schemas\Car([
        new Fields\Car\ID(31)]
    )),
])

$exporter = new Trebel\Schematic\Tools\Exporter($schema);
$exporter->export($pathToExport);

$person = new Schemas\Person([
    new Fields\User\ID(15),
    new Lists\Cars([
        new Operators\Push(
            new Schemas\Car([
                new Fields\Car\ID(33),
                new Fields\Car\Name('Ford'),
                new Fields\Car\Price(15000)
            ])
        ),
        new Operators\Pull( new Schemas\Car([
            new Fields\Car\ID(31)]
        )),
    ])
])

$exporter = new Trebel\Schematic\Tools\Exporter($person);
$exporter->export('/tmp/example/index.xml');

$car = new Schemas\Car([
    new Fields\Car\ID(31),
    new Fields\Car\Price(
        new Operators\Increase(500),
    )
]);

$importer = new Trebel\Schematic\Tools\Importer([ .. list of supported schmeas]);
$schema = $importer->import($pathToSchema);

$importer = new Tools\Importer([
    'Examples\Schemas\Car',
    'Examples\Schemas\Person',
    'Examples\Schemas\Toyota',
]);

$car = $importer->import('/tmp/car/index.xml');