Skip to content

Commit 941b1ec

Browse files
author
Enno Woortmann
committed
ComposedValueProcessor
1 parent 21d5495 commit 941b1ec

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PHPModelGenerator\PropertyProcessor\Property;
4+
5+
use PHPModelGenerator\Exception\InvalidArgumentException;
6+
use PHPModelGenerator\Model\Property\PropertyInterface;
7+
use PHPModelGenerator\Model\Validator\PropertyTemplateValidator;
8+
use PHPModelGenerator\PropertyProcessor\PropertyCollectionProcessor;
9+
use PHPModelGenerator\PropertyProcessor\PropertyFactory;
10+
use PHPModelGenerator\Utils\RenderHelper;
11+
12+
/**
13+
* Class AbstractComposedValueProcessor
14+
*
15+
* @package PHPModelGenerator\PropertyProcessor\Property
16+
*/
17+
abstract class AbstractComposedValueProcessor extends AbstractNestedValueProcessor
18+
{
19+
protected const COMPOSED_VALUE_VALIDATION = '';
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
protected function generateValidators(PropertyInterface $property, array $propertyData): void
25+
{
26+
$propertyFactory = new PropertyFactory();
27+
28+
$properties = [];
29+
foreach ($propertyData as $compositionElement) {
30+
$properties[] = $propertyFactory
31+
->create(
32+
new PropertyCollectionProcessor(),
33+
$this->schemaProcessor,
34+
$this->schema,
35+
$property->getName(),
36+
$compositionElement
37+
)
38+
->setRequired($property->isRequired());
39+
}
40+
41+
$property->addValidator(
42+
new PropertyTemplateValidator(
43+
InvalidArgumentException::class,
44+
'Invalid composed item',
45+
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ComposedItem.phptpl',
46+
[
47+
'properties' => $properties,
48+
'viewHelper' => new RenderHelper(),
49+
'availableAmount' => count($properties),
50+
'composedValueValidation' => static::COMPOSED_VALUE_VALIDATION,
51+
]
52+
)
53+
);
54+
}
55+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(function ($value) {
2+
$succeededCompositionElements = {{ availableAmount }};
3+
{% foreach properties as property %}
4+
try {
5+
{{ viewHelper.resolvePropertyDecorator(property) }}
6+
7+
{% foreach property.getOrderedValidators() as validator %}
8+
if ({{ validator.getCheck() }}) {
9+
throw new {{ viewHelper.getSimpleClassName(validator.getExceptionClass()) }}('{{ validator.getExceptionMessage() }}');
10+
}
11+
{% endforeach %}
12+
} catch (Exception $e) {
13+
$succeededCompositionElements--;
14+
}
15+
{% endforeach %}
16+
17+
if ({{ composedValueValidation }}) {
18+
return true;
19+
}
20+
21+
return false;
22+
})($value)

0 commit comments

Comments
 (0)