Skip to content

Commit 5c57857

Browse files
authored
Merge branch 'master' into AddOptionToDefaultArraysToEmptyArray
2 parents dc64343 + 014b1d8 commit 5c57857

30 files changed

+613
-100
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"wol-soft/php-json-schema-model-generator-production": "^0.15",
14+
"wol-soft/php-json-schema-model-generator-production": "^0.16.0",
1515
"wol-soft/php-micro-template": "^1.3.2",
1616

1717
"php": ">=7.2",

docs/source/combinedSchemas/if.rst

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,119 @@
11
If
22
==
33

4-
Currently not fully supported
4+
The keywords `if`, `then` and `else` can be used to conditionally combine multiple subschemas. Conditional compositions can be used on property level and on object level.
5+
6+
.. code-block:: json
7+
8+
{
9+
"$id": "example",
10+
"type": "object",
11+
"properties": {
12+
"example": {
13+
"type": "number",
14+
"if": {
15+
"multipleOf": 5
16+
},
17+
"then": {
18+
"minimum": 100
19+
},
20+
"else": {
21+
"maximum": 100
22+
}
23+
}
24+
}
25+
}
26+
27+
Valid values are eg. 100, 105, 99. Invalid values are eg. 50, 101 or any non numeric values.
28+
29+
Generated interface:
30+
31+
.. code-block:: php
32+
33+
public function setExample(float $example): self;
34+
public function getExample(): ?float;
35+
36+
Possible exception (in this case 50 was provided so the if condition succeeds but the then branch failed):
37+
38+
.. code-block:: none
39+
40+
Invalid value for example declined by conditional composition constraint
41+
- Condition: Valid
42+
- Conditional branch failed:
43+
* Value for example must not be smaller than 100
44+
45+
Another example exception with 101 as value for the property:
46+
47+
.. code-block:: none
48+
49+
Invalid value for example declined by conditional composition constraint
50+
- Condition: Failed
51+
* Value for example must be a multiple of 5
52+
- Conditional branch failed:
53+
* Value for example must not be larger than 100
54+
55+
The thrown exception will be a *PHPModelGenerator\\Exception\\ComposedValue\\ConditionalException* which provides the following methods to get further error details:
56+
57+
.. code-block:: php
58+
59+
// get the exception which triggered the condition to fail
60+
// if error collection is enabled an ErrorRegistryException will be returned
61+
public function getIfException(): ?Exception
62+
63+
// get the exception which triggered the conditional branch to fail
64+
// if error collection is enabled an ErrorRegistryException will be returned
65+
public function getThenException(): ?Exception
66+
public function getElseException(): ?Exception
67+
68+
// get the name of the property which failed
69+
public function getPropertyName(): string
70+
// get the value provided to the property
71+
public function getProvidedValue()
72+
73+
An object level composition will result in an object which contains all properties contained in the three possible blocks of the condition.
74+
75+
.. code-block:: json
76+
77+
{
78+
"$id": "customer",
79+
"type": "object",
80+
"properties": {
81+
"country": {
82+
"enum": ["United States of America", "Canada"]
83+
}
84+
},
85+
"if": {
86+
"type": "object",
87+
"properties": {
88+
"country": {
89+
"const": "United States of America"
90+
}
91+
}
92+
},
93+
"then": {
94+
"type": "object",
95+
"properties": {
96+
"postal_code": {
97+
"pattern": "[0-9]{5}(-[0-9]{4})?"
98+
}
99+
}
100+
},
101+
"else": {
102+
"type": "object",
103+
"properties": {
104+
"postal_code": {
105+
"pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"
106+
}
107+
}
108+
}
109+
}
110+
111+
Generated interface:
112+
113+
.. code-block:: php
114+
115+
public function setCountry(string $country): self;
116+
public function getCountry(): ?string;
117+
118+
public function setPostalCode(string $country): self;
119+
public function getPostalCode(): ?string;

docs/source/complexTypes/enum.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ An enum can also be defined without a specific type.
4545
}
4646
}
4747
48-
Generated interface (no typehints are generated as it's an untyped enum):
48+
Generated interface (no typehints are generated as it's a mixed untyped enum. If all values in the untyped enum are of the same type [eg. only strings] the generated interface will contain type hinting):
4949

5050
.. code-block:: php
5151

docs/source/generator/postProcessor.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ AdditionalPropertiesAccessorPostProcessor
9292
9393
The **AdditionalPropertiesAccessorPostProcessor** adds methods to your model to work with `additional properties <../complexTypes/object.html#additional-properties>`__ on your objects. By default the post processor only adds methods to objects from a schema which defines constraints for additional properties. If the first constructor parameter *$addForModelsWithoutAdditionalPropertiesDefinition* is set to true the methods will also be added to objects generated from a schema which doesn't define additional properties constraints. If the *additionalProperties* keyword in a schema is set to false the methods will never be added.
9494

95+
.. note::
96+
97+
If the `deny additional properties setting <../gettingStarted.html#deny-additional-properties>`__ is set to true the setting *$addForModelsWithoutAdditionalPropertiesDefinition* is ignored as all objects which don't define additional properties are restricted to the defined properties
98+
9599
Added methods
96100
~~~~~~~~~~~~~
97101

@@ -144,7 +148,7 @@ By default additional properties are not included in serialized models. If the *
144148
Custom Post Processors
145149
----------------------
146150

147-
You can implement custom post processors to accomplish your tasks. Each post processor must implement the **PHPModelGenerator\\SchemaProcessor\\PostProcessor\\PostProcessorInterface**. If you have implemented a post processor add the post processor to your `ModelGenerator` and the post processor will be executed for each class.
151+
You can implement custom post processors to accomplish your tasks. Each post processor must extend the class **PHPModelGenerator\\SchemaProcessor\\PostProcessor\\PostProcessor**. If you have implemented a post processor add the post processor to your `ModelGenerator` and the post processor will be executed for each class.
148152

149153
A custom post processor which adds a custom trait to the generated model (eg. a trait adding methods for an active record pattern implementation) may look like:
150154

@@ -153,9 +157,9 @@ A custom post processor which adds a custom trait to the generated model (eg. a
153157
namespace MyApp\Model\Generator\PostProcessor;
154158
155159
use MyApp\Model\ActiveRecordTrait;
156-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
160+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
157161
158-
class ActiveRecordPostProcessor implements PostProcessorInterface
162+
class ActiveRecordPostProcessor extends PostProcessor
159163
{
160164
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
161165
{
@@ -184,3 +188,5 @@ What can you do inside your custom post processor?
184188
If a setter for a property is called with the same value which is already stored internally (consequently no update of the property is required), the setters will return directly and as a result of that the setter hooks will not be executed.
185189

186190
This behaviour also applies also to properties changed via the *populate* method added by the `PopulatePostProcessor <#populatepostprocessor>`__ and the *setAdditionalProperty* method added by the `AdditionalPropertiesAccessorPostProcessor <#additionalpropertiesaccessorpostprocessor>`__
191+
192+
To execute code before/after the processing of the schemas override the methods **preProcess** and **postProcess** inside your custom post processor.

docs/source/gettingStarted.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ By default optional properties which contain an `array <complexTypes/array.html>
199199
(new GeneratorConfiguration())
200200
->setDefaultArraysToEmptyArray(true);
201201
202+
Deny additional properties
203+
^^^^^^^^^^^^^^^^^^^^^^^^^^
204+
205+
By default each generated object accepts additional properties. For strict property checks which error if undefined properties are provided each object must contain the *additionalProperties* key set to *false*.
206+
207+
By setting the **denyAdditionalProperties** option each object which doesn't specify a value for *additionalProperties* is restricted to the defined properties.
208+
209+
.. code-block:: php
210+
211+
setDenyAdditionalProperties(bool $denyAdditionalProperties);
212+
213+
.. code-block:: php
214+
215+
(new GeneratorConfiguration())
216+
->setDenyAdditionalProperties(true);
217+
202218
Collect errors vs. early return
203219
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
204220

docs/source/types/string.rst

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The thrown exception will be a *PHPModelGenerator\\Exception\\String\\MinLengthE
7878
Pattern validation
7979
------------------
8080

81-
To add a pattern validation to the property use `pattern` keyword.
81+
To add a pattern validation to the property use the `pattern` keyword.
8282

8383
.. warning::
8484

@@ -115,4 +115,66 @@ The thrown exception will be a *PHPModelGenerator\\Exception\\String\\PatternExc
115115
Format
116116
------
117117

118-
String formats are currently not supported.
118+
To add a format validation to the property use the `format` keyword.
119+
120+
.. code-block:: json
121+
122+
{
123+
"$id": "example",
124+
"type": "object",
125+
"properties": {
126+
"example": {
127+
"type": "string",
128+
"format": "myFormat"
129+
}
130+
}
131+
}
132+
133+
Possible exceptions:
134+
135+
* Value for property must match the format __FORMAT__
136+
137+
The thrown exception will be a *PHPModelGenerator\\Exception\\String\\FormatException* which provides the following methods to get further error details:
138+
139+
.. code-block:: php
140+
141+
// get the expected format
142+
public function getExpectedFormat(): string
143+
// get the name of the property which failed
144+
public function getPropertyName(): string
145+
// get the value provided to the property
146+
public function getProvidedValue()
147+
148+
Builtin formats
149+
^^^^^^^^^^^^^^^
150+
151+
Currently no builtin formats are available.
152+
153+
Custom formats
154+
^^^^^^^^^^^^^^
155+
156+
You can implement custom format validators and use them in your schema files. You must add your custom format to the generator configuration to make them available.
157+
158+
.. code-block:: php
159+
160+
$generator = new Generator(
161+
(new GeneratorConfiguration())
162+
->addFormat(new MyCustomFormat())
163+
);
164+
165+
Your format validator must implement the interface **PHPModelGenerator\\Format\\FormatValidatorInterface**.
166+
167+
If your custom format is representable by a regular expression you can bypass implementing an own class and simply add a **FormatValidatorFromRegEx** (for example a string which must contain only numbers):
168+
169+
.. code-block:: php
170+
171+
$generator = new Generator(
172+
(new GeneratorConfiguration())
173+
->addFormat(new FormatValidatorFromRegEx('/^\d*$/'))
174+
);
175+
176+
.. hint::
177+
178+
Pull requests for common usable format validators are always welcome.
179+
A new format validator must be added in the *GeneratorConfiguration* method *initFormatValidator*.
180+
If the format validator requires a class implementation and can't be added via the *FormatValidatorFromRegEx* the class must be added to the *wol-soft/php-json-schema-model-generator-production* repository.

0 commit comments

Comments
 (0)