|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mschindler83\ArrayAccess; |
|
5
|
|
|
|
|
6
|
|
|
use Opis\JsonSchema\ValidationError; |
|
7
|
|
|
|
|
8
|
|
|
class ArrayAccessFailed extends \RuntimeException |
|
9
|
|
|
{ |
|
10
|
3 |
|
public static function notAnArray($value): self |
|
11
|
|
|
{ |
|
12
|
3 |
|
return new self(\sprintf('Given parameter "%s" is not an array', gettype($value))); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
2 |
|
public static function invalidType(array $arrayPath, $value, string $expectedType): self |
|
16
|
|
|
{ |
|
17
|
2 |
|
return new self( |
|
18
|
2 |
|
\sprintf( |
|
19
|
2 |
|
'[Array path: %s] Could not get value for "%s". Invalid type "%s". Expected type: "%s"', |
|
20
|
2 |
|
\implode('.', $arrayPath), |
|
21
|
2 |
|
end($arrayPath), |
|
22
|
2 |
|
is_object($value) ? get_class($value) : gettype($value), |
|
23
|
2 |
|
$expectedType |
|
24
|
|
|
) |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
public static function invalidDateTimeType(array $arrayPath, $value, string $dateFormat): self |
|
29
|
|
|
{ |
|
30
|
2 |
|
return new self( |
|
31
|
2 |
|
\sprintf( |
|
32
|
2 |
|
'[Array path: %s] Could not get datetime object at "%s" with format "%s" from value "%s"', |
|
33
|
2 |
|
\implode('.', $arrayPath), |
|
34
|
2 |
|
end($arrayPath), |
|
35
|
2 |
|
$dateFormat, |
|
36
|
2 |
|
$value |
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public static function failedByCallbackRestriction(array $arrayPath): self |
|
42
|
|
|
{ |
|
43
|
1 |
|
return new self( |
|
44
|
1 |
|
\sprintf( |
|
45
|
1 |
|
'[Array path: %s] Could not get value for "%s". Reason: Callback restriction', |
|
46
|
1 |
|
\implode('.', $arrayPath), |
|
47
|
1 |
|
end($arrayPath), |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public static function pathNotFound(): self |
|
53
|
|
|
{ |
|
54
|
2 |
|
return new self('Path not found'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function jsonSchemaValidationFailed(ValidationError ...$errors): self |
|
58
|
|
|
{ |
|
59
|
|
|
$messages = \array_map( |
|
60
|
|
|
function(ValidationError $error) { |
|
61
|
|
|
return \sprintf( |
|
62
|
|
|
'Error: [%s], Data pointer: [%s]', |
|
63
|
|
|
$error->keyword(), |
|
64
|
|
|
\implode(', ', $error->dataPointer()), |
|
65
|
|
|
); |
|
66
|
|
|
}, |
|
67
|
|
|
$errors |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
return new self( |
|
71
|
|
|
\sprintf('Json schema validation failed: %s', \implode(', ', $messages)) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|