PHPackages                             open-code-modeling/json-schema-to-php-ast - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. open-code-modeling/json-schema-to-php-ast

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

open-code-modeling/json-schema-to-php-ast
=========================================

Provides factories to create PhpParser node visitors from JSON schema e. g. value objects

0.5.4(4y ago)11717[1 PRs](https://github.com/open-code-modeling/json-schema-to-php-ast/pulls)2MITPHPPHP ^7.4 || ^8.0

Since Nov 12Pushed 1y agoCompare

[ Source](https://github.com/open-code-modeling/json-schema-to-php-ast)[ Packagist](https://packagist.org/packages/open-code-modeling/json-schema-to-php-ast)[ RSS](/packages/open-code-modeling-json-schema-to-php-ast/feed)WikiDiscussions 0.6.x Synced 3w ago

READMEChangelog (10)Dependencies (10)Versions (22)Used By (2)

JSON Schema to PHP AST
======================

[](#json-schema-to-php-ast)

Compiles a JSON schema to PHP classes / value objects via PHP AST.

Installation
------------

[](#installation)

```
$ composer require open-code-modeling/json-schema-to-php-ast --dev
```

Usage
-----

[](#usage)

> See unit tests in `tests` folder for comprehensive examples.

You can use each value object factory to compose your value object with PHP AST node visitors or high level builder API. The easiest way to use this library is the `ValueObjectFactory`.

Assume you have the following JSON schema

```
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "address": {
            "type": "object",
            "properties": {
                "street_address": {
                    "type": "string"
                },
                "city": {
                    "type": ["string", "null"]
                },
                "federal_state": {
                    "$ref": "#/definitions/state"
                }
            },
            "required": [
                "street_address",
                "city",
                "federal_state"
            ]
        },
        "state": {
            "type": "string",
            "enum": ["NY", "DC"]
        }
    },
    "type": "object",
    "properties": {
        "billing_address": {
            "$ref": "#/definitions/address"
        },
        "shipping_addresses": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/address"
            }
        }
    },
    "required": [
        "billing_address"
    ]
}
```

Then you can use the `ValueObjectFactory` to generate PHP code for the following classes:

- `Order`
- `ShippingAddresses`
- `Address`
- `StreetAddress`
- `City`
- `State`

```
