|
| 1 | +PopulatePostProcessor |
| 2 | +===================== |
| 3 | + |
| 4 | +.. code-block:: php |
| 5 | +
|
| 6 | + $generator = new ModelGenerator(); |
| 7 | + $generator->addPostProcessor(new PopulatePostProcessor()); |
| 8 | +
|
| 9 | +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. |
| 10 | + |
| 11 | +.. code-block:: json |
| 12 | +
|
| 13 | + { |
| 14 | + "$id": "example", |
| 15 | + "type": "object", |
| 16 | + "properties": { |
| 17 | + "example": { |
| 18 | + "type": "string" |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | +
|
| 23 | +Generated interface with the **PopulatePostProcessor**: |
| 24 | + |
| 25 | +.. code-block:: php |
| 26 | +
|
| 27 | + public function getRawModelDataInput(): array; |
| 28 | +
|
| 29 | + public function setExample(float $example): self; |
| 30 | + public function getExample(): float; |
| 31 | +
|
| 32 | + public function populate(array $modelData): self; |
| 33 | +
|
| 34 | +Now let's have a look at the behaviour of the generated model: |
| 35 | + |
| 36 | +.. code-block:: php |
| 37 | +
|
| 38 | + // initialize the model with a valid value |
| 39 | + $example = new Example(['value' => 'Hello World']); |
| 40 | + $example->getRawModelDataInput(); // returns ['value' => 'Hello World'] |
| 41 | +
|
| 42 | + // add an additional property to the model. |
| 43 | + // if additional property constraints are defined in your JSON-Schema |
| 44 | + // each additional property will be validated against the defined constraints. |
| 45 | + $example->populate(['additionalValue' => 12]); |
| 46 | + $example->getRawModelDataInput(); // returns ['value' => 'Hello World', 'additionalValue' => 12] |
| 47 | +
|
| 48 | + // update an existing property with a valid value |
| 49 | + $example->populate(['value' => 'Good night!']); |
| 50 | + $example->getRawModelDataInput(); // returns ['value' => 'Good night!', 'additionalValue' => 12] |
| 51 | +
|
| 52 | + // update an existing property with an invalid value which will throw an exception |
| 53 | + try { |
| 54 | + $example->populate(['value' => false]); |
| 55 | + } catch (Exception $e) { |
| 56 | + // perform error handling |
| 57 | + } |
| 58 | + // if the update of the model fails no values will be updated |
| 59 | + $example->getRawModelDataInput(); // returns ['value' => 'Good night!', 'additionalValue' => 12] |
| 60 | +
|
| 61 | +.. warning:: |
| 62 | + |
| 63 | + 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>`__. |
| 64 | + |
| 65 | +The **PopulatePostProcessor** will also resolve all hooks which are applied to setters. Added code will be executed for all properties changed by a populate call. Schema hooks which implement the **SetterAfterValidationHookInterface** will only be executed if all provided properties pass the validation. |
0 commit comments