PHPackages                             trebel/schematic - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. trebel/schematic

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

trebel/schematic
================

Schematic: Schema exchange protocol

1.0(3y ago)06MITPHPPHP &gt;=7.4

Since Dec 28Pushed 3y ago1 watchersCompare

[ Source](https://github.com/manvel-khnkoyan/schematic)[ Packagist](https://packagist.org/packages/trebel/schematic)[ Docs](https://github.com/manvel-khnkoyan/schematic)[ RSS](/packages/trebel-schematic/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

Schematic Protocol
------------------

[](#schematic-protocol)

The purpose of this library is to create a schema transfer protocol with the ability to export and import XML.

### How does it work

[](#how-does-it-work)

The library helps us create new data types with strict validation and a defined structure.

### Data types:

[](#data-types)

In current version, there are 4 data types: **Field**, **List**, **Operator**, **Schema**. Every created object is immutable, once created, you cannot change it.

### Data type: Trebel\\Schematic\\Field

[](#data-type-trebelschematicfield)

Fields are the smallest points of data types, the value of any "Field" must be primitive or we can see below can be Operator:

```
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;
    }
}
```

### Data type: Trebel\\Schematic\\Schema

[](#data-type-trebelschematicschema)

Schema data type is object-like type, each schema can contain Field, List, or other Schema data types.

```
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',
    ];
}
```

Then we can create our first Schema object:

```
$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
```

> `ID` is required field for each Schema data type

### Data type: Trebel\\Schematic\\Collection (List)

[](#data-type-trebelschematiccollection-list)

**Collection** Why not List? beacouse of list is reserved word in PHP

Lists are data types similar to arrays, and the only difference between a Schema and a List is that List can only handle one type, while Schema can handle any.

```
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( ... )
])
```

### Data type: Trebel\\Schematic\\Operator

[](#data-type-trebelschematicoperator)

Operators are special types, not to define an element, but to modify it.

For example, suppose we have a schema **Car** with fields **ID** and **Price**, and in the case where we want to change the price to add or subtract existing price, we can create **Increase** operator inside **Price**

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

In this version, the operators only work on the Field and List data types. There are defined operators

**Push** (Trebel\\Schematic\\Operators\\Push),
**Pull** (Trebel\\Schematic\\Operators\\Pull)
for **List** data type

**Increase** (Trebel\\Schematic\\Operators\\Increase)
for **Field** data type

But you can create your own data types as well.

To support any operator on a Field or List, you need to define all the operators within the types.

```
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)]
    )),
])
```

### Export / Import

[](#export--import)

The main part of the library is the ability to export and import XML schemas using a specific protocol.

#### Export Schema

[](#export-schema)

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

You can only import a schema, not a field, list, or operator.

Let's look at a specific example:

```
$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');
```

Outpout xml files could be:

File: /tmp/example/index.xml

```

        15

```

File: /tmp/example/Car/33.xml

```

        33
        Ford
        15000

```

and the same for Car/31.xml

Another example:

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

output:

```

        31

            500

```

#### Import Schema

[](#import-schema)

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

Concrete example:

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

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

#### Conclusion

[](#conclusion)

For more information, check out the tutorials located in the `examples` folder

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1236d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/73c797cb8af696819e6fa82d0d5c1fd9af189b29d067255c85ce2e148b697f2c?d=identicon)[trebel](/maintainers/trebel)

---

Top Contributors

[![mankhn](https://avatars.githubusercontent.com/u/5570356?v=4)](https://github.com/mankhn "mankhn (15 commits)")

---

Tags

schemavalidatordata

### Embed Badge

![Health badge](/badges/trebel-schematic/health.svg)

```
[![Health](https://phpackages.com/badges/trebel-schematic/health.svg)](https://phpackages.com/packages/trebel-schematic)
```

###  Alternatives

[opis/json-schema

Json Schema Validator for PHP

64236.9M186](/packages/opis-json-schema)[ergebnis/json-schema-validator

Provides a JSON schema validator, building on top of justinrainbow/json-schema.

3626.9M7](/packages/ergebnis-json-schema-validator)[geraintluff/jsv4

A (coercive) JSON Schema v4 Validator for PHP

115455.2k4](/packages/geraintluff-jsv4)[johnstevenson/json-works

Create, edit, query and validate json

272.5M6](/packages/johnstevenson-json-works)[form-manager/form-manager

PHP-HTML form manager

16041.0k7](/packages/form-manager-form-manager)[dstotijn/yii2-json-schema-validator

A Yii2 extension that provides a validator class for JSON Schema validation.

1730.7k](/packages/dstotijn-yii2-json-schema-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
