@@ -67,7 +67,7 @@ public function getDateTime(string $name, callable $callback):DateTimeInterface
6767 public function getArray (string $ name , callable $ callback ):array {
6868 $ value = $ this ->get ($ name , $ callback );
6969 if (!is_array ($ value )) {
70- throw new TypeError ("Value with key '$ name' is not an array " );
70+ throw new TypeError ("Data '$ name' is not an array " );
7171 }
7272
7373 return $ value ;
@@ -81,50 +81,98 @@ public function getArray(string $name, callable $callback):array {
8181 public function getTypedArray (string $ name , string $ className , callable $ callback ):array {
8282 $ array = $ this ->get ($ name , $ callback );
8383 if (!is_array ($ array )) {
84- throw new TypeError ("Value with key '$ name' is not an array " );
84+ throw new TypeError ("Data '$ name' is not an array " );
8585 }
8686
8787 foreach ($ array as $ key => $ value ) {
88- if ($ className === "int " || $ className === "integer " ) {
89- if (!is_int ($ value )) {
90- if (is_numeric ($ value )) {
91- $ array [$ key ] = (int )$ value ;
92- }
93- else {
94- throw new TypeError ("Array value at key ' $ key' is not an integer " );
95- }
96- }
97- }
98- elseif ($ className === "float " || $ className === "double " ) {
99- if (!is_float ($ value )) {
100- if (is_numeric ($ value )) {
101- $ array [$ key ] = (float )$ value ;
102- }
103- else {
104- throw new TypeError ("Array value at key ' $ key' is not a float " );
105- }
106- }
107- }
108- elseif ($ className === "string " ) {
109- if (!is_string ($ value )) {
110- $ array [$ key ] = (string )$ value ;
111- }
112- }
113- elseif ($ className === "bool " || $ className === "boolean " ) {
114- if (!is_bool ($ value )) {
115- $ array [$ key ] = (bool )$ value ;
116- }
117- }
118- else {
119- if (!$ value instanceof $ className ) {
120- throw new TypeError ("Array value at key ' $ key' is not an instance of $ className " );
121- }
122- }
88+ $ array [$ key ] = $ this ->validateAndConvertValue ($ value , $ className , $ key );
12389 }
12490
12591 return $ array ;
12692 }
12793
94+ /**
95+ * @template T
96+ * @param mixed $value
97+ * @param class-string<T> $className
98+ * @param string|int $key
99+ * @return T
100+ */
101+ private function validateAndConvertValue (mixed $ value , string $ className , string |int $ key ): mixed {
102+ return match (strtolower ($ className )) {
103+ "int " , "integer " => $ this ->validateAndConvertInt ($ value , $ key ),
104+ "float " , "double " => $ this ->validateAndConvertFloat ($ value , $ key ),
105+ "string " => $ this ->convertToString ($ value ),
106+ "bool " , "boolean " => $ this ->convertToBool ($ value ),
107+ default => $ this ->validateInstance ($ value , $ className , $ key ),
108+ };
109+ }
110+
111+ /**
112+ * @param mixed $value
113+ * @param string|int $key
114+ * @return int
115+ */
116+ private function validateAndConvertInt (mixed $ value , string |int $ key ): int {
117+ if (is_int ($ value )) {
118+ return $ value ;
119+ }
120+
121+ if (is_numeric ($ value )) {
122+ return (int )$ value ;
123+ }
124+
125+ throw new TypeError ("Array value at key ' $ key' is not an integer " );
126+ }
127+
128+ /**
129+ * @param mixed $value
130+ * @param string|int $key
131+ * @return float
132+ */
133+ private function validateAndConvertFloat (mixed $ value , string |int $ key ): float {
134+ if (is_float ($ value )) {
135+ return $ value ;
136+ }
137+
138+ if (is_numeric ($ value )) {
139+ return (float )$ value ;
140+ }
141+
142+ throw new TypeError ("Array value at key ' $ key' is not a float " );
143+ }
144+
145+ /**
146+ * @param mixed $value
147+ * @return string
148+ */
149+ private function convertToString (mixed $ value ): string {
150+ return (string )$ value ;
151+ }
152+
153+ /**
154+ * @param mixed $value
155+ * @return bool
156+ */
157+ private function convertToBool (mixed $ value ): bool {
158+ return (bool )$ value ;
159+ }
160+
161+ /**
162+ * @template T
163+ * @param mixed $value
164+ * @param class-string<T> $className
165+ * @param string|int $key
166+ * @return T
167+ */
168+ private function validateInstance (mixed $ value , string $ className , string |int $ key ): object {
169+ if ($ value instanceof $ className ) {
170+ return $ value ;
171+ }
172+
173+ throw new TypeError ("Array value at key ' $ key' is not an instance of $ className " );
174+ }
175+
128176 /**
129177 * @template T
130178 * @param class-string<T> $className
@@ -133,7 +181,7 @@ public function getTypedArray(string $name, string $className, callable $callbac
133181 public function getInstance (string $ name , string $ className , callable $ callback ):object {
134182 $ value = $ this ->get ($ name , $ callback );
135183 if (get_class ($ value ) !== $ className ) {
136- throw new TypeError ("Value is not of type $ className " );
184+ throw new TypeError ("Value is not an instance of $ className " );
137185 }
138186
139187 return $ value ;
0 commit comments