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
``` setOutputEnabled(bool $outputEnabled) ``` <br><br>Example:<br> ``` setOutputEnabled(false) ``` | Enable or disable output of the generation process to STDOUT | true
82
82
``` setErrorRegistryClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setErrorRegistryClass(CustomException::class) ``` | Define a custom exception implementing the ErrorRegistryExceptionInterface to be used. The exception will be thrown if a validation fails and error collection is **enabled** | ErrorRegistryException::class
83
83
``` setExceptionClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setExceptionClass(CustomException::class) ``` | Define a custom exception to be used. The exception will be thrown if a validation fails and error collection is **disabled** | ValidationException::class
84
+
``` addFilter(FilterInterface $filter) ``` <br><br>Example:<br> ``` addFilter(new CustomFilter()) ``` | Add a custom filter to the generator. Check out the docs for more details. | -
84
85
85
86
## Examples ##
86
87
@@ -127,7 +128,7 @@ Now let's have a look at the behaviour of the generated model:
127
128
```php
128
129
// Throws an exception as the required name isn't provided.
129
130
// Exception: 'Missing required value for name'
130
-
$person = new Person();
131
+
$person = new Person([]);
131
132
132
133
// Throws an exception as the name provides an invalid value.
133
134
// Exception: 'Invalid type for name. Requires string, got int'
Copy file name to clipboardExpand all lines: docs/source/nonStandardExtensions/filter.rst
+59-4Lines changed: 59 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@ Filter
2
2
======
3
3
4
4
Filter can be used to preprocess values. Filters are applied after the required and type validation. If a filter is applied to a property which has a type which is not supported by the filter an exception will be thrown.
5
-
Filters can be either supplied as a string or as a list of filters:
5
+
Filters can be either supplied as a string or as a list of filters (multiple filters can be applied to a single property):
6
6
7
7
.. code-block:: json
8
8
@@ -40,7 +40,7 @@ The trim filter is only valid for string properties.
40
40
"name": {
41
41
"type": "string",
42
42
"filter": "trim",
43
-
"minLength": 2,
43
+
"minLength": 2
44
44
}
45
45
}
46
46
}
@@ -50,7 +50,7 @@ Let's have a look how the generated model behaves:
50
50
.. code-block:: php
51
51
52
52
// valid, the name will be NULL as the name is not required
53
-
$person = new Person();
53
+
$person = new Person([]);
54
54
55
55
// Throws an exception as the name provides an invalid value after being trimmed.
56
56
// Exception: 'Value for name must not be shorter than 2'
@@ -73,7 +73,7 @@ If the filter trim is used for a property which doesn't require a string value a
73
73
Custom filter
74
74
-------------
75
75
76
-
You can implement custom filter and use them in your schema files. You must add your custom filter to the generator configuration.
76
+
You can implement custom filter and use them in your schema files. You must add your custom filter to the generator configuration to make them available.
77
77
78
78
.. code-block:: php
79
79
@@ -83,3 +83,58 @@ You can implement custom filter and use them in your schema files. You must add
83
83
);
84
84
85
85
Your filter must implement the interface **PHPModelGenerator\\PropertyProcessor\\Filter\\FilterInterface**. Make sure the given callable array returned by **getFilter** is accessible as well during the generation process as during code execution using the generated model.
86
+
The callable filter method must be a static method. Internally it will be called via *call_user_func*. A custom filter may look like:
87
+
88
+
.. code-block:: php
89
+
90
+
namespace MyApp\Model\Generator\Filter;
91
+
92
+
use PHPModelGenerator\PropertyProcessor\Filter\FilterInterface;
93
+
94
+
class UppercaseFilter implements FilterInterface
95
+
{
96
+
public static function uppercase(?string $value): ?string
97
+
{
98
+
// we want to handle strings and null values with this filter
0 commit comments