Skip to content

Commit ea4d65f

Browse files
committed
Update docs
1 parent f16f554 commit ea4d65f

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

docs/source/complexTypes/object.rst

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,48 @@ Multiple violations against the schema dependency may be included.
514514
Pattern Properties
515515
------------------
516516

517-
Pattern properties are currently not supported.
517+
Using the keyword `patternProperties` further restrictions for properties matching a pattern can be defined.
518+
519+
.. hint::
520+
521+
If you define constraints via `patternProperties` you may want to use the `PatternPropertiesAccessorPostProcessor <../generator/postProcessor.html#patternpropertiesaccessorpostprocessor>`__ to access your pattern properties.
522+
523+
.. code-block:: json
524+
525+
{
526+
"$id": "example",
527+
"type": "object",
528+
"properties": {
529+
"example": {
530+
"type": "integer"
531+
}
532+
},
533+
"patternProperties": {
534+
"^a": {
535+
"type": "string"
536+
}
537+
}
538+
}
539+
540+
Possible exceptions:
541+
542+
If invalid pattern properties are provided a detailed exception will be thrown containing all violations:
543+
544+
.. code-block:: none
545+
546+
Provided JSON for Example contains invalid pattern properties.
547+
- invalid property 'a0' matching pattern '\^a'
548+
* Invalid type for pattern property. Requires string, got integer
549+
550+
The thrown exception will be a *PHPModelGenerator\\Exception\\Object\\InvalidPatternPropertiesException* which provides the following methods to get further error details:
551+
552+
.. code-block:: php
553+
554+
// returns a two-dimensional array which contains all validation exceptions grouped by property names
555+
public function getNestedExceptions(): array
556+
// get the pattern which lead to the error
557+
public function getPattern(): string
558+
// get the name of the property which failed
559+
public function getPropertyName(): string
560+
// get the value provided to the property
561+
public function getProvidedValue()

docs/source/generator/postProcessor.rst

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,68 @@ Generated interface with the **AdditionalPropertiesAccessorPostProcessor**:
143143
Serialization
144144
~~~~~~~~~~~~~
145145

146-
By default additional properties are not included in serialized models. If the **AdditionalPropertiesAccessorPostProcessor** is applied and `serialization <../gettingStarted.html#serialization-methods>`__ is enabled the additional properties will be merged into the serialization result. If the additional properties are processed via a transforming filter each value will be serialized via the serialisation method of the transforming filter.
146+
By default additional properties are only included in the serialized models if the *additionalProperties* field is set to true or contains further restrictions. If the option *$addForModelsWithoutAdditionalPropertiesDefinition* is set to true also additional properties for entities which don't define the *additionalProperties* field will be included in the serialization result. If the **AdditionalPropertiesAccessorPostProcessor** is applied and `serialization <../gettingStarted.html#serialization-methods>`__ is enabled the additional properties will be merged into the serialization result. If the additional properties are processed via a transforming filter each value will be serialized via the serialisation method of the transforming filter.
147+
148+
PatternPropertiesAccessorPostProcessor
149+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
150+
151+
.. code-block:: php
152+
153+
$generator = new ModelGenerator();
154+
$generator->addPostProcessor(new PatternPropertiesAccessorPostProcessor());
155+
156+
The **PatternPropertiesAccessorPostProcessor** adds methods to your model to work with `pattern properties <../complexTypes/object.html#pattern-properties>`__ on your objects. The methods will only be added if the schema for the object defines pattern properties.
157+
158+
Added methods
159+
~~~~~~~~~~~~~
160+
161+
.. code-block:: json
162+
163+
{
164+
"$id": "example",
165+
"type": "object",
166+
"properties": {
167+
"example": {
168+
"type": "string"
169+
}
170+
},
171+
"patternProperties": {
172+
"^a": {
173+
"type": "string"
174+
},
175+
"^b": {
176+
"key": "numbers"
177+
"type": "integer"
178+
},
179+
}
180+
}
181+
182+
Generated interface with the **AdditionalPropertiesAccessorPostProcessor**:
183+
184+
.. code-block:: php
185+
186+
public function getRawModelDataInput(): array;
187+
188+
public function setExample(float $example): self;
189+
public function getExample(): float;
190+
191+
public function getPatternProperties(string $key): array;
192+
193+
The added method **getPatternProperties** can be used to fetch a list of all properties matching the given pattern. As *$key* you have to provide the pattern you want to fetch. Alternatively you can define a key in your schema and use the key to fetch the properties.
194+
195+
.. code-block:: php
196+
197+
$myObject = new Example('a1' => 'Hello', 'b1' => 100);
198+
199+
// fetches all properties matching the pattern '^a', consequently will return ['a1' => 'Hello']
200+
$myObject->getPatternProperties('^a');
201+
202+
// fetches all properties matching the pattern '^b' (which has a defined key), consequently will return ['b1' => 100]
203+
$myObject->getPatternProperties('numbers');
204+
205+
.. note::
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>`__
147208

148209
Custom Post Processors
149210
----------------------

0 commit comments

Comments
 (0)