@@ -7,3 +7,127 @@ Compiles a JSON schema to PHP classes / value objects via PHP AST.
7
7
``` bash
8
8
$ composer require open-code-modeling/json-schema-to-php-ast --dev
9
9
```
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
+ ```
0 commit comments