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/object.rst
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -184,6 +184,10 @@ Additional Properties
184
184
185
185
Using the keyword `additionalProperties` the object can be limited to not contain any additional properties by providing `false`. If a schema is provided all additional properties must be valid against the provided schema. Simple checks like 'must provide a string' are possible as well as checks like 'must contain an object with a specific structure'.
186
186
187
+
.. hint::
188
+
189
+
If you define constraints via `additionalProperties` you may want to use the `AdditionalPropertiesAccessorPostProcessor <../generator/postProcessor.html#additionalpropertiesaccessorpostprocessor>`__ to access and modify your additional properties.
190
+
187
191
.. code-block:: json
188
192
189
193
{
@@ -243,6 +247,10 @@ The thrown exception will be a *PHPModelGenerator\\Exception\\Object\\InvalidAdd
243
247
// get the value provided to the property
244
248
public function getProvidedValue()
245
249
250
+
.. warning::
251
+
252
+
The validation of additional properties is independently from the `implicit null <../gettingStarted.html#implicit-null>`__ setting. If you require your additional properties to accept null define a `multi type <multiType.html>`__ with explicit null.
The **PopulatePostProcessor** adds a populate method to your generated model. The populate method accepts an array which might contain any subset of the model's properties. All properties present in the provided array will be validated according to the validation rules from the JSON-Schema. If all values are valid the properties will be updated otherwise an exception will be thrown (if error collection is enabled an exception containing all violations, otherwise on the first occurring error, compare `collecting errors <../gettingStarted.html#collect-errors-vs-early-return>`__). Also basic model constraints like `minProperties`, `maxProperties` or `propertyNames` will be validated as the provided array may add additional properties to the model. If the model is updated also the values which can be fetched via `getRawModelDataInput` will be updated.
23
28
24
29
.. code-block:: json
@@ -27,7 +32,7 @@ The **PopulatePostProcessor** adds a populate method to your generated model. Th
27
32
"$id": "example",
28
33
"type": "object",
29
34
"properties": {
30
-
"value": {
35
+
"example": {
31
36
"type": "string"
32
37
}
33
38
}
@@ -74,6 +79,65 @@ Now let's have a look at the behaviour of the generated model:
74
79
75
80
If the **PopulatePostProcessor** is added to your model generator the populate method will be added to the model independently of the `immutable setting <../gettingStarted.html#immutable-classes>`__.
The **AdditionalPropertiesAccessorPostProcessor** adds methods to your model to work with `additional properties <../complexTypes/object.html#additional-properties>`__ on your objects. By default the post processor only adds methods to objects from a schema which defines constraints for additional properties. If the first constructor parameter *$addForModelsWithoutAdditionalPropertiesDefinition* is set to true the methods will also be added to objects generated from a schema which doesn't define additional properties constraints. If the *additionalProperties* keyword in a schema is set to false the methods will never be added.
91
+
92
+
Added methods
93
+
~~~~~~~~~~~~~
94
+
95
+
.. code-block:: json
96
+
97
+
{
98
+
"$id": "example",
99
+
"type": "object",
100
+
"properties": {
101
+
"example": {
102
+
"type": "string"
103
+
}
104
+
},
105
+
"additionalProperties": {
106
+
"type": "string"
107
+
}
108
+
}
109
+
110
+
Generated interface with the **AdditionalPropertiesAccessorPostProcessor**:
111
+
112
+
.. code-block:: php
113
+
114
+
public function getRawModelDataInput(): array;
115
+
116
+
public function setExample(float $example): self;
117
+
public function getExample(): float;
118
+
119
+
public function getAdditionalProperties(): array;
120
+
public function getAdditionalProperty(string $property): ?string;
121
+
public function setAdditionalProperty(string $property, string $value): self;
122
+
public function removeAdditionalProperty(string $property): bool;
123
+
124
+
.. note::
125
+
126
+
The methods **setAdditionalProperty** and **removeAdditionalProperty** are only added if the `immutable setting <../gettingStarted.html#immutable-classes>`__ is set to false.
127
+
128
+
**getAdditionalProperties**: This method returns all additional properties which are currently part of the model as key-value pairs where the key is the property name and the value the current value stored in the model. All other properties which are part of the object (in this case the property *example*) will not be included. In opposite to the *getRawModelDataInput* the values provided via this method are the processed values. This means if the schema provides an object-schema for additional properties an array of object instances will be returned. If the additional properties schema contains `filter <../nonStandardExtensions/filter.html>`__ the filtered (and in case of transforming filter transformed) values will be returned.
129
+
130
+
**getAdditionalProperty**: Returns the current value of a single additional property. If the requested property doesn't exist null will be returned. Returns as well as *getAdditionalProperties* the processed values.
131
+
132
+
**setAdditionalProperty**: Adds or updates an additional property. Performs all necessary validations like property names or min and max properties validations will be performed. If the additional properties are processed via a transforming filter an already transformed value will be accepted. If a property which is regularly defined in the schema a *RegularPropertyAsAdditionalPropertyException* will be thrown. If the change is valid and performed also the output of *getRawModelDataInput* will be updated.
133
+
134
+
**removeAdditionalProperty**: Removes an existing additional property from the model. Returns true if the additional property has been removed, false otherwise (if no additional property with the requested key exists). May throw a *MinPropertiesException* if the change would result in an invalid model state. If the change is valid and performed also the output of *getRawModelDataInput* will be updated.
135
+
136
+
Serialization
137
+
~~~~~~~~~~~~~
138
+
139
+
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.
Copy file name to clipboardExpand all lines: docs/source/gettingStarted.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
@@ -247,6 +247,10 @@ The generated class will implement the interface **PHPModelGenerator\\Interfaces
247
247
248
248
Additionally the class will implement the PHP builtin interface **\JsonSerializable** which allows the direct usage of the generated classes in a custom json_encode.
249
249
250
+
.. warning::
251
+
252
+
If you provide `additional properties <complexTypes/object.html#additional-properties>`__ you may want to use the `AdditionalPropertiesAccessorPostProcessor <generator/postProcessor.html#additionalpropertiesaccessorpostprocessor>`__ as the additional properties by default aren't included into the serialization result.
0 commit comments