@@ -11,6 +11,14 @@ final class InputException extends AbstractException
11
11
private const CODE_INVALID_FORMAT = 1901 ;
12
12
private const CODE_MISSING_REQUIRED = 1902 ;
13
13
private const CODE_EXCEEDS_MAX_LENGTH = 1903 ;
14
+ private const CODE_INVALID_ARGUMENT = 1904 ;
15
+ private const CODE_BELOW_MIN_LENGTH = 1905 ;
16
+ private const CODE_OUT_OF_RANGE = 1906 ;
17
+ private const CODE_INVALID_TYPE = 1907 ;
18
+ private const CODE_INVALID_OPTION = 1908 ;
19
+ private const CODE_DUPLICATE_ENTRY = 1909 ;
20
+ private const CODE_INVALID_DATE = 1910 ;
21
+ private const CODE_INVALID_CONFIG_PARAM = 1911 ;
14
22
15
23
public static function invalidFormat (string $ field ): self
16
24
{
@@ -38,4 +46,99 @@ public static function exceedsMaxLength(string $field, int $maxLength): self
38
46
"Field ' {$ field }' exceeds maximum length of {$ maxLength }"
39
47
);
40
48
}
49
+
50
+ public static function invalidArgument (string $ field , mixed $ value , ?string $ expectedType = null ): self
51
+ {
52
+ $ message = "Invalid argument for field ' {$ field }'. Received value: " . self ::formatValue ($ value );
53
+ if (null !== $ expectedType ) {
54
+ $ message .= ", expected type: {$ expectedType }" ;
55
+ }
56
+
57
+ return self ::createException (
58
+ self ::CODE_INVALID_ARGUMENT ,
59
+ 'INVALID_ARGUMENT ' ,
60
+ $ message
61
+ );
62
+ }
63
+
64
+ public static function belowMinLength (string $ field , int $ minLength ): self
65
+ {
66
+ return self ::createException (
67
+ self ::CODE_BELOW_MIN_LENGTH ,
68
+ 'BELOW_MIN_LENGTH ' ,
69
+ "Field ' {$ field }' is below minimum length of {$ minLength }"
70
+ );
71
+ }
72
+
73
+ public static function outOfRange (string $ field , $ min , $ max ): self
74
+ {
75
+ return self ::createException (
76
+ self ::CODE_OUT_OF_RANGE ,
77
+ 'OUT_OF_RANGE ' ,
78
+ "Field ' {$ field }' is out of range. Must be between {$ min } and {$ max }"
79
+ );
80
+ }
81
+
82
+ public static function invalidType (string $ field , string $ expectedType ): self
83
+ {
84
+ return self ::createException (
85
+ self ::CODE_INVALID_TYPE ,
86
+ 'INVALID_TYPE ' ,
87
+ "Field ' {$ field }' is of invalid type. Expected {$ expectedType }"
88
+ );
89
+ }
90
+
91
+ public static function invalidOption (string $ field , array $ validOptions ): self
92
+ {
93
+ $ optionsString = implode (', ' , $ validOptions );
94
+
95
+ return self ::createException (
96
+ self ::CODE_INVALID_OPTION ,
97
+ 'INVALID_OPTION ' ,
98
+ "Invalid option for field ' {$ field }'. Valid options are: {$ optionsString }"
99
+ );
100
+ }
101
+
102
+ public static function duplicateEntry (string $ field , $ value ): self
103
+ {
104
+ return self ::createException (
105
+ self ::CODE_DUPLICATE_ENTRY ,
106
+ 'DUPLICATE_ENTRY ' ,
107
+ "Duplicate entry for field ' {$ field }' with value ' {$ value }' "
108
+ );
109
+ }
110
+
111
+ public static function invalidDate (string $ field , string $ format ): self
112
+ {
113
+ return self ::createException (
114
+ self ::CODE_INVALID_DATE ,
115
+ 'INVALID_DATE ' ,
116
+ "Invalid date for field ' {$ field }'. Expected format: {$ format }"
117
+ );
118
+ }
119
+
120
+ public static function invalidConfigParam (string $ param , mixed $ value , string $ expectedDescription ): self
121
+ {
122
+ $ message = "Invalid configuration parameter ' {$ param }'. " .
123
+ 'Received value: ' . self ::formatValue ($ value ) .
124
+ ". Expected: {$ expectedDescription }" ;
125
+
126
+ return self ::createException (
127
+ self ::CODE_INVALID_CONFIG_PARAM ,
128
+ 'INVALID_CONFIG_PARAM ' ,
129
+ $ message
130
+ );
131
+ }
132
+
133
+ private static function formatValue (mixed $ value ): string
134
+ {
135
+ return match (true ) {
136
+ is_string ($ value ) => "' {$ value }' " ,
137
+ is_bool ($ value ) => $ value ? 'true ' : 'false ' ,
138
+ is_null ($ value ) => 'null ' ,
139
+ is_array ($ value ) => 'array ' ,
140
+ is_object ($ value ) => get_class ($ value ),
141
+ default => (string ) $ value ,
142
+ };
143
+ }
41
144
}
0 commit comments