You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To avoid adding all dependencies of the php-json-schema-model-generator to your production dependencies it's recommended to add the library as a dev-dependency and include the [wol-soft/php-json-schema-model-generator-production](https://github.com/wol-soft/php-json-schema-model-generator-production) library. The production library provides all classes to run the generated code. Generating the classes should either be a step done in the development environment (if you decide to commit the models) or as a build step of your application.
41
+
42
+
To avoid adding all dependencies of the php-json-schema-model-generator to your production dependencies it's recommended to add the library as a dev-dependency and include the [wol-soft/php-json-schema-model-generator-production](https://github.com/wol-soft/php-json-schema-model-generator-production) library. The production library provides all classes to run the generated code. Generating the classes should either be a step done in the development environment or as a build step of your application (for example you could generate the models in a [composer post-autoload-dump script](https://getcomposer.org/doc/articles/scripts.md#command-events), which is the recommended workflow).
``` setNamespacePrefix(string $prefix) ``` <br><br>Example:<br> ``` setNamespacePrefix('\MyApp\Model') ``` | Configures a namespace prefix for all generated classes. The namespaces will be extended with the directory structure of the source directory. | Empty string so no namespace prefix will be used
85
86
``` setImmutable(bool $immutable) ``` <br><br>Example:<br> ``` setImmutable(false) ``` | If set to true the generated model classes will be delivered without setter methods for the object properties. | true
87
+
``` setImplicitNull(bool $allowImplicitNull) ``` <br><br>Example:<br> ``` setImplicitNull(true) ``` | By setting the implicit null option to true all of your object properties which aren't required will implicitly accept null. | false
86
88
``` setCollectErrors(bool $collectErrors) ``` <br><br>Example:<br> ``` setCollectErrors(false) ``` | By default the complete input is validated and in case of failing validations all error messages will be thrown in a single exception. If set to false the first failing validation will throw an exception. | true
87
89
``` setPrettyPrint(bool $prettyPrint) ``` <br><br>Example:<br> ``` setPrettyPrint(true) ``` | If set to false, the generated model classes won't follow coding guidelines (but the generation is faster). If enabled the package [Symplify/EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard) will be used to clean up the generated code. By default pretty printing is disabled. | false
88
90
``` setSerialization(bool $serialization) ``` <br><br>Example:<br> ``` setSerialization(true) ``` | If set to true the serialization methods `toArray` and `toJSON` will be added to the public interface of the generated classes. | false
To avoid adding all dependencies of the php-json-model-generator to your production dependencies it's recommended to add the library as a dev-dependency and include the php-json-model-generator-exception library. The exception library provides all classes to run the generated code. Generating the classes should either be a step done in the development environment (if you decide to commit the models) or as a build step of your application.
14
+
To avoid adding all dependencies of the php-json-model-generator to your production dependencies it's recommended to add the library as a dev-dependency and include the php-json-model-generator-exception library. The exception library provides all classes to run the generated code. Generating the classes should either be a step done in the development environment or as a build step of your application (for example you could generate the models in a `composer post-autoload-dump script<https://getcomposer.org/doc/articles/scripts.md#command-events>`__, which is the recommended workflow).
15
15
16
16
Generating classes
17
17
------------------
@@ -154,6 +154,22 @@ If set to true the generated model classes will be delivered without setter meth
154
154
(new GeneratorConfiguration())
155
155
->setImmutable(false);
156
156
157
+
Implicit null
158
+
^^^^^^^^^^^^^
159
+
160
+
By default the properties are strictly checked against their defined types. Consequently if you want a property to accept null you have to extend the type of your property explicitly (eg. ['string', 'null']).
161
+
162
+
By setting the implicit null option to true all of your object properties which aren't required will implicitly accept null. All properties which are required and don't explicitly allow null in the type definition will still reject null.
163
+
164
+
.. code-block:: php
165
+
166
+
setImplicitNull(bool $allowImplicitNull);
167
+
168
+
.. code-block:: php
169
+
170
+
(new GeneratorConfiguration())
171
+
->setImplicitNull(true);
172
+
157
173
Collect errors vs. early return
158
174
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
159
175
@@ -209,7 +225,7 @@ Serialization methods
209
225
210
226
setSerialization(bool $serialization);
211
227
212
-
If set to true the serialization methods `toArray`and `toJSON` will be added to the public interface of the generated classes. By default no serialization methods are added.
228
+
If set to true the serialization methods `toArray`, `toJSON` and `jsonSerialize` will be added to the public interface of the generated classes. By default no serialization methods are added.
213
229
214
230
.. code-block:: php
215
231
@@ -222,9 +238,12 @@ Generated interface:
222
238
223
239
public function toArray([int $depth = 512]): array;
224
240
public function toJSON([int $options = 0 [, int $depth = 512]]): string;
241
+
public function jsonSerialize(): array;
225
242
226
243
The generated class will implement the interface **PHPModelGenerator\\Interfaces\\SerializationInterface** implemented in the php-json-schema-model-generator-production repository. This interface can be used to write additional generic modules to handle the generated models. The $depth parameter defines the maximum amount of nested objects which are serialized. The $options parameter for the toJSON method provides access to the underlying option bitmask of `json_encode <https://www.php.net/manual/de/function.json-encode.php>`_.
227
244
245
+
Additionally the class will implement the PHP builtin interface **\JsonSerializable** which allows the direct usage of the generated classes in a custom json_encode.
Copy file name to clipboardExpand all lines: docs/source/nonStandardExtensions/filter.rst
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,7 +80,7 @@ Transforming filter
80
80
81
81
You may keep it simple and skip this for your first tries and only experiment with non-transforming filters like the trim filter
82
82
83
-
Filters may change the type of the property. For example the builtin filter **dateTime** creates a DateTime object. Consequently further validations like pattern checks for the string property won't be performed.
83
+
Filters may change the type of the property. For example the builtin filter **dateTime** creates a DateTime object. Consequently further type-related validations like pattern checks for the string property won't be performed. Additionally enum validations will not be executed if an already transformed value is provided.
84
84
85
85
As the required check is executed before the filter a filter may transform a required value into a null value. Be aware when writing custom filters which transform values to not break your validation rules by adding filters to a property.
86
86
@@ -90,13 +90,15 @@ If you write a custom transforming filter you must define the return type of you
90
90
91
91
The return type of the transforming filter will be used to define the type of the property inside the generated model (in the example one section above given above the method **getCreated** will return a DateTime object). Additionally the generated model also accepts the transformed type as input type. So **setCreated** will accept a string and a DateTime object. If an already transformed value is provided the filter which transforms the value will **not** be executed. Also all filters which are defined before the transformation will **not** be executed (eg. a trim filter before a dateTime filter will not be executed if a DateTime object is provided).
92
92
93
+
If you use a filter on a property which accepts multiple types (eg. explicit null ['string', 'null'] or ['string', 'integer']) the filter must accept each of the types defined on the property.
94
+
93
95
Builtin filter
94
96
--------------
95
97
96
98
trim
97
99
^^^^
98
100
99
-
The trim filter is only valid for string properties.
101
+
The trim filter is only valid for string and null properties.
100
102
101
103
.. code-block:: json
102
104
@@ -140,7 +142,7 @@ If the filter trim is used for a property which doesn't require a string value a
140
142
notEmpty
141
143
^^^^^^^^
142
144
143
-
The dateTime filter is only valid for array properties.
145
+
The dateTime filter is only valid for array and null properties.
144
146
145
147
.. code-block:: json
146
148
@@ -174,7 +176,7 @@ Let's have a look how the generated model behaves:
174
176
dateTime
175
177
^^^^^^^^
176
178
177
-
The dateTime filter is only valid for string properties.
179
+
The dateTime filter is only valid for string and null properties.
178
180
179
181
.. code-block:: json
180
182
@@ -274,9 +276,9 @@ The callable filter method must be a static method. Internally it will be called
274
276
public function getAcceptedTypes(): array
275
277
{
276
278
// return an array of types which can be handled by the filter.
277
-
// valid types are: [integer, number, boolean, string, array] or available classes (FQCN required, eg.
// or available classes (FQCN required, eg. DateTime::class)
281
+
return ['string', 'null'];
280
282
}
281
283
282
284
public function getToken(): string
@@ -290,6 +292,10 @@ The callable filter method must be a static method. Internally it will be called
290
292
}
291
293
}
292
294
295
+
.. hint::
296
+
297
+
If your filter accepts null values add 'null' to your *getAcceptedTypes* to make sure your filter is compatible with explicit null type.
298
+
293
299
.. hint::
294
300
295
301
If a filter with the token of your custom filter already exists the existing filter will be overwritten when adding the filter to the generator configuration. By overwriting filters you may change the behaviour of builtin filters by replacing them with your custom implementation.
@@ -387,12 +393,12 @@ The custom serializer method will be called if the model utilizing the custom fi
Copy file name to clipboardExpand all lines: docs/source/types/null.rst
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,3 +25,5 @@ Generated interface (as null is no explicit type no typehints are generated):
25
25
Possible exceptions:
26
26
27
27
* Invalid type for property. Requires null, got __TYPE__
28
+
29
+
The main use case for the **null** type is a property with `multiple types <complexTypes/multiType.html>`__ accepting for example a string and null values when using explicit null types.
0 commit comments