22
33namespace PhpSlides \Core \Utils \Routes ;
44
5+ use PhpSlides \Exception ;
56use PhpSlides \Core \Utils \Routes \Exception \InvalidTypesException ;
67
78/**
@@ -66,13 +67,14 @@ protected static function matchStrictType (
6667 return match ($ typeOfNeedle )
6768 {
6869 'INT ' => (int ) $ needle ,
69- 'BOOL ' => ( bool ) $ needle ,
70+ 'BOOL ' => filter_var ( $ needle, FILTER_VALIDATE_BOOLEAN ) ,
7071 'FLOAT ' => (float ) $ needle ,
7172 'ARRAY ' => json_decode ($ needle , true ),
7273 default => $ needle ,
7374 };
7475 }
7576
77+ InvalidTypesException::catchInvalidStrictTypes ($ haystack );
7678 throw InvalidTypesException::catchInvalidParameterTypes ($ types , $ typeOfNeedle );
7779 }
7880
@@ -103,10 +105,20 @@ private static function matches (string $needle, string $haystack): bool
103105 )
104106 {
105107 $ needle = json_decode ($ needle , true );
106- $ eachArrayTypes = explode (', ' , $ matches [1 ]);
108+ $ eachArrayTypes = preg_split ('/,(?![^<]*>)/ ' , $ matches [1 ]);
109+
110+ if (!is_array ($ needle ))
111+ {
112+ throw new Exception ("Invalid request parameter type. {ARRAY} requested, but got { {$ typeOfNeedle }} " );
113+ }
107114
108115 foreach ($ eachArrayTypes as $ key => $ eachArrayType )
109116 {
117+ if (!isset ($ needle [$ key ]))
118+ {
119+ throw new Exception ("Array index $ key not found in the request parameter " );
120+ }
121+
110122 $ needle2 = is_array ($ needle [$ key ])
111123 ? json_encode ($ needle [$ key ])
112124 : (string ) $ needle [$ key ];
@@ -152,20 +164,13 @@ private static function matches (string $needle, string $haystack): bool
152164 */
153165 protected static function typeOfString (string $ string ): string
154166 {
155- $ jd = json_decode ($ string , false );
167+ $ decoded = json_decode ($ string , false );
156168
157169 if (is_numeric ($ string ))
158170 {
159- if (strpos ($ string , '. ' ) !== false )
160- {
161- return 'FLOAT ' ;
162- }
163- else
164- {
165- return 'INT ' ;
166- }
171+ return strpos ($ string , '. ' ) !== false ? 'FLOAT ' : 'INT ' ;
167172 }
168- elseif (is_bool ($ string) || $ string === ' true ' || $ string === ' false ' )
173+ elseif (filter_var ($ string, FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE ) !== null )
169174 {
170175 return 'BOOL ' ;
171176 }
@@ -179,16 +184,14 @@ protected static function typeOfString (string $string): string
179184 }
180185 elseif (json_last_error () === JSON_ERROR_NONE )
181186 {
182- return match (gettype ($ jd ))
187+ return match (gettype ($ decoded ))
183188 {
184189 'object ' => 'JSON ' ,
185190 'array ' => 'ARRAY ' ,
186191 default => 'STRING ' ,
187192 };
188193 }
189- else
190- {
191- return 'STRING ' ;
192- }
194+
195+ return 'STRING ' ;
193196 }
194197}
0 commit comments