Passed
Push — main ( 066d74...a98deb )
by Anatoly
04:16 queued 15s
created

InvalidValueException   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 105
c 3
b 0
f 1
dl 0
loc 335
ccs 143
cts 143
cp 1
rs 10
wmc 19

19 Methods

Rating   Name   Duplication   Size   Complexity  
A mustBeProvided() 0 8 1
A getMessageTemplate() 0 3 1
A getInvalidValue() 0 3 1
A getErrorCode() 0 3 1
A mustBeNumber() 0 9 1
A mustNotBeEmpty() 0 9 1
A getViolation() 0 11 1
A invalidChoice() 0 13 1
A arrayOverflow() 0 13 1
A mustBeString() 0 9 1
A getMessagePlaceholders() 0 3 1
A invalidTimezone() 0 9 1
A invalidTimestamp() 0 16 1
A getPropertyPath() 0 3 1
A invalidUid() 0 9 1
A __construct() 0 15 1
A mustBeInteger() 0 9 1
A mustBeArray() 0 9 1
A mustBeBoolean() 0 9 1
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
class InvalidValueException extends RuntimeException implements ExceptionInterface
26
{
27
    /**
28
     * @see ErrorCode
29
     */
30
    private string $errorCode;
31
32
    /**
33
     * @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...
34
     */
35
    private array $propertyPath;
36
37
    /**
38
     * @see ErrorMessage
39
     */
40
    private string $messageTemplate;
41
42
    /**
43
     * @var array<string, int|float|string>
44
     */
45
    private array $messagePlaceholders;
46
47
    /**
48
     * @var mixed
49
     */
50
    private $invalidValue;
51
52
    /**
53
     * @param list<array-key> $propertyPath
54
     * @param array<string, int|float|string> $messagePlaceholders
55
     * @param mixed $invalidValue
56
     */
57 247
    public function __construct(
58
        string $message,
59
        string $errorCode,
60
        array $propertyPath,
61
        string $messageTemplate,
62
        array $messagePlaceholders,
63
        $invalidValue = null
64
    ) {
65 247
        parent::__construct($message);
66
67 247
        $this->errorCode = $errorCode;
68 247
        $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...
69 247
        $this->messageTemplate = $messageTemplate;
70 247
        $this->messagePlaceholders = $messagePlaceholders;
71 247
        $this->invalidValue = $invalidValue;
72
    }
73
74 247
    final public function getPropertyPath(): string
75
    {
76 247
        return join('.', $this->propertyPath);
77
    }
78
79
    /**
80
     * @since 3.0.0
81
     */
82 246
    final public function getErrorCode(): string
83
    {
84 246
        return $this->errorCode;
85
    }
86
87
    /**
88
     * @since 3.7.0
89
     */
90 1
    final public function getMessageTemplate(): string
91
    {
92 1
        return $this->messageTemplate;
93
    }
94
95
    /**
96
     * @return array<string, int|float|string>
97
     *
98
     * @since 3.7.0
99
     */
100 1
    final public function getMessagePlaceholders(): array
101
    {
102 1
        return $this->messagePlaceholders;
103
    }
104
105
    /**
106
     * @return mixed
107
     *
108
     * @since 3.13.0
109
     */
110 1
    final public function getInvalidValue()
111
    {
112 1
        return $this->invalidValue;
113
    }
114
115
    /**
116
     * @since 3.9.0
117
     */
118 1
    final public function getViolation(): ConstraintViolationInterface
119
    {
120 1
        return new ConstraintViolation(
121 1
            $this->getMessage(),
122 1
            $this->getMessageTemplate(),
123 1
            $this->getMessagePlaceholders(),
124 1
            null,
125 1
            $this->getPropertyPath(),
126 1
            $this->getInvalidValue(),
127 1
            null,
128 1
            $this->getErrorCode(),
129 1
        );
130
    }
131
132
    /**
133
     * @param list<array-key> $propertyPath
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
     * @param mixed $invalidValue
151
     *
152
     * @since 3.0.0
153
     */
154 51
    final public static function mustNotBeEmpty(array $propertyPath, $invalidValue = null): self
155
    {
156 51
        return new self(
157 51
            ErrorMessage::MUST_NOT_BE_EMPTY,
158 51
            ErrorCode::MUST_NOT_BE_EMPTY,
159 51
            $propertyPath,
160 51
            ErrorMessage::MUST_NOT_BE_EMPTY,
161 51
            [],
162 51
            $invalidValue,
163 51
        );
164
    }
165
166
    /**
167
     * @param list<array-key> $propertyPath
168
     * @param mixed $invalidValue
169
     *
170
     * @since 3.0.0
171
     */
172 43
    final public static function mustBeBoolean(array $propertyPath, $invalidValue = null): self
173
    {
174 43
        return new self(
175 43
            ErrorMessage::MUST_BE_BOOLEAN,
176 43
            ErrorCode::MUST_BE_BOOLEAN,
177 43
            $propertyPath,
178 43
            ErrorMessage::MUST_BE_BOOLEAN,
179 43
            [],
180 43
            $invalidValue,
181 43
        );
182
    }
183
184
    /**
185
     * @param list<array-key> $propertyPath
186
     * @param mixed $invalidValue
187
     *
188
     * @since 3.0.0
189
     */
190 11
    final public static function mustBeInteger(array $propertyPath, $invalidValue = null): self
191
    {
192 11
        return new self(
193 11
            ErrorMessage::MUST_BE_INTEGER,
194 11
            ErrorCode::MUST_BE_INTEGER,
195 11
            $propertyPath,
196 11
            ErrorMessage::MUST_BE_INTEGER,
197 11
            [],
198 11
            $invalidValue,
199 11
        );
200
    }
201
202
    /**
203
     * @param list<array-key> $propertyPath
204
     * @param mixed $invalidValue
205
     *
206
     * @since 3.0.0
207
     */
208 4
    final public static function mustBeNumber(array $propertyPath, $invalidValue = null): self
209
    {
210 4
        return new self(
211 4
            ErrorMessage::MUST_BE_NUMBER,
212 4
            ErrorCode::MUST_BE_NUMBER,
213 4
            $propertyPath,
214 4
            ErrorMessage::MUST_BE_NUMBER,
215 4
            [],
216 4
            $invalidValue,
217 4
        );
218
    }
219
220
    /**
221
     * @param list<array-key> $propertyPath
222
     * @param mixed $invalidValue
223
     *
224
     * @since 3.0.0
225
     */
226 25
    final public static function mustBeString(array $propertyPath, $invalidValue = null): self
227
    {
228 25
        return new self(
229 25
            ErrorMessage::MUST_BE_STRING,
230 25
            ErrorCode::MUST_BE_STRING,
231 25
            $propertyPath,
232 25
            ErrorMessage::MUST_BE_STRING,
233 25
            [],
234 25
            $invalidValue,
235 25
        );
236
    }
237
238
    /**
239
     * @param list<array-key> $propertyPath
240
     * @param mixed $invalidValue
241
     *
242
     * @since 3.0.0
243
     */
244 35
    final public static function mustBeArray(array $propertyPath, $invalidValue = null): self
245
    {
246 35
        return new self(
247 35
            ErrorMessage::MUST_BE_ARRAY,
248 35
            ErrorCode::MUST_BE_ARRAY,
249 35
            $propertyPath,
250 35
            ErrorMessage::MUST_BE_ARRAY,
251 35
            [],
252 35
            $invalidValue,
253 35
        );
254
    }
255
256
    /**
257
     * @param list<array-key> $propertyPath
258
     * @param int<0, max> $maximumElements
259
     * @param mixed $invalidValue
260
     *
261
     * @since 3.0.0
262
     */
263 51
    final public static function arrayOverflow(array $propertyPath, int $maximumElements, $invalidValue = null): self
264
    {
265 51
        $placeholders = [
266 51
            '{{ maximum_elements }}' => $maximumElements,
267 51
        ];
268
269 51
        return new self(
270 51
            strtr(ErrorMessage::ARRAY_OVERFLOW, $placeholders),
271 51
            ErrorCode::ARRAY_OVERFLOW,
272 51
            $propertyPath,
273 51
            ErrorMessage::ARRAY_OVERFLOW,
274 51
            $placeholders,
275 51
            $invalidValue,
276 51
        );
277
    }
278
279
    /**
280
     * @param list<array-key> $propertyPath
281
     * @param list<int|string> $expectedValues
282
     * @param mixed $invalidValue
283
     *
284
     * @since 3.0.0
285
     */
286 3
    final public static function invalidChoice(array $propertyPath, array $expectedValues, $invalidValue = null): self
287
    {
288 3
        $placeholders = [
289 3
            '{{ expected_values }}' => join(', ', $expectedValues),
290 3
        ];
291
292 3
        return new self(
293 3
            strtr(ErrorMessage::INVALID_CHOICE, $placeholders),
294 3
            ErrorCode::INVALID_CHOICE,
295 3
            $propertyPath,
296 3
            ErrorMessage::INVALID_CHOICE,
297 3
            $placeholders,
298 3
            $invalidValue,
299 3
        );
300
    }
301
302
    /**
303
     * @param list<array-key> $propertyPath
304
     * @param mixed $invalidValue
305
     *
306
     * @since 3.0.0
307
     */
308 1
    final public static function invalidTimestamp(
309
        array $propertyPath,
310
        string $expectedFormat,
311
        $invalidValue = null
312
    ): self {
313 1
        $placeholders = [
314 1
            '{{ expected_format }}' => $expectedFormat,
315 1
        ];
316
317 1
        return new self(
318 1
            strtr(ErrorMessage::INVALID_TIMESTAMP, $placeholders),
319 1
            ErrorCode::INVALID_TIMESTAMP,
320 1
            $propertyPath,
321 1
            ErrorMessage::INVALID_TIMESTAMP,
322 1
            $placeholders,
323 1
            $invalidValue,
324 1
        );
325
    }
326
327
    /**
328
     * @param list<array-key> $propertyPath
329
     * @param mixed $invalidValue
330
     *
331
     * @since 3.0.0
332
     */
333 1
    final public static function invalidTimezone(array $propertyPath, $invalidValue = null): self
334
    {
335 1
        return new self(
336 1
            ErrorMessage::INVALID_TIMEZONE,
337 1
            ErrorCode::INVALID_TIMEZONE,
338 1
            $propertyPath,
339 1
            ErrorMessage::INVALID_TIMEZONE,
340 1
            [],
341 1
            $invalidValue,
342 1
        );
343
    }
344
345
    /**
346
     * @param list<array-key> $propertyPath
347
     * @param mixed $invalidValue
348
     *
349
     * @since 3.0.0
350
     */
351 2
    final public static function invalidUid(array $propertyPath, $invalidValue = null): self
352
    {
353 2
        return new self(
354 2
            ErrorMessage::INVALID_UID,
355 2
            ErrorCode::INVALID_UID,
356 2
            $propertyPath,
357 2
            ErrorMessage::INVALID_UID,
358 2
            [],
359 2
            $invalidValue,
360 2
        );
361
    }
362
}
363