Skip to content

Commit

Permalink
Move ClassGenerator code to ValueObjectFactory - close #19
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Jan 31, 2021
1 parent fa19733 commit f3ea2c4
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 332 deletions.
124 changes: 124 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,127 @@ Compiles a JSON schema to PHP classes / value objects via PHP AST.
```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`
- `BillingAddress`
- `ShippingAddresses`
- `Address`
- `StreetAddress`
- `City`
- `State`

```php
<?php

use OpenCodeModeling\CodeAst\Builder\FileCollection;
use OpenCodeModeling\CodeAst\Package\ClassInfoList;
use OpenCodeModeling\CodeAst\Package\Psr4Info;
use OpenCodeModeling\Filter\FilterFactory;
use OpenCodeModeling\JsonSchemaToPhp\Type\Type;
use OpenCodeModeling\JsonSchemaToPhpAst\ValueObjectFactory;

$parser = (new PhpParser\ParserFactory())->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 7 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
}
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"require": {
"php": "^7.4 || ^8.0",
"open-code-modeling/json-schema-to-php": "^0.2.0 || 0.3.x-dev",
"open-code-modeling/php-code-ast": "^0.10.0 || 0.11.x-dev"
"open-code-modeling/php-code-ast": "^0.11.0 || 0.12.x-dev"
},
"require-dev": {
"laminas/laminas-filter": "^2.9",
Expand Down
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f3ea2c4

Please sign in to comment.