Skip to content

Commit f3ea2c4

Browse files
committed
Move ClassGenerator code to ValueObjectFactory - close #19
1 parent fa19733 commit f3ea2c4

File tree

7 files changed

+456
-332
lines changed

7 files changed

+456
-332
lines changed

README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,127 @@ Compiles a JSON schema to PHP classes / value objects via PHP AST.
77
```bash
88
$ composer require open-code-modeling/json-schema-to-php-ast --dev
99
```
10+
11+
## Usage
12+
13+
> See unit tests in `tests` folder for comprehensive examples.
14+
15+
You can use each value object factory to compose your value object with PHP AST node visitors or high level builder API.
16+
The easiest way to use this library is the `ValueObjectFactory`.
17+
18+
Assume you have the following JSON schema
19+
```json
20+
{
21+
"$schema": "http://json-schema.org/draft-07/schema#",
22+
"definitions": {
23+
"address": {
24+
"type": "object",
25+
"properties": {
26+
"street_address": {
27+
"type": "string"
28+
},
29+
"city": {
30+
"type": ["string", "null"]
31+
},
32+
"federal_state": {
33+
"$ref": "#/definitions/state"
34+
}
35+
},
36+
"required": [
37+
"street_address",
38+
"city",
39+
"federal_state"
40+
]
41+
},
42+
"state": {
43+
"type": "string",
44+
"enum": ["NY", "DC"]
45+
}
46+
},
47+
"type": "object",
48+
"properties": {
49+
"billing_address": {
50+
"$ref": "#/definitions/address"
51+
},
52+
"shipping_addresses": {
53+
"type": "array",
54+
"items": {
55+
"$ref": "#/definitions/address"
56+
}
57+
}
58+
},
59+
"required": [
60+
"billing_address"
61+
]
62+
}
63+
```
64+
65+
Then you can use the `ValueObjectFactory` to generate PHP code for the following classes:
66+
- `Order`
67+
- `BillingAddress`
68+
- `ShippingAddresses`
69+
- `Address`
70+
- `StreetAddress`
71+
- `City`
72+
- `State`
73+
74+
```php
75+
<?php
76+
77+
use OpenCodeModeling\CodeAst\Builder\FileCollection;
78+
use OpenCodeModeling\CodeAst\Package\ClassInfoList;
79+
use OpenCodeModeling\CodeAst\Package\Psr4Info;
80+
use OpenCodeModeling\Filter\FilterFactory;
81+
use OpenCodeModeling\JsonSchemaToPhp\Type\Type;
82+
use OpenCodeModeling\JsonSchemaToPhpAst\ValueObjectFactory;
83+
84+
$parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7);
85+
$printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]);
86+
87+
// configure your Composer info
88+
// FilterFactory of library open-code-modeling/php-filter is used for sake of brevity
89+
$classInfoList = new ClassInfoList(
90+
...Psr4Info::fromComposer(
91+
'src/',
92+
file_get_contents('composer.json'),
93+
FilterFactory::directoryToNamespaceFilter(),
94+
FilterFactory::namespaceToDirectoryFilter(),
95+
)
96+
);
97+
98+
$valueObjectFactory = new ValueObjectFactory(
99+
$classInfoList,
100+
$parser,
101+
$printer,
102+
true,
103+
FilterFactory::classNameFilter(),
104+
FilterFactory::propertyNameFilter(),
105+
FilterFactory::methodNameFilter(),
106+
FilterFactory::constantNameFilter(),
107+
FilterFactory::constantValueFilter()
108+
);
109+
110+
// $json contains the json string from above
111+
$decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
112+
113+
$typeSet = Type::fromDefinition($decodedJson);
114+
$srcFolder = 'tmp/';
115+
116+
$fileCollection = FileCollection::emptyList();
117+
$classBuilder = ClassBuilder::fromScratch('Order', 'YourNamespaceFromComposer')->setFinal(true);
118+
119+
$valueObjectFactory->generateClasses($classBuilder, $fileCollection, $typeSet, $srcFolder);
120+
121+
// $fileCollection contains 7 classes
122+
123+
// now let's add constants and getter methods of properties for non value objects
124+
$valueObjectFactory->addGetterMethodsForProperties($fileCollection, true);
125+
$valueObjectFactory->addClassConstantsForProperties($fileCollection);
126+
127+
// generate PHP code
128+
$files = $valueObjectFactory->generateFiles($fileCollection);
129+
130+
foreach ($files as $filename => $code) {
131+
// store PHP code to filesystem
132+
}
133+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"require": {
3535
"php": "^7.4 || ^8.0",
3636
"open-code-modeling/json-schema-to-php": "^0.2.0 || 0.3.x-dev",
37-
"open-code-modeling/php-code-ast": "^0.10.0 || 0.11.x-dev"
37+
"open-code-modeling/php-code-ast": "^0.11.0 || 0.12.x-dev"
3838
},
3939
"require-dev": {
4040
"laminas/laminas-filter": "^2.9",

composer.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)