Skip to content

Commit 892f4a5

Browse files
committed
Don't pass an error registry exception into a nested object and add errors to the passed exception. Instead always throw an exception from generated objects and catch those exceptions inside a parent model.
1 parent 632e4d7 commit 892f4a5

17 files changed

+62
-63
lines changed

src/Model/RenderJob.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,25 @@ class RenderJob
2727
protected $classPath;
2828
/** @var string */
2929
protected $fileName;
30-
/** @var bool */
31-
protected $initialClass;
3230

3331
/**
3432
* Create a new class render job
3533
*
36-
* @param string $fileName The file name
37-
* @param string $classPath The relative path of the class for namespace generation
38-
* @param string $className The class name
39-
* @param Schema $schema The Schema object which holds properties and validators
40-
* @param bool $initialClass Render job for an initial class or render job for a nested class?
34+
* @param string $fileName The file name
35+
* @param string $classPath The relative path of the class for namespace generation
36+
* @param string $className The class name
37+
* @param Schema $schema The Schema object which holds properties and validators
4138
*/
4239
public function __construct(
4340
string $fileName,
4441
string $classPath,
4542
string $className,
46-
Schema $schema,
47-
bool $initialClass
43+
Schema $schema
4844
) {
4945
$this->fileName = $fileName;
5046
$this->classPath = $classPath;
5147
$this->className = $className;
5248
$this->schema = $schema;
53-
$this->initialClass = $initialClass;
5449
}
5550

5651
/**
@@ -151,7 +146,6 @@ protected function renderClass(GeneratorConfiguration $generatorConfiguration):
151146
'schema' => $this->schema,
152147
'generatorConfiguration' => $generatorConfiguration,
153148
'viewHelper' => new RenderHelper($generatorConfiguration),
154-
'initialClass' => $this->initialClass,
155149
// one hack a day keeps the problems away. Make true literal available for the templating. Easy fix
156150
'true' => true,
157151
]

src/Model/Validator/AbstractPropertyValidator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPModelGenerator\Model\Property\Property;
87
use PHPModelGenerator\Model\Property\PropertyInterface;
98
use PHPModelGenerator\Model\Validator;
109

@@ -20,6 +19,18 @@ abstract class AbstractPropertyValidator implements PropertyValidatorInterface
2019
/** @var array */
2120
protected $exceptionParams;
2221

22+
/**
23+
* AbstractPropertyValidator constructor.
24+
*
25+
* @param string $exceptionClass
26+
* @param array $exceptionParams
27+
*/
28+
public function __construct(string $exceptionClass, array $exceptionParams = [])
29+
{
30+
$this->exceptionClass = $exceptionClass;
31+
$this->exceptionParams = $exceptionParams;
32+
}
33+
2334
/**
2435
* @inheritDoc
2536
*/

src/Model/Validator/InstanceOfValidator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class InstanceOfValidator extends PropertyValidator
2222
public function __construct(PropertyInterface $property)
2323
{
2424
parent::__construct(
25-
sprintf('is_object($value) && !($value instanceof %s)', $property->getType()),
25+
sprintf(
26+
'is_object($value) && !($value instanceof \Exception) && !($value instanceof %s)',
27+
$property->getType()
28+
),
2629
InvalidInstanceOfException::class,
2730
[$property->getName(), $property->getType()]
2831
);

src/Model/Validator/PropertyTemplateValidator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public function __construct(
3939
$this->template = $template;
4040
$this->templateValues = $templateValues;
4141

42-
$this->exceptionClass = $exceptionClass;
43-
$this->exceptionParams = $exceptionParams;
42+
parent::__construct($exceptionClass, $exceptionParams);
4443
}
4544

4645
/**

src/Model/Validator/PropertyValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class PropertyValidator extends AbstractPropertyValidator
2424
public function __construct(string $check, string $exceptionClass, array $exceptionParams = [])
2525
{
2626
$this->check = $check;
27-
$this->exceptionClass = $exceptionClass;
28-
$this->exceptionParams = $exceptionParams;
27+
28+
parent::__construct($exceptionClass, $exceptionParams);
2929
}
3030

3131
/**

src/PropertyProcessor/Decorator/Property/ObjectInstantiationDecorator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPMicroTemplate\Render;
88
use PHPModelGenerator\Model\GeneratorConfiguration;
99
use PHPModelGenerator\Model\Property\PropertyInterface;
10-
use PHPModelGenerator\Utils\RenderHelper;
1110

1211
/**
1312
* Class ObjectInstantiationDecorator
@@ -46,12 +45,15 @@ public function __construct(string $className, GeneratorConfiguration $generator
4645
*/
4746
public function decorate(string $input, PropertyInterface $property): string
4847
{
48+
$template = $this->generatorConfiguration->collectErrors()
49+
? 'ObjectInstantiationDecoratorErrorRegistry.phptpl'
50+
: 'ObjectInstantiationDecoratorDirectException.phptpl';
51+
4952
return static::$renderer->renderTemplate(
50-
DIRECTORY_SEPARATOR . 'Decorator' . DIRECTORY_SEPARATOR . 'ObjectInstantiationDecorator.phptpl',
53+
DIRECTORY_SEPARATOR . 'Decorator' . DIRECTORY_SEPARATOR . $template,
5154
[
5255
'input' => $input,
5356
'className' => $this->className,
54-
'generatorConfiguration' => $this->generatorConfiguration,
5557
]
5658
);
5759
}

src/SchemaProcessor/PostProcessor/Templates/Populate.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function populate(array $modelData): self
1212
{
1313
{% if generatorConfiguration.collectErrors() %}
1414
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
15-
{% endif%}
15+
{% endif %}
1616
$rollbackValues = [];
1717

1818
{% if schema.getBaseValidators() %}

src/SchemaProcessor/PostProcessor/Templates/RemoveAdditionalProperty.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function removeAdditionalProperty(string $property): bool
1717
{% if minPropertyValidator %}
1818
{% if generatorConfiguration.collectErrors() %}
1919
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
20-
{% endif%}
20+
{% endif %}
2121

2222
if ({{ minPropertyValidator.getCheck() }}) {
2323
{{ viewHelper.validationError(minPropertyValidator) }}

src/SchemaProcessor/SchemaProcessor.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected function generateModel(
162162
$jsonSchema->withJson($json)
163163
);
164164

165-
$this->generateClassFile($classPath, $className, $schema, $initialClass);
165+
$this->generateClassFile($classPath, $className, $schema);
166166

167167
return $schema;
168168
}
@@ -173,20 +173,18 @@ protected function generateModel(
173173
* @param string $classPath
174174
* @param string $className
175175
* @param Schema $schema
176-
* @param bool $initialClass
177176
*/
178177
public function generateClassFile(
179178
string $classPath,
180179
string $className,
181-
Schema $schema,
182-
bool $initialClass = false
180+
Schema $schema
183181
): void {
184182
$fileName = join(
185183
DIRECTORY_SEPARATOR,
186184
[$this->destination, str_replace('\\', DIRECTORY_SEPARATOR, $classPath), $className]
187185
) . '.php';
188186

189-
$this->renderQueue->addRenderJob(new RenderJob($fileName, $classPath, $className, $schema, $initialClass));
187+
$this->renderQueue->addRenderJob(new RenderJob($fileName, $classPath, $className, $schema));
190188

191189
if ($this->generatorConfiguration->isOutputEnabled()) {
192190
echo "Generated class {$this->generatorConfiguration->getNamespacePrefix()}\\$classPath\\$className\n";

src/Templates/Decorator/ObjectInstantiationDecorator.phptpl

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(is_array($param = {{ input }}) ? new {{ className }}($param) : $param)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(function ($value) {
2+
try {
3+
return is_array($value) ? new {{ className }}($value) : $value;
4+
} catch (\Exception $instantiationException) {
5+
foreach($instantiationException->getErrors() as $nestedValidationError) {
6+
$this->errorRegistry->addError($nestedValidationError);
7+
}
8+
9+
return $instantiationException;
10+
}
11+
})({{ input }})

src/Templates/Model.phptpl

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,14 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
3636
* {{ class }} constructor.
3737
*
3838
* @param array $modelData
39-
{% if generatorConfiguration.collectErrors() %}{% if not initialClass %}* @param {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }} $errorRegistry{% endif %}{% endif %}
4039
*
4140
* @throws {% if generatorConfiguration.collectErrors() %}{{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}{% else %}ValidationException{% endif %}
4241
*/
43-
public function __construct(
44-
array $modelData = []
42+
public function __construct(array $modelData = [])
43+
{
4544
{% if generatorConfiguration.collectErrors() %}
46-
{% if not initialClass %}
47-
, {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }} $errorRegistry = null
48-
{% endif %}
45+
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
4946
{% endif %}
50-
) {
51-
{% if generatorConfiguration.collectErrors() %}
52-
{% if initialClass %}
53-
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
54-
{% else %}
55-
$this->errorRegistry = $errorRegistry;
56-
{% endif %}
57-
{% endif%}
5847

5948
{% if schema.getBaseValidators() %}
6049
$this->executeBaseValidators($modelData);
@@ -65,12 +54,10 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
6554
{% endforeach %}
6655

6756
{% if generatorConfiguration.collectErrors() %}
68-
{% if initialClass %}
69-
if (count($this->errorRegistry->getErrors())) {
70-
throw $this->errorRegistry;
71-
}
72-
{% endif%}
73-
{% endif%}
57+
if (count($this->errorRegistry->getErrors())) {
58+
throw $this->errorRegistry;
59+
}
60+
{% endif %}
7461

7562
$this->rawModelDataInput = $modelData;
7663
}
@@ -130,15 +117,15 @@ class {{ class }} {% if schema.getInterfaces() %}implements {{ viewHelper.joinCl
130117

131118
{% if generatorConfiguration.collectErrors() %}
132119
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
133-
{% endif%}
120+
{% endif %}
134121

135122
$value = $this->validate{{ viewHelper.ucfirst(property.getAttribute()) }}($value, $modelData);
136123

137124
{% if generatorConfiguration.collectErrors() %}
138125
if ($this->errorRegistry->getErrors()) {
139126
throw $this->errorRegistry;
140127
}
141-
{% endif%}
128+
{% endif %}
142129

143130
$this->{{ property.getAttribute() }} = $value;
144131

src/Templates/Validator/ArrayContains.phptpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ is_array($value) && (function (&$items) {
55

66
{% if generatorConfiguration.collectErrors() %}
77
$originalErrorRegistry = $this->errorRegistry;
8-
{% endif%}
8+
{% endif %}
99

1010
foreach ($items as &$value) {
1111
try {
@@ -28,7 +28,7 @@ is_array($value) && (function (&$items) {
2828
}
2929

3030
$this->errorRegistry = $originalErrorRegistry;
31-
{% endif%}
31+
{% endif %}
3232

3333
// one matched item is enough to pass the contains check
3434
return false;
@@ -39,7 +39,7 @@ is_array($value) && (function (&$items) {
3939

4040
{% if generatorConfiguration.collectErrors() %}
4141
$this->errorRegistry = $originalErrorRegistry;
42-
{% endif%}
42+
{% endif %}
4343

4444
return true;
4545
})($value)

src/Templates/Validator/ArrayItem.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
is_array($value) && (function (&$items) use (&$invalidItems{{ suffix }}) {
22
{% if generatorConfiguration.collectErrors() %}
33
$originalErrorRegistry = $this->errorRegistry;
4-
{% endif%}
4+
{% endif %}
55

66
foreach ($items as $index => &$value) {
77
{% if generatorConfiguration.collectErrors() %}
88
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
9-
{% endif%}
9+
{% endif %}
1010

1111
try {
1212
{{ viewHelper.resolvePropertyDecorator(nestedProperty) }}

src/Templates/Validator/ArrayTuple.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
is_array($value) && (function (&$items) use (&$invalidTuples) {
22
{% if generatorConfiguration.collectErrors() %}
33
$originalErrorRegistry = $this->errorRegistry;
4-
{% endif%}
4+
{% endif %}
55

66
$index = 0;
77
{% foreach tupleProperties as tuple %}

src/Templates/Validator/SchemaDependency.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ array_key_exists('{{ propertyName }}', $modelData) && (function () use ($modelDa
44
$this->errorRegistry = new {{ viewHelper.getSimpleClassName(generatorConfiguration.getErrorRegistryClass()) }}();
55
{% else %}
66
try {
7-
{% endif%}
7+
{% endif %}
88

99
$value = $modelData;
1010
{{ viewHelper.resolvePropertyDecorator(nestedProperty) }}

0 commit comments

Comments
 (0)