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
Copy file name to clipboardExpand all lines: docs/source/complexTypes/enum.rst
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,10 @@ Enum
3
3
4
4
Enums can be used to define a set of constant values a property must accept.
5
5
6
+
.. hint::
7
+
8
+
If you define constraints via `enum` you may want to use the `EnumPostProcessor <../generator/postProcessor.html#enumpostprocessor>`__ to generate PHP enums.
Copy file name to clipboardExpand all lines: docs/source/generator/postProcessor.rst
+96-1Lines changed: 96 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -204,7 +204,102 @@ The added method **getPatternProperties** can be used to fetch a list of all pro
204
204
205
205
.. note::
206
206
207
-
If you want to add or remove pattern properties to your object after the object instantiation you can use the `AdditionalPropertiesAccessorPostProcessor <generator/postProcessor.html#additionalpropertiesaccessorpostprocessor>`__ or the `PopulatePostProcessor <generator/postProcessor.html#populatepostprocessor>`__
207
+
If you want to modify your object by adding or removing pattern properties after the object instantiation you can use the `AdditionalPropertiesAccessorPostProcessor <postProcessor.html#additionalpropertiesaccessorpostprocessor>`__ or the `PopulatePostProcessor <postProcessor.html#populatepostprocessor>`__
The **EnumPostProcessor** generates a `PHP enum <https://www.php.net/manual/en/language.enumerations.basics.php>`_ for each `enum <../complexTypes/enum.html>`__ found in the processed schemas.
222
+
Enums which contain only integer values or only string values will be rendered into a `backed enum <https://www.php.net/manual/en/language.enumerations.backed.php>`_.
223
+
Other enums will provide the following interface similar to the capabilities of a backed enum:
224
+
225
+
.. code-block:: php
226
+
227
+
public static function from(mixed $value): self;
228
+
public static function tryFrom(mixed $value): ?self;
229
+
230
+
public function value(): mixed;
231
+
232
+
Let's have a look at the most simple case of a string-only enum:
233
+
234
+
.. code-block:: json
235
+
236
+
{
237
+
"$id": "offer",
238
+
"type": "object",
239
+
"properties": {
240
+
"state": {
241
+
"enum": ["open", "sold", "cancelled"]
242
+
}
243
+
}
244
+
}
245
+
246
+
The provided schema will generate the following enum:
247
+
248
+
.. code-block:: php
249
+
250
+
enum OfferState: string {
251
+
case Open = 'open';
252
+
case Sold = 'sold';
253
+
case Cancelled = 'cancelled';
254
+
}
255
+
256
+
The type hints and annotations of the generated class will be changed to match the generated enum:
257
+
258
+
.. code-block:: php
259
+
260
+
/**
261
+
* @param OfferState|string|null $state
262
+
*/
263
+
public function setState($state): self;
264
+
public function getState(): ?OfferState;
265
+
266
+
Mapping
267
+
~~~~~~~
268
+
269
+
Each enum which is not a string-only enum must provide a mapping in the **enum-map** property, for example an integer-only enum:
270
+
271
+
.. code-block:: json
272
+
273
+
{
274
+
"$id": "offer",
275
+
"type": "object",
276
+
"properties": {
277
+
"state": {
278
+
"enum": [0, 1, 2],
279
+
"enum-map": {
280
+
"open": 0,
281
+
"sold": 1,
282
+
"cancelled": 2
283
+
}
284
+
}
285
+
}
286
+
}
287
+
288
+
The provided schema will generate the following enum:
289
+
290
+
.. code-block:: php
291
+
292
+
enum OfferState: int {
293
+
case Open = 0;
294
+
case Sold = 1;
295
+
case Cancelled = 2;
296
+
}
297
+
298
+
If an enum which requires a mapping is found a **SchemaException** will be thrown.
299
+
300
+
.. note::
301
+
302
+
By enabling the *$skipNonMappedEnums* option of the **EnumPostProcessor** you can skip enums which require a mapping but don't provide a mapping. Those enums will provide the default `enum <../complexTypes/enum.html>`__ behaviour.
Copy file name to clipboardExpand all lines: docs/source/nonStandardExtensions/filter.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ Filters may change the type of the property. For example the builtin filter **da
91
91
92
92
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.
93
93
94
-
Only one transforming filter per property is allowed. may be positioned anywhere in the filter chain of a single property. If multiple filters are applied and a transforming filter is among them you have to make sure the property types are compatible. If you use a custom filter after the dateTime filter for example the custom filter has to accept a DateTime value. Filters used before a transforming filter must accept the base type of the property the filter is applied to defined in the schema. If the transformation of a property fails (the transforming filter throws an exception), subsequent filters won't be executed as their execution would add another error due to incompatible types which is irrelevant for the currently provided value.
94
+
Only one transforming filter per property is allowed. The filter may be positioned anywhere in the filter chain of a single property. If multiple filters are applied and a transforming filter is among them you have to make sure the property types are compatible. If you use a custom filter after the dateTime filter for example the custom filter has to accept a DateTime value. Filters used before a transforming filter must accept the base type of the property the filter is applied to defined in the schema. If the transformation of a property fails (the transforming filter throws an exception), subsequent filters won't be executed as their execution would add another error due to incompatible types which is irrelevant for the currently provided value.
95
95
96
96
If you write a custom transforming filter you must define the return type of your filter function as the implementation uses Reflection methods to determine to which type a value is transformed by a filter.
0 commit comments