Skip to content

Commit 973eeac

Browse files
committed
Add properties to merged property without validators
1 parent 9988a6c commit 973eeac

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"php": ">=7.2",
15-
"wol-soft/php-micro-template": "^1.3",
15+
"wol-soft/php-micro-template": "^1.3.1",
1616
"symplify/easy-coding-standard": "^4.1",
1717
"ext-json": "*"
1818
},

src/PropertyProcessor/ComposedValue/AbstractComposedValueProcessor.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ protected function generateValidators(PropertyInterface $property, array $proper
6666

6767
$availableAmount = count($properties);
6868

69-
if ($createMergedProperty) {
70-
$this->createMergedProperty($property, $properties);
71-
}
72-
7369
$property->addValidator(
7470
new ComposedPropertyValidator(
7571
$property,
@@ -84,8 +80,10 @@ protected function generateValidators(PropertyInterface $property, array $proper
8480
// final value after the validations (eg. object instantiations may be performed). Otherwise (eg. a
8581
// NotProcessor) the value must be proposed before the validation
8682
'postPropose' => $this instanceof ComposedPropertiesInterface,
87-
'onlyForDefinedValues' => $propertyData['onlyForDefinedValues'] &&
88-
$this instanceof ComposedPropertiesInterface,
83+
'mergedProperty' =>
84+
$createMergedProperty ? $this->createMergedProperty($property, $properties) : null,
85+
'onlyForDefinedValues' =>
86+
$propertyData['onlyForDefinedValues'] && $this instanceof ComposedPropertiesInterface,
8987
]
9088
),
9189
100
@@ -97,12 +95,12 @@ protected function generateValidators(PropertyInterface $property, array $proper
9795
*
9896
* Gather all nested object properties and merge them together into a single merged property
9997
*
100-
* @param PropertyInterface $property
98+
* @param PropertyInterface $compositionProperty
10199
* @param CompositionPropertyDecorator[] $properties
102100
*
103101
* @return PropertyInterface
104102
*/
105-
private function createMergedProperty(PropertyInterface $property, array $properties): PropertyInterface
103+
private function createMergedProperty(PropertyInterface $compositionProperty, array $properties): PropertyInterface
106104
{
107105
$mergedPropertySchema = new Schema();
108106
$mergedClassName = sprintf('%s_Merged_%s', $this->schemaProcessor->getCurrentClassName(), uniqid());
@@ -111,7 +109,13 @@ private function createMergedProperty(PropertyInterface $property, array $proper
111109
foreach ($properties as $property) {
112110
if ($property->getNestedSchema()) {
113111
foreach ($property->getNestedSchema()->getProperties() as $nestedProperty) {
114-
$mergedPropertySchema->addProperty($nestedProperty);
112+
$mergedPropertySchema->addProperty(
113+
// don't validate fields in merged properties. All fields were validated before corresponding to
114+
// the defined constraints of the composition property.
115+
(clone $nestedProperty)->filterValidators(function () {
116+
return false;
117+
})
118+
);
115119
}
116120
}
117121
}
@@ -122,7 +126,7 @@ private function createMergedProperty(PropertyInterface $property, array $proper
122126
$mergedPropertySchema
123127
);
124128

125-
$property->addTypeHintDecorator(new CompositionTypeHintDecorator($mergedProperty));
129+
$compositionProperty->addTypeHintDecorator(new CompositionTypeHintDecorator($mergedProperty));
126130

127131
return $mergedProperty
128132
->addDecorator(new ObjectInstantiationDecorator($mergedClassName))

src/PropertyProcessor/Property/AbstractPropertyProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected function addComposedValueValidator(PropertyInterface $property, array
112112
new PropertyCollectionProcessor(),
113113
$this->schemaProcessor,
114114
$this->schema,
115-
$property->getName(),
115+
$property->getName() . '_composed_' . uniqid(),
116116
[
117117
'type' => $composedValueKeyword,
118118
'propertyData' => $propertyData,

src/Templates/Validator/ComposedItem.phptpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@
3333
$value = $originalModelData;
3434
{% endforeach %}
3535

36-
$value = $proposedValue;
36+
{% if mergedProperty %}
37+
if (is_object($proposedValue)) {
38+
{{ viewHelper.resolvePropertyDecorator(mergedProperty) }}
39+
} else {
40+
$value = $proposedValue;
41+
}
42+
{% else %}
43+
$value = $proposedValue;
44+
{% endif %}
3745

3846
return !({{ composedValueValidation }});
3947
})($value)

0 commit comments

Comments
 (0)