Skip to content

Commit c121ae2

Browse files
committed
extend custom filter documentation
1 parent a13f4ad commit c121ae2

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Method | Configuration | Default
8181
``` setOutputEnabled(bool $outputEnabled) ``` <br><br>Example:<br> ``` setOutputEnabled(false) ``` | Enable or disable output of the generation process to STDOUT | true
8282
``` 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
8383
``` 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. | -
8485

8586
## Examples ##
8687

@@ -127,7 +128,7 @@ Now let's have a look at the behaviour of the generated model:
127128
```php
128129
// Throws an exception as the required name isn't provided.
129130
// Exception: 'Missing required value for name'
130-
$person = new Person();
131+
$person = new Person([]);
131132

132133
// Throws an exception as the name provides an invalid value.
133134
// Exception: 'Invalid type for name. Requires string, got int'

docs/source/gettingStarted.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Now let's have a look at the behaviour of the generated model:
8787
8888
// Throws an exception as the required name isn't provided.
8989
// Exception: 'Missing required value for name'
90-
$person = new Person();
90+
$person = new Person([]);
9191
9292
// Throws an exception as the name provides an invalid value.
9393
// Exception: 'Invalid type for name. Requires string, got int'
@@ -236,3 +236,12 @@ The output of a generation process may look like:
236236
Duplicated signature 444fd086d8d1f186145a6f81a3ac3f7a for class Register_Message. Redirecting to Login_Message
237237
Rendered class MyApp\User\Response\Login
238238
Rendered class MyApp\User\Response\Register
239+
240+
Custom filter
241+
^^^^^^^^^^^^^
242+
243+
.. code-block:: php
244+
245+
addFilter(FilterInterface $customFilter);
246+
247+
Add a custom filter to the generator. For more details see `Filter <nonStandardExtensions/filter.html>`__.

docs/source/nonStandardExtensions/filter.rst

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Filter
22
======
33

44
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):
66

77
.. code-block:: json
88
@@ -40,7 +40,7 @@ The trim filter is only valid for string properties.
4040
"name": {
4141
"type": "string",
4242
"filter": "trim",
43-
"minLength": 2,
43+
"minLength": 2
4444
}
4545
}
4646
}
@@ -50,7 +50,7 @@ Let's have a look how the generated model behaves:
5050
.. code-block:: php
5151
5252
// valid, the name will be NULL as the name is not required
53-
$person = new Person();
53+
$person = new Person([]);
5454
5555
// Throws an exception as the name provides an invalid value after being trimmed.
5656
// 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
7373
Custom filter
7474
-------------
7575

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.
7777

7878
.. code-block:: php
7979
@@ -83,3 +83,58 @@ You can implement custom filter and use them in your schema files. You must add
8383
);
8484
8585
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
99+
return $value !== null ? strtoupper($value) : null;
100+
}
101+
102+
public function getAcceptedTypes(): array
103+
{
104+
return ['string'];
105+
}
106+
107+
public function getToken(): string
108+
{
109+
return 'uppercase';
110+
}
111+
112+
public function getFilter(): array
113+
{
114+
return [self::class, 'uppercase'];
115+
}
116+
}
117+
118+
If the custom filter is added to the generator configuration you can now use the filter in your schema and the generator will resolve the function:
119+
120+
121+
.. code-block:: json
122+
123+
{
124+
"$id": "person",
125+
"type": "object",
126+
"properties": {
127+
"name": {
128+
"type": "string",
129+
"filter": [
130+
"uppercase",
131+
"trim"
132+
]
133+
}
134+
}
135+
}
136+
137+
.. code-block:: php
138+
139+
$person = new Person(['name' => ' Albert ']);
140+
$person->getName(); // returns 'ALBERT'

0 commit comments

Comments
 (0)