-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCastException.php
More file actions
135 lines (120 loc) · 3.63 KB
/
CastException.php
File metadata and controls
135 lines (120 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Entity\Exceptions;
use CodeIgniter\Exceptions\FrameworkException;
use CodeIgniter\Exceptions\HasExitCodeInterface;
/**
* CastException is thrown for invalid cast initialization and management.
*/
class CastException extends FrameworkException implements HasExitCodeInterface
{
public function getExitCode(): int
{
return EXIT_CONFIG;
}
/**
* Thrown when the cast class does not extends BaseCast.
*
* @return static
*/
public static function forInvalidInterface(string $class)
{
return new static(lang('Cast.baseCastMissing', [$class]));
}
/**
* Thrown when the Json format is invalid.
*
* @return static
*/
public static function forInvalidJsonFormat(int $error)
{
return match ($error) {
JSON_ERROR_DEPTH => new static(lang('Cast.jsonErrorDepth')),
JSON_ERROR_STATE_MISMATCH => new static(lang('Cast.jsonErrorStateMismatch')),
JSON_ERROR_CTRL_CHAR => new static(lang('Cast.jsonErrorCtrlChar')),
JSON_ERROR_SYNTAX => new static(lang('Cast.jsonErrorSyntax')),
JSON_ERROR_UTF8 => new static(lang('Cast.jsonErrorUtf8')),
default => new static(lang('Cast.jsonErrorUnknown')),
};
}
/**
* Thrown when the cast method is not `get` or `set`.
*
* @return static
*/
public static function forInvalidMethod(string $method)
{
return new static(lang('Cast.invalidCastMethod', [$method]));
}
/**
* Thrown when the casting timestamp is not correct timestamp.
*
* @return static
*/
public static function forInvalidTimestamp()
{
return new static(lang('Cast.invalidTimestamp'));
}
/**
* Thrown when the enum class is not specified in cast parameters.
*
* @return static
*/
public static function forMissingEnumClass()
{
return new static(lang('Cast.enumMissingClass'));
}
/**
* Thrown when the specified class is not an enum.
*
* @return static
*/
public static function forNotEnum(string $class)
{
return new static(lang('Cast.enumNotEnum', [$class]));
}
/**
* Thrown when an invalid value is provided for an enum.
*
* @return static
*/
public static function forInvalidEnumValue(string $enumClass, mixed $value)
{
return new static(lang('Cast.enumInvalidValue', [$enumClass, $value]));
}
/**
* Thrown when an invalid case name is provided for a unit enum.
*
* @return static
*/
public static function forInvalidEnumCaseName(string $enumClass, string $caseName)
{
return new static(lang('Cast.enumInvalidCaseName', [$caseName, $enumClass]));
}
/**
* Thrown when an enum instance of wrong type is provided.
*
* @return static
*/
public static function forInvalidEnumType(string $expectedClass, string $actualClass)
{
return new static(lang('Cast.enumInvalidType', [$actualClass, $expectedClass]));
}
/**
* Thrown when an invalid rounding mode is provided for float casting.
*
* @return static
*/
public static function forInvalidFloatRoundingMode(string $mode)
{
return new static(lang('Cast.floatInvalidRoundingMode', [$mode]));
}
}