Skip to content

Commit 1bb58df

Browse files
author
Enno Woortmann
committed
Added an option to collect all errors before throwing an exception
Decouple from package-exception
1 parent 590e8ca commit 1bb58df

40 files changed

+225
-183
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Exception;
6+
7+
use Exception;
8+
9+
/**
10+
* Class ErrorRegistryException
11+
*
12+
* @package PHPModelGenerator\Exception
13+
*/
14+
class ErrorRegistryException extends Exception implements ErrorRegistryExceptionInterface
15+
{
16+
protected $errors = [];
17+
18+
public function addError(string $message): ErrorRegistryExceptionInterface
19+
{
20+
$this->errors[] = $message;
21+
return $this;
22+
}
23+
24+
public function getErrors(): array
25+
{
26+
return $this->errors;
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Exception;
6+
7+
use Throwable;
8+
9+
/**
10+
* Class ErrorRegistryExceptionInterface
11+
*
12+
* @package PHPModelGenerator\Exception
13+
*/
14+
interface ErrorRegistryExceptionInterface extends Throwable
15+
{
16+
public function addError(string $message): self;
17+
18+
public function getErrors(): array;
19+
}

src/Model/GeneratorConfiguration.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace PHPModelGenerator\Model;
66

7+
use PHPModelGenerator\Exception\ErrorRegistryException;
8+
use PHPModelGenerator\Exception\InvalidArgumentException;
9+
710
/**
811
* Class GeneratorConfiguration
912
*
@@ -19,6 +22,12 @@ class GeneratorConfiguration
1922
protected $prettyPrint = true;
2023
/** @var bool */
2124
protected $outputEnabled = true;
25+
/** @var bool */
26+
protected $collectErrors = true;
27+
/** @var string */
28+
protected $errorRegistryClass = ErrorRegistryException::class;
29+
/** @var string */
30+
protected $exceptionClass = InvalidArgumentException::class;
2231

2332
/**
2433
* @return string
@@ -95,4 +104,61 @@ public function isOutputEnabled(): bool
95104
{
96105
return $this->outputEnabled;
97106
}
107+
108+
/**
109+
* @return bool
110+
*/
111+
public function collectErrors(): bool
112+
{
113+
return $this->collectErrors;
114+
}
115+
116+
/**
117+
* @param bool $collectErrors
118+
*
119+
* @return GeneratorConfiguration
120+
*/
121+
public function setCollectErrors(bool $collectErrors): self
122+
{
123+
$this->collectErrors = $collectErrors;
124+
return $this;
125+
}
126+
127+
/**
128+
* @return string
129+
*/
130+
public function getErrorRegistryClass(): string
131+
{
132+
return $this->errorRegistryClass;
133+
}
134+
135+
/**
136+
* @param string $errorRegistryClass
137+
*
138+
* @return GeneratorConfiguration
139+
*/
140+
public function setErrorRegistryClass(string $errorRegistryClass): self
141+
{
142+
$this->errorRegistryClass = $errorRegistryClass;
143+
return $this;
144+
}
145+
146+
/**
147+
* @return string
148+
*/
149+
public function getExceptionClass(): string
150+
{
151+
return $this->exceptionClass;
152+
}
153+
154+
/**
155+
* @param string $exceptionClass
156+
*
157+
* @return GeneratorConfiguration
158+
*/
159+
public function setExceptionClass(string $exceptionClass): self
160+
{
161+
$this->exceptionClass = $exceptionClass;
162+
return $this;
163+
}
98164
}

src/Model/Property/Property.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -229,28 +229,6 @@ public function hasDecorators(): bool
229229
return count($this->decorators) > 0;
230230
}
231231

232-
/**
233-
* @inheritdoc
234-
*/
235-
public function getExceptionClasses(): array
236-
{
237-
$use = [];
238-
239-
foreach ($this->getOrderedValidators() as $validator) {
240-
$use[] = $validator->getExceptionClass();
241-
}
242-
243-
foreach ($this->getNestedProperties() as $property) {
244-
$use = array_merge($use, $property->getExceptionClasses());
245-
}
246-
247-
foreach ($this->decorators as $decorator) {
248-
$use = array_merge($use, $decorator->getExceptionClasses());
249-
}
250-
251-
return $use;
252-
}
253-
254232
/**
255233
* Convert a name of a JSON-field into a valid PHP variable name to be used as class attribute
256234
*

src/Model/Property/PropertyInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,6 @@ public function resolveDecorator(string $input): string;
133133
*/
134134
public function hasDecorators(): bool;
135135

136-
/**
137-
* Get a list of all required classes
138-
*
139-
* @return array
140-
*/
141-
public function getExceptionClasses(): array;
142-
143136
/**
144137
* @param bool $isPropertyRequired
145138
*

src/Model/Property/PropertyProxy.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,6 @@ public function hasDecorators(): bool
180180
return $this->getProperty()->hasDecorators();
181181
}
182182

183-
/**
184-
* @inheritdoc
185-
*/
186-
public function getExceptionClasses(): array
187-
{
188-
return $this->getProperty()->getExceptionClasses();
189-
}
190-
191183
/**
192184
* @inheritdoc
193185
*/

src/Model/RenderJob.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ protected function renderClass(GeneratorConfiguration $generatorConfiguration):
9999
$render = new Render(__DIR__ . "/../Templates/");
100100

101101
$namespace = trim($generatorConfiguration->getNamespacePrefix() . $this->classPath, '\\');
102-
$use = $this->schema->getUseList(empty($namespace));
102+
103+
$use = $generatorConfiguration->collectErrors()
104+
? [$generatorConfiguration->getErrorRegistryClass()]
105+
: [$generatorConfiguration->getExceptionClass()];
103106

104107
try {
105108
$class = $render->renderTemplate(
@@ -111,7 +114,7 @@ protected function renderClass(GeneratorConfiguration $generatorConfiguration):
111114
'baseValidators' => $this->schema->getBaseValidators(),
112115
'properties' => $this->schema->getProperties(),
113116
'generatorConfiguration' => $generatorConfiguration,
114-
'viewHelper' => new RenderHelper(),
117+
'viewHelper' => new RenderHelper($generatorConfiguration),
115118
]
116119
);
117120
} catch (PHPMicroTemplateException $exception) {

src/Model/Schema.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,6 @@ public function addBaseValidator(PropertyValidatorInterface $baseValidator)
8080
return $this;
8181
}
8282

83-
/**
84-
* Extract all required uses for the properties of the schema
85-
*
86-
* @param bool $skipGlobalNamespace
87-
*
88-
* @return array
89-
*/
90-
public function getUseList(bool $skipGlobalNamespace): array
91-
{
92-
$use = [];
93-
94-
foreach ($this->getBaseValidators() as $validator) {
95-
$use[] = $validator->getExceptionClass();
96-
}
97-
98-
foreach ($this->getProperties() as $property) {
99-
if (empty($property->getValidators())) {
100-
continue;
101-
}
102-
103-
$use = array_merge($use, [Exception::class], $property->getExceptionClasses());
104-
}
105-
106-
if ($skipGlobalNamespace) {
107-
$use = array_filter($use, function ($namespace) {
108-
return strstr($namespace, '\\');
109-
});
110-
}
111-
112-
return $use;
113-
}
114-
11583
/**
11684
* @return SchemaDefinitionDictionary
11785
*/

src/Model/Validator/AbstractPropertyValidator.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,9 @@
1111
*/
1212
abstract class AbstractPropertyValidator implements PropertyValidatorInterface
1313
{
14-
/** @var string */
15-
protected $exceptionClass;
1614
/** @var string */
1715
protected $exceptionMessage;
1816

19-
/**
20-
* Get the exception class to be thrown if the validation fails
21-
*
22-
* @return string
23-
*/
24-
public function getExceptionClass(): string
25-
{
26-
return $this->exceptionClass;
27-
}
28-
2917
/**
3018
* Get the message of the exception which is thrown if the validation fails
3119
*

src/Model/Validator/ComposedPropertyValidator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPModelGenerator\Exception\InvalidArgumentException;
87
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
98
use PHPModelGenerator\Model\Property\PropertyInterface;
109

@@ -30,7 +29,6 @@ public function __construct(
3029
array $validatorVariables
3130
) {
3231
parent::__construct(
33-
InvalidArgumentException::class,
3432
"Invalid value for {$property->getName()} declined by composition constraint",
3533
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ComposedItem.phptpl',
3634
$validatorVariables

src/Model/Validator/ConditionalPropertyValidator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPModelGenerator\Exception\InvalidArgumentException;
87
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
98
use PHPModelGenerator\Model\Property\PropertyInterface;
109
use PHPModelGenerator\PropertyProcessor\ComposedValue\IfProcessor;
@@ -29,7 +28,6 @@ public function __construct(
2928
array $validatorVariables
3029
) {
3130
parent::__construct(
32-
InvalidArgumentException::class,
3331
"Invalid value for {$property->getName()} declined by conditional composition constraint",
3432
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ConditionalComposedItem.phptpl',
3533
$validatorVariables

src/Model/Validator/PropertyTemplateValidator.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ class PropertyTemplateValidator extends AbstractPropertyValidator
2525
/**
2626
* PropertyValidator constructor.
2727
*
28-
* @param string $exceptionClass
2928
* @param string $exceptionMessage
3029
* @param string $template
3130
* @param array $templateValues
3231
*/
3332
public function __construct(
34-
string $exceptionClass,
3533
string $exceptionMessage,
3634
string $template,
3735
array $templateValues
3836
) {
39-
$this->exceptionClass = $exceptionClass;
4037
$this->exceptionMessage = $exceptionMessage;
4138
$this->template = $template;
4239
$this->templateValues = $templateValues;

src/Model/Validator/PropertyValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ class PropertyValidator extends AbstractPropertyValidator
1818
* PropertyValidator constructor.
1919
*
2020
* @param string $check
21-
* @param string $exceptionClass
2221
* @param string $exceptionMessage
2322
*/
24-
public function __construct(string $check, string $exceptionClass, string $exceptionMessage)
23+
public function __construct(string $check, string $exceptionMessage)
2524
{
2625
$this->check = $check;
27-
$this->exceptionClass = $exceptionClass;
2826
$this->exceptionMessage = $exceptionMessage;
2927
}
3028

src/Model/Validator/PropertyValidatorInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ interface PropertyValidatorInterface
1818
*/
1919
public function getCheck(): string;
2020

21-
/**
22-
* Get the exception class to be thrown if the validation fails
23-
*
24-
* @return string
25-
*/
26-
public function getExceptionClass(): string;
27-
2821
/**
2922
* Get the message of the exception which is thrown if the validation fails
3023
*

src/Model/Validator/RequiredPropertyValidator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPModelGenerator\Exception\InvalidArgumentException;
87
use PHPModelGenerator\Model\Property\PropertyInterface;
98

109
/**
@@ -23,7 +22,6 @@ public function __construct(PropertyInterface $property)
2322
{
2423
parent::__construct(
2524
"!array_key_exists('{$property->getName()}', \$modelData)",
26-
InvalidArgumentException::class,
2725
"Missing required value for {$property->getName()}"
2826
);
2927
}

src/Model/Validator/TypeCheckValidator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7-
use PHPModelGenerator\Exception\InvalidArgumentException;
87
use PHPModelGenerator\Model\Property\PropertyInterface;
98

109
/**
@@ -24,7 +23,6 @@ public function __construct(string $type, PropertyInterface $property)
2423
{
2524
parent::__construct(
2625
'!is_' . strtolower($type) . '($value)' . ($property->isRequired() ? '' : ' && $value !== null'),
27-
InvalidArgumentException::class,
2826
"invalid type for {$property->getName()}"
2927
);
3028
}

0 commit comments

Comments
 (0)