Passed
Push — main ( 131673...f122aa )
by Anatoly
04:13 queued 13s
created

InvalidValueException::getViolation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2021, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/hydrator
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Hydrator\Exception;
15
16
use Sunrise\Hydrator\Dictionary\ErrorCode;
17
use Sunrise\Hydrator\Dictionary\ErrorMessage;
18
use RuntimeException;
19
use Symfony\Component\Validator\ConstraintViolation;
20
use Symfony\Component\Validator\ConstraintViolationInterface;
21
22
use function join;
23
use function strtr;
24
25
/**
26
 * InvalidValueException
27
 */
28
class InvalidValueException extends RuntimeException implements ExceptionInterface
29
{
30
31
    /**
32
     * @var string
33
     */
34
    private string $errorCode;
35
36
    /**
37
     * @var list<array-key>
0 ignored issues
show
Bug introduced by
The type Sunrise\Hydrator\Exception\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
     */
39
    private array $propertyPath;
40
41
    /**
42
     * @var string
43
     */
44
    private string $messageTemplate;
45
46
    /**
47
     * @var array<string, int|float|string>
48
     */
49
    private array $messagePlaceholders;
50
51
    /**
52
     * Constructor of the class
53
     *
54
     * @param string $message
55
     * @param string $errorCode
56
     * @param list<array-key> $propertyPath
57
     * @param string $messageTemplate
58
     * @param array<string, int|float|string> $messagePlaceholders
59
     */
60 246
    public function __construct(
61
        string $message,
62
        string $errorCode,
63
        array $propertyPath,
64
        string $messageTemplate,
65
        array $messagePlaceholders
66
    ) {
67 246
        parent::__construct($message);
68
69 246
        $this->errorCode = $errorCode;
70 246
        $this->propertyPath = $propertyPath;
0 ignored issues
show
Documentation Bug introduced by
It seems like $propertyPath of type array is incompatible with the declared type Sunrise\Hydrator\Exception\list of property $propertyPath.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71 246
        $this->messageTemplate = $messageTemplate;
72 246
        $this->messagePlaceholders = $messagePlaceholders;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 246
    final public function getPropertyPath(): string
79
    {
80 246
        return join('.', $this->propertyPath);
81
    }
82
83
    /**
84
     * @return string
85
     *
86
     * @since 3.0.0
87
     */
88 245
    final public function getErrorCode(): string
89
    {
90 245
        return $this->errorCode;
91
    }
92
93
    /**
94
     * @return string
95
     *
96
     * @since 3.7.0
97
     */
98 1
    final public function getMessageTemplate(): string
99
    {
100 1
        return $this->messageTemplate;
101
    }
102
103
    /**
104
     * @return array<string, int|float|string>
105
     *
106
     * @since 3.7.0
107
     */
108 1
    final public function getMessagePlaceholders(): array
109
    {
110 1
        return $this->messagePlaceholders;
111
    }
112
113
    /**
114
     * @since 3.9.0
115
     */
116 1
    final public function getViolation(): ConstraintViolationInterface
117
    {
118 1
        return new ConstraintViolation(
119 1
            $this->getMessage(),
120 1
            $this->getMessageTemplate(),
121 1
            $this->getMessagePlaceholders(),
122 1
            null,
123 1
            $this->getPropertyPath(),
124 1
            null,
125 1
            null,
126 1
            $this->getErrorCode(),
127 1
        );
128
    }
129
130
    /**
131
     * @param list<array-key> $propertyPath
132
     *
133
     * @return self
134
     *
135
     * @since 3.0.0
136
     */
137 20
    final public static function mustBeProvided(array $propertyPath): self
138
    {
139 20
        return new self(
140 20
            ErrorMessage::MUST_BE_PROVIDED,
141 20
            ErrorCode::MUST_BE_PROVIDED,
142 20
            $propertyPath,
143 20
            ErrorMessage::MUST_BE_PROVIDED,
144 20
            [],
145 20
        );
146
    }
147
148
    /**
149
     * @param list<array-key> $propertyPath
150
     *
151
     * @return self
152
     *
153
     * @since 3.0.0
154
     */
155 51
    final public static function mustNotBeEmpty(array $propertyPath): self
156
    {
157 51
        return new self(
158 51
            ErrorMessage::MUST_NOT_BE_EMPTY,
159 51
            ErrorCode::MUST_NOT_BE_EMPTY,
160 51
            $propertyPath,
161 51
            ErrorMessage::MUST_NOT_BE_EMPTY,
162 51
            [],
163 51
        );
164
    }
165
166
    /**
167
     * @param list<array-key> $propertyPath
168
     *
169
     * @return self
170
     *
171
     * @since 3.0.0
172
     */
173 42
    final public static function mustBeBoolean(array $propertyPath): self
174
    {
175 42
        return new self(
176 42
            ErrorMessage::MUST_BE_BOOLEAN,
177 42
            ErrorCode::MUST_BE_BOOLEAN,
178 42
            $propertyPath,
179 42
            ErrorMessage::MUST_BE_BOOLEAN,
180 42
            [],
181 42
        );
182
    }
183
184
    /**
185
     * @param list<array-key> $propertyPath
186
     *
187
     * @return self
188
     *
189
     * @since 3.0.0
190
     */
191 11
    final public static function mustBeInteger(array $propertyPath): self
192
    {
193 11
        return new self(
194 11
            ErrorMessage::MUST_BE_INTEGER,
195 11
            ErrorCode::MUST_BE_INTEGER,
196 11
            $propertyPath,
197 11
            ErrorMessage::MUST_BE_INTEGER,
198 11
            [],
199 11
        );
200
    }
201
202
    /**
203
     * @param list<array-key> $propertyPath
204
     *
205
     * @return self
206
     *
207
     * @since 3.0.0
208
     */
209 4
    final public static function mustBeNumber(array $propertyPath): self
210
    {
211 4
        return new self(
212 4
            ErrorMessage::MUST_BE_NUMBER,
213 4
            ErrorCode::MUST_BE_NUMBER,
214 4
            $propertyPath,
215 4
            ErrorMessage::MUST_BE_NUMBER,
216 4
            [],
217 4
        );
218
    }
219
220
    /**
221
     * @param list<array-key> $propertyPath
222
     *
223
     * @return self
224
     *
225
     * @since 3.0.0
226
     */
227 25
    final public static function mustBeString(array $propertyPath): self
228
    {
229 25
        return new self(
230 25
            ErrorMessage::MUST_BE_STRING,
231 25
            ErrorCode::MUST_BE_STRING,
232 25
            $propertyPath,
233 25
            ErrorMessage::MUST_BE_STRING,
234 25
            [],
235 25
        );
236
    }
237
238
    /**
239
     * @param list<array-key> $propertyPath
240
     *
241
     * @return self
242
     *
243
     * @since 3.0.0
244
     */
245 35
    final public static function mustBeArray(array $propertyPath): self
246
    {
247 35
        return new self(
248 35
            ErrorMessage::MUST_BE_ARRAY,
249 35
            ErrorCode::MUST_BE_ARRAY,
250 35
            $propertyPath,
251 35
            ErrorMessage::MUST_BE_ARRAY,
252 35
            [],
253 35
        );
254
    }
255
256
    /**
257
     * @param list<array-key> $propertyPath
258
     * @param int<0, max> $maximumElements
259
     *
260
     * @return self
261
     *
262
     * @since 3.0.0
263
     */
264 51
    final public static function arrayOverflow(array $propertyPath, int $maximumElements): self
265
    {
266 51
        $placeholders = [
267 51
            '{{ maximum_elements }}' => $maximumElements,
268 51
        ];
269
270 51
        return new self(
271 51
            strtr(ErrorMessage::ARRAY_OVERFLOW, $placeholders),
272 51
            ErrorCode::ARRAY_OVERFLOW,
273 51
            $propertyPath,
274 51
            ErrorMessage::ARRAY_OVERFLOW,
275 51
            $placeholders,
276 51
        );
277
    }
278
279
    /**
280
     * @param list<array-key> $propertyPath
281
     * @param list<int|string> $expectedValues
282
     *
283
     * @return self
284
     *
285
     * @since 3.0.0
286
     */
287 3
    final public static function invalidChoice(array $propertyPath, array $expectedValues): self
288
    {
289 3
        $placeholders = [
290 3
            '{{ expected_values }}' => join(', ', $expectedValues),
291 3
        ];
292
293 3
        return new self(
294 3
            strtr(ErrorMessage::INVALID_CHOICE, $placeholders),
295 3
            ErrorCode::INVALID_CHOICE,
296 3
            $propertyPath,
297 3
            ErrorMessage::INVALID_CHOICE,
298 3
            $placeholders,
299 3
        );
300
    }
301
302
    /**
303
     * @param list<array-key> $propertyPath
304
     * @param string $expectedFormat
305
     *
306
     * @return self
307
     *
308
     * @since 3.0.0
309
     */
310 1
    final public static function invalidTimestamp(array $propertyPath, string $expectedFormat): self
311
    {
312 1
        $placeholders = [
313 1
            '{{ expected_format }}' => $expectedFormat,
314 1
        ];
315
316 1
        return new self(
317 1
            strtr(ErrorMessage::INVALID_TIMESTAMP, $placeholders),
318 1
            ErrorCode::INVALID_TIMESTAMP,
319 1
            $propertyPath,
320 1
            ErrorMessage::INVALID_TIMESTAMP,
321 1
            $placeholders,
322 1
        );
323
    }
324
325
    /**
326
     * @param list<array-key> $propertyPath
327
     *
328
     * @return self
329
     *
330
     * @since 3.0.0
331
     */
332 1
    final public static function invalidTimezone(array $propertyPath): self
333
    {
334 1
        return new self(
335 1
            ErrorMessage::INVALID_TIMEZONE,
336 1
            ErrorCode::INVALID_TIMEZONE,
337 1
            $propertyPath,
338 1
            ErrorMessage::INVALID_TIMEZONE,
339 1
            [],
340 1
        );
341
    }
342
343
    /**
344
     * @param list<array-key> $propertyPath
345
     *
346
     * @return self
347
     *
348
     * @since 3.0.0
349
     */
350 2
    final public static function invalidUid(array $propertyPath): self
351
    {
352 2
        return new self(
353 2
            ErrorMessage::INVALID_UID,
354 2
            ErrorCode::INVALID_UID,
355 2
            $propertyPath,
356 2
            ErrorMessage::INVALID_UID,
357 2
            [],
358 2
        );
359
    }
360
}
361