1
|
|
|
<?php namespace Limoncello\Flute\Validation\JsonApi\Execution; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2018 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Contracts\L10n\FormatterInterface; |
20
|
|
|
use Limoncello\Flute\Contracts\Validation\ErrorCodes; |
21
|
|
|
use Limoncello\Flute\Http\JsonApiResponse; |
22
|
|
|
use Limoncello\Validation\Contracts\Errors\ErrorInterface; |
23
|
|
|
use Neomerx\JsonApi\Exceptions\ErrorCollection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Limoncello\Flute |
27
|
|
|
*/ |
28
|
|
|
class JsonApiErrorCollection extends ErrorCollection |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var FormatterInterface |
32
|
|
|
*/ |
33
|
|
|
private $messageFormatter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param FormatterInterface $formatter |
37
|
|
|
*/ |
38
|
60 |
|
public function __construct(FormatterInterface $formatter) |
39
|
|
|
{ |
40
|
60 |
|
$this->messageFormatter = $formatter; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
1 |
View Code Duplication |
public function addValidationIdError( |
|
|
|
|
47
|
|
|
ErrorInterface $error, |
48
|
|
|
int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY |
49
|
|
|
): void { |
50
|
1 |
|
$title = $this->getInvalidValueMessage(); |
51
|
1 |
|
$detail = $this->getValidationMessage($error); |
52
|
1 |
|
$this->addDataIdError($title, $detail, $errorStatus); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
1 |
View Code Duplication |
public function addValidationTypeError( |
|
|
|
|
59
|
|
|
ErrorInterface $error, |
60
|
|
|
int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY |
61
|
|
|
): void { |
62
|
1 |
|
$title = $this->getInvalidValueMessage(); |
63
|
1 |
|
$detail = $this->getValidationMessage($error); |
64
|
1 |
|
$this->addDataTypeError($title, $detail, $errorStatus); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
2 |
View Code Duplication |
public function addValidationAttributeError( |
|
|
|
|
71
|
|
|
ErrorInterface $error, |
72
|
|
|
int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY |
73
|
|
|
): void { |
74
|
2 |
|
$title = $this->getInvalidValueMessage(); |
75
|
2 |
|
$detail = $this->getValidationMessage($error); |
76
|
2 |
|
$this->addDataAttributeError($error->getParameterName(), $title, $detail, $errorStatus); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
4 |
View Code Duplication |
public function addValidationRelationshipError( |
|
|
|
|
83
|
|
|
ErrorInterface $error, |
84
|
|
|
int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY |
85
|
|
|
): void { |
86
|
4 |
|
$title = $this->getInvalidValueMessage(); |
87
|
4 |
|
$detail = $this->getValidationMessage($error); |
88
|
4 |
|
$this->addRelationshipError($error->getParameterName(), $title, $detail, $errorStatus); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
6 |
View Code Duplication |
public function addValidationQueryError( |
|
|
|
|
95
|
|
|
string $paramName, |
96
|
|
|
ErrorInterface $error, |
97
|
|
|
int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY |
98
|
|
|
): void { |
99
|
6 |
|
$title = $this->getInvalidValueMessage(); |
100
|
6 |
|
$detail = $this->getValidationMessage($error); |
101
|
6 |
|
$this->addQueryParameterError($paramName, $title, $detail, $errorStatus); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
11 |
|
private function getInvalidValueMessage(): string |
109
|
|
|
{ |
110
|
11 |
|
$message = $this->getMessageFormatter()->formatMessage(ErrorCodes::INVALID_VALUE); |
111
|
|
|
|
112
|
11 |
|
return $message; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param ErrorInterface $error |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
11 |
|
private function getValidationMessage(ErrorInterface $error): string |
121
|
|
|
{ |
122
|
11 |
|
$context = $error->getMessageContext(); |
123
|
11 |
|
$args = $context === null ? [] : $context; |
124
|
11 |
|
$message = $this->getMessageFormatter()->formatMessage($error->getMessageCode(), $args); |
125
|
|
|
|
126
|
11 |
|
return $message; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return FormatterInterface |
131
|
|
|
*/ |
132
|
11 |
|
private function getMessageFormatter(): FormatterInterface |
133
|
|
|
{ |
134
|
11 |
|
return $this->messageFormatter; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.