Skip to content

Commit e33ffa0

Browse files
author
Enno Woortmann
committed
Rework remaining exceptions
1 parent c65652d commit e33ffa0

26 files changed

+114
-118
lines changed

docs/source/combinedSchemas/allOf.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Possible exception (if a string is provided):
3939
.. code-block:: none
4040
4141
Invalid value for example declined by composition constraint.
42-
Requires to match 2 composition elements but matched 0 elements.
42+
Requires to match all composition elements but matched 0 elements.
4343
- Composition element #1: Failed
4444
* Invalid type for example. Requires float, got string
4545
- Composition element #2: Failed

docs/source/complexTypes/array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ With a schema like this all items must contain a string with at least two charac
5656

5757
.. code-block:: none
5858
59-
Invalid item in array example:
59+
Invalid items in array example:
6060
- invalid item #3
6161
* Invalid type for item of array example. Requires string, got double
6262

docs/source/complexTypes/multiType.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ For example if an array **["Hello", 123, "Goodbye"]** is given the validation wi
5454

5555
.. code-block:: none
5656
57-
Invalid item in array example:
57+
Invalid items in array example:
5858
- invalid item #1
5959
* Invalid type for item of array example. Requires string, got integer

docs/source/generic/combinedException.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,30 @@ The exception which will be thrown (combined array-exception and combined-schema
6060

6161
.. code-block:: none
6262
63-
Invalid item in array property:
63+
Invalid items in array property:
6464
- invalid item #0
6565
* Invalid value for item of array property declined by composition constraint.
66-
Requires to match 2 composition elements but matched 1 elements.
66+
Requires to match all composition elements but matched 1 elements.
6767
- Composition element #1: Failed
6868
* Invalid type for name. Requires string, got boolean
6969
- Composition element #2: Valid
7070
- invalid item #1
7171
* Invalid value for item of array property declined by composition constraint.
72-
Requires to match 2 composition elements but matched 0 elements.
72+
Requires to match all composition elements but matched 0 elements.
7373
- Composition element #1: Failed
7474
* Value for name must not be shorter than 2
7575
- Composition element #2: Failed
7676
* Invalid type for age. Requires int, got string
7777
- invalid item #2
7878
* Invalid value for item of array property declined by composition constraint.
79-
Requires to match 2 composition elements but matched 0 elements.
79+
Requires to match all composition elements but matched 0 elements.
8080
- Composition element #1: Failed
8181
* Invalid type for item of array property. Requires object, got integer
8282
- Composition element #2: Failed
8383
* Invalid type for item of array property. Requires object, got integer
8484
- invalid item #3
8585
* Invalid value for item of array property declined by composition constraint.
86-
Requires to match 2 composition elements but matched 1 elements.
86+
Requires to match all composition elements but matched 1 elements.
8787
- Composition element #1: Failed
8888
* Missing required value for name
8989
* Invalid type for name. Requires string, got NULL

src/Model/Validator/ConditionalPropertyValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\ComposedValue\ConditionalException;
78
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
89
use PHPModelGenerator\Model\Property\PropertyInterface;
910
use PHPModelGenerator\PropertyProcessor\ComposedValue\IfProcessor;
@@ -28,9 +29,10 @@ public function __construct(
2829
array $validatorVariables
2930
) {
3031
parent::__construct(
31-
"Invalid value for {$property->getName()} declined by conditional composition constraint",
3232
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ConditionalComposedItem.phptpl',
33-
$validatorVariables
33+
$validatorVariables,
34+
ConditionalException::class,
35+
[$property->getName()]
3436
);
3537

3638
$this->composedProcessor = IfProcessor::class;

src/Model/Validator/FilterValidator.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Generic\IncompatibleFilterException;
8+
use PHPModelGenerator\Exception\Generic\InvalidFilterValueException;
79
use PHPModelGenerator\Exception\SchemaException;
810
use PHPModelGenerator\Model\GeneratorConfiguration;
911
use PHPModelGenerator\Model\Property\PropertyInterface;
10-
use PHPModelGenerator\Model\Schema;
1112
use PHPModelGenerator\PropertyProcessor\Filter\FilterInterface;
1213
use PHPModelGenerator\PropertyProcessor\Filter\TransformingFilterInterface;
1314
use PHPModelGenerator\Utils\RenderHelper;
@@ -24,13 +25,14 @@ class FilterValidator extends PropertyTemplateValidator
2425
/**
2526
* FilterValidator constructor.
2627
*
27-
* @param GeneratorConfiguration $generatorConfiguration
28-
* @param FilterInterface $filter
29-
* @param PropertyInterface $property
30-
* @param array $filterOptions
28+
* @param GeneratorConfiguration $generatorConfiguration
29+
* @param FilterInterface $filter
30+
* @param PropertyInterface $property
31+
* @param array $filterOptions
3132
* @param TransformingFilterInterface|null $transformingFilter
3233
*
3334
* @throws SchemaException
35+
* @throws ReflectionException
3436
*/
3537
public function __construct(
3638
GeneratorConfiguration $generatorConfiguration,
@@ -44,11 +46,6 @@ public function __construct(
4446
: $this->validateFilterCompatibilityWithTransformedType($filter, $transformingFilter, $property);
4547

4648
parent::__construct(
47-
sprintf(
48-
'Filter %s is not compatible with property type " . gettype($value) . " for property %s',
49-
$filter->getToken(),
50-
$property->getName()
51-
),
5249
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'Filter.phptpl',
5350
[
5451
'skipTransformedValuesCheck' => false,
@@ -63,13 +60,15 @@ public function __construct(
6360
'filterClass' => $filter->getFilter()[0],
6461
'filterMethod' => $filter->getFilter()[1],
6562
'filterOptions' => var_export($filterOptions, true),
66-
'transferExceptionMessage' => sprintf(
67-
'Invalid value for property %s denied by filter %s: {$e->getMessage()}',
68-
$property->getName(),
69-
$filter->getToken()
63+
'filterValueValidator' => new PropertyValidator(
64+
'',
65+
InvalidFilterValueException::class,
66+
[$property->getName(), $filter->getToken(), '&$filterException']
7067
),
7168
'viewHelper' => new RenderHelper($generatorConfiguration),
72-
]
69+
],
70+
IncompatibleFilterException::class,
71+
[$property->getName(), $filter->getToken()]
7372
);
7473
}
7574

src/Model/Validator/PassThroughTypeCheckValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Generic\InvalidTypeException;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89
use ReflectionType;
910

@@ -37,12 +38,8 @@ public function __construct(
3738
ReflectionTypeCheckValidator::fromReflectionType($passThroughType, $property)->getCheck(),
3839
$typeCheckValidator->getCheck()
3940
),
40-
sprintf(
41-
'Invalid type for %s. Requires [%s, %s], got " . gettype($value) . "',
42-
$property->getName(),
43-
$passThroughType->getName(),
44-
$property->getType()
45-
)
41+
InvalidTypeException::class,
42+
[$property->getName(), [$passThroughType->getName(), $property->getType()]]
4643
);
4744
}
4845

src/Model/Validator/PropertyDependencyValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Dependency\InvalidPropertyDependencyException;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89

910
/**
@@ -22,11 +23,6 @@ class PropertyDependencyValidator extends PropertyTemplateValidator
2223
public function __construct(PropertyInterface $property, array $dependencies)
2324
{
2425
parent::__construct(
25-
sprintf(
26-
'Missing required attributes which are dependants of %s:' .
27-
'\n - " . join("\n - ", $missingAttributes) . "',
28-
$property->getName()
29-
),
3026
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PropertyDependency.phptpl',
3127
[
3228
'propertyName' => $property->getName(),
@@ -35,7 +31,9 @@ public function __construct(PropertyInterface $property, array $dependencies)
3531
'',
3632
var_export(array_values($dependencies), true)
3733
),
38-
]
34+
],
35+
InvalidPropertyDependencyException::class,
36+
[$property->getName(), '&$missingAttributes']
3937
);
4038
}
4139

src/Model/Validator/PropertyNamesValidator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPMicroTemplate\Exception\FileSystemException;
88
use PHPMicroTemplate\Exception\SyntaxErrorException;
99
use PHPMicroTemplate\Exception\UndefinedSymbolException;
10+
use PHPModelGenerator\Exception\Generic\InvalidPropertyNamesException;
1011
use PHPModelGenerator\Exception\SchemaException;
1112
use PHPModelGenerator\Model\Schema;
1213
use PHPModelGenerator\Model\Validator;
@@ -48,16 +49,14 @@ public function __construct(
4849
});
4950

5051
parent::__construct(
51-
$this->getRenderer()->renderTemplate(
52-
DIRECTORY_SEPARATOR . 'Exception' . DIRECTORY_SEPARATOR . 'InvalidPropertiesException.phptpl',
53-
['error' => 'Provided JSON contains properties with invalid names.', 'property' => 'property']
54-
),
5552
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PropertyNames.phptpl',
5653
[
5754
'nameValidationProperty' => $nameValidationProperty,
5855
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
5956
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
60-
]
57+
],
58+
InvalidPropertyNamesException::class,
59+
[$schema->getClassName(), '&$invalidProperties']
6160
);
6261
}
6362

src/Model/Validator/SchemaDependencyValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Dependency\InvalidSchemaDependencyException;
8+
use PHPModelGenerator\Exception\SchemaException;
79
use PHPModelGenerator\Model\Property\Property;
810
use PHPModelGenerator\Model\Property\PropertyInterface;
911
use PHPModelGenerator\Model\Schema;
@@ -21,15 +23,15 @@ class SchemaDependencyValidator extends PropertyTemplateValidator
2123
/**
2224
* SchemaDependencyValidator constructor.
2325
*
24-
* @param SchemaProcessor $schemaProcessor
26+
* @param SchemaProcessor $schemaProcessor
2527
* @param PropertyInterface $property
26-
* @param Schema $schema
28+
* @param Schema $schema
29+
*
30+
* @throws SchemaException
2731
*/
2832
public function __construct(SchemaProcessor $schemaProcessor, PropertyInterface $property, Schema $schema)
2933
{
3034
parent::__construct(
31-
"Invalid schema which is dependant on {$property->getName()}:\\n - " .
32-
'" . preg_replace("/\n([^\s])/m", "\n - $1", preg_replace("/\n\s/m", "\n ", $dependencyException->getMessage())) . "',
3335
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'SchemaDependency.phptpl',
3436
[
3537
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
@@ -42,7 +44,9 @@ public function __construct(SchemaProcessor $schemaProcessor, PropertyInterface
4244
$schema->getClassName(),
4345
$schemaProcessor->getGeneratorConfiguration()
4446
))
45-
]
47+
],
48+
InvalidSchemaDependencyException::class,
49+
[$property->getName(), '&$dependencyException']
4650
);
4751
}
4852

0 commit comments

Comments
 (0)