Skip to content

Commit 26d8b02

Browse files
committed
Don't cache property validation state for composed validations for immutable objects (fixes #49)
1 parent 5ec7390 commit 26d8b02

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

src/Templates/Validator/ComposedItem.phptpl

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,47 @@
1111
$succeededCompositionElements = {{ availableAmount }};
1212
$validatorComponentIndex = 0;
1313
$originalModelData = $value;
14-
$originalPropertyValidationState = $this->_propertyValidationState ?? [];
1514
$proposedValue = null;
1615

16+
{% if not generatorConfiguration.isImmutable() %}
17+
$originalPropertyValidationState = $this->_propertyValidationState ?? [];
18+
{% endif %}
19+
1720
{% if generatorConfiguration.collectErrors() %}
1821
$originalErrorRegistry = $this->_errorRegistry;
1922
{% endif %}
2023

2124
{% foreach compositionProperties as compositionProperty %}
2225
try {
23-
// check if the state of the validator is already known.
24-
// If none of the properties affected by the validator are changed the validator must not be re-evaluated
25-
if (isset($validatorIndex) &&
26-
isset($this->_propertyValidationState[$validatorIndex][$validatorComponentIndex]) &&
27-
!array_intersect(
28-
array_keys($modifiedModelData),
29-
[
30-
{% foreach compositionProperty.getAffectedObjectProperties() as affectedObjectProperty %}
31-
'{{ affectedObjectProperty.getName() }}',
32-
{% endforeach %}
33-
]
34-
)
35-
) {
36-
{% if generatorConfiguration.collectErrors() %}
37-
$compositionErrorCollection[] = $this->_propertyValidationState[$validatorIndex][$validatorComponentIndex];
38-
{% endif %}
39-
40-
if ({% if generatorConfiguration.collectErrors() %}
41-
$this->_propertyValidationState[$validatorIndex][$validatorComponentIndex]->getErrors()
42-
{% else %}
43-
$this->_propertyValidationState[$validatorIndex][$validatorComponentIndex] !== true
44-
{% endif %}
26+
{% if not generatorConfiguration.isImmutable() %}
27+
// check if the state of the validator is already known.
28+
// If none of the properties affected by the validator are changed the validator must not be re-evaluated
29+
if (isset($validatorIndex) &&
30+
isset($this->_propertyValidationState[$validatorIndex][$validatorComponentIndex]) &&
31+
!array_intersect(
32+
array_keys($modifiedModelData),
33+
[
34+
{% foreach compositionProperty.getAffectedObjectProperties() as affectedObjectProperty %}
35+
'{{ affectedObjectProperty.getName() }}',
36+
{% endforeach %}
37+
]
38+
)
4539
) {
46-
throw new \Exception();
47-
}
48-
} else {
40+
{% if generatorConfiguration.collectErrors() %}
41+
$compositionErrorCollection[] = $this->_propertyValidationState[$validatorIndex][$validatorComponentIndex];
42+
{% endif %}
43+
44+
if ({% if generatorConfiguration.collectErrors() %}
45+
$this->_propertyValidationState[$validatorIndex][$validatorComponentIndex]->getErrors()
46+
{% else %}
47+
$this->_propertyValidationState[$validatorIndex][$validatorComponentIndex] !== true
48+
{% endif %}
49+
) {
50+
throw new \Exception();
51+
}
52+
} else {
53+
{% endif %}
54+
4955
{% if generatorConfiguration.collectErrors() %}
5056
// collect errors for each composition element
5157
$this->_errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
@@ -66,9 +72,12 @@
6672

6773
{% if generatorConfiguration.collectErrors() %}
6874
$compositionErrorCollection[] = $this->_errorRegistry;
69-
if (isset($validatorIndex)) {
70-
$this->_propertyValidationState[$validatorIndex][$validatorComponentIndex] = $this->_errorRegistry;
71-
}
75+
76+
{% if not generatorConfiguration.isImmutable() %}
77+
if (isset($validatorIndex)) {
78+
$this->_propertyValidationState[$validatorIndex][$validatorComponentIndex] = $this->_errorRegistry;
79+
}
80+
{% endif %}
7281

7382
// an error inside the composed validation occurred. Throw an exception to count the validity of the
7483
// composition item
@@ -81,12 +90,14 @@
8190
$proposedValue = $proposedValue ?? $value;
8291
{% endif %}
8392

93+
{% if not generatorConfiguration.isImmutable() %}
8494
{% if not generatorConfiguration.collectErrors() %}
8595
if (isset($validatorIndex)) {
8696
$this->_propertyValidationState[$validatorIndex][$validatorComponentIndex] = true;
8797
}
8898
{% endif %}
8999
}
100+
{% endif %}
90101
} catch (\Exception $e) {
91102
{% if not generatorConfiguration.collectErrors() %}
92103
if (isset($validatorIndex)) {
@@ -121,9 +132,11 @@
121132

122133
$result = !({{ composedValueValidation }});
123134

124-
if ($result) {
125-
$this->_propertyValidationState = $originalPropertyValidationState;
126-
}
135+
{% if not generatorConfiguration.isImmutable() %}
136+
if ($result) {
137+
$this->_propertyValidationState = $originalPropertyValidationState;
138+
}
139+
{% endif %}
127140

128141
return $result;
129142
})($value)

0 commit comments

Comments
 (0)