Skip to content

Commit efc32a5

Browse files
authored
Merge pull request #8 from wol-soft/ReworkExceptions
Rework exceptions
2 parents a4c89a5 + a6475d2 commit efc32a5

File tree

77 files changed

+782
-427
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+782
-427
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ Method | Configuration | Default
9090
``` setSerialization(bool $serialization) ``` <br><br>Example:<br> ``` setSerialization(true) ``` | If set to true the serialization methods `toArray` and `toJSON` will be added to the public interface of the generated classes. | false
9191
``` setOutputEnabled(bool $outputEnabled) ``` <br><br>Example:<br> ``` setOutputEnabled(false) ``` | Enable or disable output of the generation process to STDOUT | true
9292
``` setErrorRegistryClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setErrorRegistryClass(CustomException::class) ``` | Define a custom exception implementing the ErrorRegistryExceptionInterface to be used. The exception will be thrown if a validation fails and error collection is **enabled** | ErrorRegistryException::class
93-
``` setExceptionClass(string $exceptionClass) ``` <br><br>Example:<br> ``` setExceptionClass(CustomException::class) ``` | Define a custom exception to be used. The exception will be thrown if a validation fails and error collection is **disabled** | ValidationException::class
9493
``` addFilter(FilterInterface $filter) ``` <br><br>Example:<br> ``` addFilter(new CustomFilter()) ``` | Add a custom filter to the generator. Check out the docs for more details. | -
9594

9695
## Examples ##

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"symplify/easy-coding-standard": "^7.2.3",
15-
"wol-soft/php-json-schema-model-generator-production": "^0.11.0",
15+
"wol-soft/php-json-schema-model-generator-production": "^0.12.0",
1616
"wol-soft/php-micro-template": "^1.3.1",
1717

1818
"php": ">=7.2",

docs/source/combinedSchemas/allOf.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ Generated interface:
3434
public function getExample(): float;
3535
3636
37-
Possible exception (if a string is provided):
37+
Possible exception (eg. if a string is provided):
3838

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
@@ -55,6 +55,19 @@ Possible exception (if eg. 5 is provided, which matches only one subschema):
5555
- Composition element #2: Failed
5656
* Value for example must be a multiple of 3
5757
58+
The thrown exception will be a *PHPModelGenerator\\Exception\\ComposedValue\\AllOfException* which provides the following methods to get further error details:
59+
60+
.. code-block:: php
61+
62+
// returns a two-dimensional array which contains all validation exceptions grouped by composition elements
63+
public function getCompositionErrorCollection(): array
64+
// get the amount of succeeded composition elements
65+
public function getSucceededCompositionElements(): int
66+
// get the name of the property which failed
67+
public function getPropertyName(): string
68+
// get the value provided to the property
69+
public function getProvidedValue()
70+
5871
.. hint::
5972

6073
When combining multiple nested objects with an `allOf` composition a `merged property <mergedProperty.html>`__ will be generated

docs/source/combinedSchemas/anyOf.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ Possible exception (if a string is provided):
4545
- Composition element #2: Failed
4646
* Invalid type for example. Requires float, got string
4747
48+
The thrown exception will be a *PHPModelGenerator\\Exception\\ComposedValue\\AnyOfException* which provides the following methods to get further error details:
49+
50+
.. code-block:: php
51+
52+
// returns a two-dimensional array which contains all validation exceptions grouped by composition elements
53+
public function getCompositionErrorCollection(): array
54+
// get the amount of succeeded composition elements
55+
public function getSucceededCompositionElements(): int
56+
// get the name of the property which failed
57+
public function getPropertyName(): string
58+
// get the value provided to the property
59+
public function getProvidedValue()
60+
4861
.. hint::
4962

5063
When combining multiple nested objects with an `anyOf` composition a `merged property <mergedProperty.html>`__ will be generated

docs/source/combinedSchemas/not.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@ Possible exceptions:
3232
Invalid value for property declined by composition constraint.
3333
Requires to match none composition element but matched 1 elements.
3434
- Composition element #1: Valid
35+
36+
The thrown exception will be a *PHPModelGenerator\\Exception\\ComposedValue\\NotException* which provides the following methods to get further error details:
37+
38+
.. code-block:: php
39+
40+
// returns a two-dimensional array which contains all validation exceptions grouped by composition elements
41+
public function getCompositionErrorCollection(): array
42+
// get the amount of succeeded composition elements
43+
public function getSucceededCompositionElements(): int
44+
// get the name of the property which failed
45+
public function getPropertyName(): string
46+
// get the value provided to the property
47+
public function getProvidedValue()

docs/source/combinedSchemas/oneOf.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ Possible exception (if eg. 15 is provided, which matches both subschemas):
5555
- Composition element #1: Valid
5656
- Composition element #2: Valid
5757
58+
The thrown exception will be a *PHPModelGenerator\\Exception\\ComposedValue\\OneOfException* which provides the following methods to get further error details:
59+
60+
.. code-block:: php
61+
62+
// returns a two-dimensional array which contains all validation exceptions grouped by composition elements
63+
public function getCompositionErrorCollection(): array
64+
// get the amount of succeeded composition elements
65+
public function getSucceededCompositionElements(): int
66+
// get the name of the property which failed
67+
public function getPropertyName(): string
68+
// get the value provided to the property
69+
public function getProvidedValue()
70+
5871
.. hint::
5972

6073
When combining multiple nested objects with an `oneOf` composition a `merged property <mergedProperty.html>`__ will be generated

docs/source/complexTypes/array.rst

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ Possible exceptions:
3131

3232
* Invalid type for example. Requires array, got __TYPE__
3333

34+
The thrown exception will be a *PHPModelGenerator\\Exception\\Generic\\InvalidTypeException* which provides the following methods to get further error details:
35+
36+
.. code-block:: php
37+
38+
// returns a string if the property expects exactly one type, an array if the property accepts multiple types
39+
public function getExpectedType()
40+
// get the name of the property which failed
41+
public function getPropertyName(): string
42+
// get the value provided to the property
43+
public function getProvidedValue()
44+
3445
Items
3546
^^^^^
3647

@@ -56,10 +67,21 @@ With a schema like this all items must contain a string with at least two charac
5667

5768
.. code-block:: none
5869
59-
Invalid item in array example:
70+
Invalid items in array example:
6071
- invalid item #3
6172
* Invalid type for item of array example. Requires string, got double
6273
74+
The thrown exception will be a *PHPModelGenerator\\Exception\\Arrays\\InvalidItemException* which provides the following methods to get further error details:
75+
76+
.. code-block:: php
77+
78+
// returns a two-dimensional array which contains all validation exceptions grouped by item index
79+
public function getInvalidItems(): array
80+
// get the name of the property which failed
81+
public function getPropertyName(): string
82+
// get the value provided to the property
83+
public function getProvidedValue()
84+
6385
A more complex array may contain a nested object.
6486

6587
.. code-block:: json
@@ -160,6 +182,17 @@ If invalid tuples are provided a detailed exception will be thrown containing al
160182
- invalid tuple #1
161183
* Invalid type for name. Requires string, got boolean
162184
185+
The thrown exception will be a *PHPModelGenerator\\Exception\\Arrays\\InvalidTupleException* which provides the following methods to get further error details:
186+
187+
.. code-block:: php
188+
189+
// returns a two-dimensional array which contains all validation exceptions grouped by item index
190+
public function getInvalidTuples()
191+
// get the name of the property which failed
192+
public function getPropertyName(): string
193+
// get the value provided to the property
194+
public function getProvidedValue()
195+
163196
.. hint::
164197

165198
Incomplete tuples are valid. Consequently an empty array provided for the schema shown above would pass the validation. Keep this in mind when designing tuple constraints. To force the given data to provide all tuples use tuple items combined with the `minItems` array size validation.
@@ -202,6 +235,19 @@ Possible exceptions:
202235

203236
* Tuple array example contains not allowed additional items. Expected 2 items, got 3
204237

238+
The thrown exception will be a *PHPModelGenerator\\Exception\\Arrays\\AdditionalTupleItemsException* which provides the following methods to get further error details:
239+
240+
.. code-block:: php
241+
242+
// Get the expected tuple amount
243+
public function getExpectedAmount(): int
244+
// Get the amount of items provided
245+
public function getAmount(): int
246+
// get the name of the property which failed
247+
public function getPropertyName(): string
248+
// get the value provided to the property
249+
public function getProvidedValue()
250+
205251
If invalid additional items are provided a detailed exception will be thrown containing all violations:
206252

207253
.. code-block:: none
@@ -212,6 +258,17 @@ If invalid additional items are provided a detailed exception will be thrown con
212258
- invalid additional item '5'
213259
* Invalid type for additional item. Requires object, got int
214260
261+
The thrown exception will be a *PHPModelGenerator\\Exception\\Arrays\\InvalidAdditionalTupleItemsException* which provides the following methods to get further error details:
262+
263+
.. code-block:: php
264+
265+
// returns a two-dimensional array which contains all validation exceptions grouped by item index
266+
public function getNestedExceptions(): array
267+
// get the name of the property which failed
268+
public function getPropertyName(): string
269+
// get the value provided to the property
270+
public function getProvidedValue()
271+
215272
Contains
216273
--------
217274

@@ -236,6 +293,15 @@ Possible exceptions:
236293

237294
* No item in array example matches contains constraint
238295

296+
The thrown exception will be a *PHPModelGenerator\\Exception\\Arrays\\ContainsException* which provides the following methods to get further error details:
297+
298+
.. code-block:: php
299+
300+
// get the name of the property which failed
301+
public function getPropertyName(): string
302+
// get the value provided to the property
303+
public function getProvidedValue()
304+
239305
Size validation
240306
---------------
241307

@@ -260,6 +326,19 @@ Possible exceptions:
260326
* Array example must not contain less than 2 items
261327
* Array example must not contain more than 5 items
262328

329+
The thrown exception will be a *PHPModelGenerator\\Exception\\Arrays\\MaxItemsException* or a *PHPModelGenerator\\Exception\\Arrays\\MinItemsException* which provides the following methods to get further error details:
330+
331+
.. code-block:: php
332+
333+
// for a MaxItemsException: get the maximum amount of allowed items
334+
public function getMaxItems(): int
335+
// for a MinItemsException: get the minimum amount of required items
336+
public function getMinItems(): int
337+
// get the name of the property which failed
338+
public function getPropertyName(): string
339+
// get the value provided to the property
340+
public function getProvidedValue()
341+
263342
Uniqueness
264343
----------
265344

@@ -281,3 +360,12 @@ The items of an array can be forced to be unique with the `uniqueItems` keyword.
281360
Possible exceptions:
282361

283362
* Items of array example are not unique
363+
364+
The thrown exception will be an *PHPModelGenerator\\Exception\\Arrays\\UniqueItemsException* which provides the following methods to get further error details:
365+
366+
.. code-block:: php
367+
368+
// get the name of the property which failed
369+
public function getPropertyName(): string
370+
// get the value provided to the property
371+
public function getProvidedValue()

docs/source/complexTypes/enum.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ Generated interface (no typehints are generated as it's an untyped enum):
5555
Possible exceptions:
5656

5757
* Invalid value for example declined by enum constraint
58+
59+
The thrown exception will be an *PHPModelGenerator\\Exception\\Generic\\EnumException* which provides the following methods to get further error details:
60+
61+
.. code-block:: php
62+
63+
// get all values which are allowed by the enum
64+
public function getAllowedValues(): array
65+
// get the name of the property which failed
66+
public function getPropertyName(): string
67+
// get the value provided to the property
68+
public function getProvidedValue()

docs/source/complexTypes/multiType.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ Possible exceptions:
2626

2727
* Invalid type for property. Requires [float, string], got __TYPE__
2828

29+
The thrown exception will be a *PHPModelGenerator\\Exception\\Generic\\InvalidTypeException* which provides the following methods to get further error details:
30+
31+
.. code-block:: php
32+
33+
// returns a string if the property expects exactly one type, an array if the property accepts multiple types
34+
public function getExpectedType()
35+
// get the name of the property which failed
36+
public function getPropertyName(): string
37+
// get the value provided to the property
38+
public function getProvidedValue()
39+
2940
Additional validators
3041
---------------------
3142

@@ -54,6 +65,6 @@ For example if an array **["Hello", 123, "Goodbye"]** is given the validation wi
5465

5566
.. code-block:: none
5667
57-
Invalid item in array example:
68+
Invalid items in array example:
5869
- invalid item #1
5970
* Invalid type for item of array example. Requires string, got integer

0 commit comments

Comments
 (0)