Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/open-code-modeling/json-schema-to-php-ast
Provides factories to create PhpParser node visitors or PHP Code AST class builder objects from JSON schema e. g. value objects
https://github.com/open-code-modeling/json-schema-to-php-ast
abstract-syntax-tree ast code-generation code-generator json json-schema php php-ast schema value-object
Last synced: 2 months ago
JSON representation
Provides factories to create PhpParser node visitors or PHP Code AST class builder objects from JSON schema e. g. value objects
- Host: GitHub
- URL: https://github.com/open-code-modeling/json-schema-to-php-ast
- Owner: open-code-modeling
- License: mit
- Created: 2020-10-23T11:04:27.000Z (about 4 years ago)
- Default Branch: 0.6.x
- Last Pushed: 2022-03-24T13:55:06.000Z (almost 3 years ago)
- Last Synced: 2024-09-30T06:41:16.782Z (3 months ago)
- Topics: abstract-syntax-tree, ast, code-generation, code-generator, json, json-schema, php, php-ast, schema, value-object
- Language: PHP
- Homepage:
- Size: 272 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# JSON Schema to PHP AST
Compiles a JSON schema to PHP classes / value objects via PHP AST.
## Installation
```bash
$ composer require open-code-modeling/json-schema-to-php-ast --dev
```## 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
```json
{
"$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````php
create(PhpParser\ParserFactory::ONLY_PHP7);
$printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]);// configure your Composer info
// FilterFactory of library open-code-modeling/php-filter is used for sake of brevity
$classInfoList = new ClassInfoList(
...Psr4Info::fromComposer(
'src/',
file_get_contents('composer.json'),
FilterFactory::directoryToNamespaceFilter(),
FilterFactory::namespaceToDirectoryFilter(),
)
);$valueObjectFactory = new ValueObjectFactory(
$classInfoList,
$parser,
$printer,
true,
FilterFactory::classNameFilter(),
FilterFactory::propertyNameFilter(),
FilterFactory::methodNameFilter(),
FilterFactory::constantNameFilter(),
FilterFactory::constantValueFilter()
);// $json contains the json string from above
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);$typeSet = Type::fromDefinition($decodedJson);
$srcFolder = 'tmp/';$fileCollection = FileCollection::emptyList();
$classBuilder = ClassBuilder::fromScratch('Order', 'YourNamespaceFromComposer')->setFinal(true);$valueObjectFactory->generateClasses($classBuilder, $fileCollection, $typeSet, $srcFolder);
// $fileCollection contains 6 classes
// now let's add constants and getter methods of properties for non value objects
$valueObjectFactory->addGetterMethodsForProperties($fileCollection, true);
$valueObjectFactory->addClassConstantsForProperties($fileCollection);// generate PHP code
$files = $valueObjectFactory->generateFiles($fileCollection);foreach ($files as $filename => $code) {
// store PHP code to filesystem
}
```