PHPackages                             martinstarecek/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. martinstarecek/schematic

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

martinstarecek/schematic
========================

Generate POPO from JSON Schemas

0.9.0(7y ago)0109Apache-2.0PHPPHP ~5.6

Since Jul 3Pushed 7y ago1 watchersCompare

[ Source](https://github.com/martinstarecek/schematic)[ Packagist](https://packagist.org/packages/martinstarecek/schematic)[ RSS](/packages/martinstarecek-schematic/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)Dependencies (1)Versions (15)Used By (0)

Schematic
=========

[](#schematic)

[![Latest Stable Version](https://camo.githubusercontent.com/6aed32a0da7bbf4e3d4cc64ade07f3ca124875c50a8af3a560289a892a9e85bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d617274696e737461726563656b2f736368656d617469632e737667)](https://packagist.org/packages/martinstarecek/schematic)[![License](https://camo.githubusercontent.com/105011df0c12e96717a68d9596553a6831358824ff8381a9c5bdb9569fda9a25/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d617274696e737461726563656b2f736368656d617469632e737667)](https://github.com/martinstarecek/schematic/blob/master/LICENSE)[![Build Status](https://camo.githubusercontent.com/db50972a442151663ac4f993eeb5398acaf0809b210345c6d56b24b286ce3b0d/68747470733a2f2f7472617669732d63692e6f72672f6d617274696e737461726563656b2f736368656d617469632e737667)](https://travis-ci.org/martinstarecek/schematic)[![Code Coverage](https://camo.githubusercontent.com/329b03fd332d0a0d5c72f2cb546c765acdcebaae26880c6a793ba83151227a19/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d617274696e737461726563656b2f736368656d617469632f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/martinstarecek/schematic/badges/coverage.png?b=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1351c76b332ad0ca98e6e821fb2716ae8afab39b08b2887ab9c428618e5d45d9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d617274696e737461726563656b2f736368656d617469632f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/martinstarecek/schematic/?branch=master)

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 martinstarecek/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:

```
