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.

0 commit comments

Comments
 (0)