Passed
Push — main ( 25077e...ed4839 )
by Anatoly
13:27 queued 07:49
created

InvalidValueException::getMessagePlaceholders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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