1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace AmmitPhp\Ammit\UI\Resolver\Exception; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* UI Validation Exception Collection |
8
|
|
|
*/ |
9
|
|
|
class UIValidationCollectionException extends AbstractNormalizableCommandResolverException |
10
|
|
|
{ |
11
|
|
|
/** @var UIValidationException[] */ |
12
|
|
|
private $uiValidationExceptions = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param UIValidationException[] $uiValidationExceptions |
16
|
|
|
*/ |
17
|
|
|
public function __construct(array $uiValidationExceptions) |
18
|
|
|
{ |
19
|
1 |
|
$this->guardAgainstNoUIValidationException($uiValidationExceptions); |
20
|
|
|
|
21
|
1 |
|
parent::__construct( |
22
|
1 |
|
'Command Resolver UI Validation Exception', |
23
|
1 |
|
'collection' |
24
|
|
|
); |
25
|
|
|
|
26
|
1 |
|
foreach ($uiValidationExceptions as $uiValidationException) { |
27
|
1 |
|
$this->add($uiValidationException); |
28
|
|
|
} |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
private function add(UIValidationException $uiValidationException) |
32
|
|
|
{ |
33
|
1 |
|
$this->uiValidationExceptions[] = $uiValidationException; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
* See http://jsonapi.org/examples/#error-objects-basics |
39
|
|
|
* Example: |
40
|
|
|
* { |
41
|
|
|
* "errors": [ |
42
|
|
|
* { |
43
|
|
|
* "status": "403", |
44
|
|
|
* "source": { "pointer": "/data/attributes/secret-powers" }, |
45
|
|
|
* "detail": "Editing secret powers is not authorized on Sundays." |
46
|
|
|
* }, |
47
|
|
|
* { |
48
|
|
|
* "source": { "parameter": "include" }, |
49
|
|
|
* "title": "Invalid Query Parameter", |
50
|
|
|
* "detail": "The resource does not have an `auther` relationship path." |
51
|
|
|
* }, |
52
|
|
|
* { |
53
|
|
|
* "status": "422", |
54
|
|
|
* "source": { "pointer": "/data/attributes/volume" }, |
55
|
|
|
* "detail": "Volume does not, in fact, go to 11." |
56
|
|
|
* }, |
57
|
|
|
* { |
58
|
|
|
* "status": "500", |
59
|
|
|
* "source": { "pointer": "/data/attributes/reputation" }, |
60
|
|
|
* "title": "The backend responded with an error", |
61
|
|
|
* "detail": "Reputation service not responding after three requests." |
62
|
|
|
* } |
63
|
|
|
* ] |
64
|
|
|
* } |
65
|
|
|
*/ |
66
|
|
|
public function normalize(): array |
67
|
|
|
{ |
68
|
1 |
|
$normalizedExceptions = ['errors' => []]; |
69
|
|
|
|
70
|
1 |
|
foreach ($this->uiValidationExceptions as $exception) { |
71
|
1 |
|
$normalizedExceptions['errors'][] = $exception->normalize(); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return $normalizedExceptions; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param UIValidationException[] $uiValidationExceptions |
79
|
|
|
*/ |
80
|
|
|
private function guardAgainstNoUIValidationException(array $uiValidationExceptions) |
81
|
|
|
{ |
82
|
1 |
|
if (empty($uiValidationExceptions)) { |
83
|
1 |
|
throw new \LogicException('Can\'t create a UIValidationCollectionException without UIValidationException.'); |
84
|
|
|
} |
85
|
1 |
|
} |
86
|
|
|
} |
87
|
|
|
|