PHPackages                             rwos/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. rwos/schematic

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

rwos/schematic
==============

Generate POPO from JSON Schemas

0.6.0(7y ago)1830.6k↓89.3%6[2 issues](https://github.com/RoundingWellOS/schematic/issues)Apache-2.0PHPPHP &gt;=7.1

Since Jul 3Pushed 6y ago4 watchersCompare

[ Source](https://github.com/RoundingWellOS/schematic)[ Packagist](https://packagist.org/packages/rwos/schematic)[ RSS](/packages/rwos-schematic/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (5)Versions (12)Used By (0)

Schematic
=========

[](#schematic)

[![Latest Stable Version](https://camo.githubusercontent.com/96844505496e9267b0f70a373e5bcce2bbee4225591faef1ee7e13ecca6dc68a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72776f732f736368656d617469632e737667)](https://packagist.org/packages/rwos/schematic)[![License](https://camo.githubusercontent.com/35a7e3b6e67ec8ab35035d3f5646db891bcd4964c38f453abd3147bd2a38f7f6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f72776f732f736368656d617469632e737667)](https://github.com/RoundingWellOS/schematic/blob/master/LICENSE)[![Build Status](https://camo.githubusercontent.com/e0646d6a02c39242deedb4eac1d234f7aa1427cb42aec2a2944dc77c88588df4/68747470733a2f2f7472617669732d63692e6f72672f526f756e64696e6757656c6c4f532f736368656d617469632e737667)](https://travis-ci.org/RoundingWellOS/schematic)[![Code Coverage](https://camo.githubusercontent.com/da8fb001ccd717dbca9ed66d2a42a18b82669c3d0d4001277335a9903928fa4a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f526f756e64696e6757656c6c4f532f736368656d617469632f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/RoundingWellOS/schematic/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/828fae22cfc2b864969ddc7b5701dd922d44964e60ae805e7bb63fbe7f9c1dc1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f526f756e64696e6757656c6c4f532f736368656d617469632f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/RoundingWellOS/schematic/?branch=master)

**THIS PROJECT IS NOT MAINTAINED.**

Schematic is a [JSON Schema](http://json-schema.org/) parser that also supports Plain Old PHP Object (POPO) code generation. These generated models can either be used for as a starting point for domain entities or simply provide additional structure for JSON request bodies.

Strives for [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/), and [PSR-4](http://www.php-fig.org/psr/psr-4/) compliance.

Install
-------

[](#install)

```
composer require rwos/schematic

```

Examples
--------

[](#examples)

The `example/` directory contains a number of examples.

### Schema Parsing

[](#schema-parsing)

Given the following schema in `car.json`:

```
{
    "type": "object",
    "properties": {
        "make": {
            "type": "string"
        },
        "model": {
            "type": "string"
        },
        "year": {
            "type": "integer"
        },
        "owners": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "isCurrentOwner": {
                        "type": "boolean"
                    }
                },
                "required": ["name"]
            }
        }
    },
    "required": ["make", "model"]
}
```

First the schema needs to be parsed:

```
use RoundingWell\Schematic\Schema;

$schema = Schema::fromFile('car.json');

assert($schema->isObject());
```

When the schema is an object the properties can be accessed:

```
$properties = $schema->properties();

assert(is_array($properties));
assert(isset($properties['make']));
assert(isset($properties['owners']));
```

And can be checked for required properties:

```
assert($schema->isRequired('make'));
assert($schema->isRequired('year') === false);
```

Array items provide additional details about contents:

```
$items = $properties['owners']->items();

assert($items->isObject());
```

### Code Generation

[](#code-generation)

Generated classes are extremely basic, plain PHP objects from schemas.

```
use RoundingWell\Schematic\Generator;

$generator = new Generator();
```

The generator can create an [AST](https://github.com/nikic/PHP-Parser/blob/master/doc/0_Introduction.markdown) from an object schema, as well as additional classes for object properties.

```
$classes = $generator->generate($schema, 'Acme\Model\Car', 'Acme\Model');
```

This will create an `Acme\Model\Car` class that extends `Acme\Model`. Any object properties in the schema will also be created. Once the classes have been generated they can be written to a directory:

```
$files = $generator->write($classes, 'src/');
```

The writer will convert the AST into code, write to `src/`, and return the files that were created.

### Complete Example

[](#complete-example)

Putting it all together, here is a complete example for code generation:

```
use RoundingWell\Schematic\Generator;
use RoundingWell\Schematic\Schema;

$generator = new Generator();
$schema = Schema::fromFile('car.json');
$classes = $generator->generate($schema);
$files = $generator->write($classes, 'src/', 'Acme\Model');
```

***Note:** The last parameter to `write()` the [PSR-4](http://www.php-fig.org/psr/psr-4/) namespace of your project. When provided, this namespace will be removed from the file path.*

The source of `src/Car.php` will be:

```
