22namespace ExampleApp ;
33require_once '../vendor/autoload.php ' ;
44
5- use Krishna \DataValidator \MultiLinedException ;
5+ use Krishna \DataValidator \ComplexException ;
6+ use Krishna \DataValidator \RecursiveValidator ;
67use Krishna \DataValidator \Returner ;
8+ use Krishna \DataValidator \RngFmt \AllowedValues ;
79use Krishna \DataValidator \TypeHandler ;
810use Krishna \DataValidator \TypeInterface ;
11+ use Krishna \DataValidator \Types \StringType ;
912use Krishna \DataValidator \Validator ;
1013
1114$ rng_50 = strval (new \Krishna \DataValidator \RngFmt \Num \Range (max: 50 ));
@@ -27,6 +30,19 @@ public static function validate($value, bool $allow_null = false) : Returner {
2730 return Returner::valid ('my_type3_ ' . strval ($ value ));
2831 }
2932}
33+ class MyStringType implements TypeInterface {
34+ public static function validate ($ value , bool $ allow_null = false ) : Returner {
35+ $ value = StringType::validate ($ value );
36+ if (!$ value ->valid ) {
37+ return $ value ;
38+ }
39+ $ value = explode ('. ' , $ value ->value );
40+ return Returner::valid ([
41+ 'head ' => $ value [0 ],
42+ 'body ' => $ value [1 ] ?? ''
43+ ]);
44+ }
45+ }
3046
3147TypeHandler::set_custom_type_class ('my_type1 ' , MyType1::class);
3248TypeHandler::set_multiple_custom_type_class ([
@@ -56,6 +72,39 @@ public static function validate($value, bool $allow_null = false) : Returner {
5672 } else {
5773 echo "<pre>Error: \n" , $ error ,"</pre> " ;
5874 }
59- } catch (MultiLinedException $ th ) {
75+ } catch (ComplexException $ th ) {
76+ var_dump ($ th ->getInfo ());
77+ }
78+
79+ echo '<hr /> ' ;
80+
81+ // Testing recursive validator
82+ try {
83+ $ dv = (new RecursiveValidator (MyStringType::class, step_title: 'Token to array ' ))
84+ ->then ([
85+ 'head ' => 'json64 ' ,
86+ 'body ' => 'json64 '
87+ ], step_title: 'Base64 JSON decode ' )
88+ ->then ([
89+ 'head ' => [
90+ 'alg ' => 'string@ ' . new AllowedValues ('HS256 ' , 'HS384 ' , 'HS512 ' , 'RS256 ' , 'RS384 ' , 'RS512 ' ),
91+ 'typ ' => 'string@ ' . new AllowedValues ('JWT ' )
92+ ],'body ' => [
93+ '?iss ' => 'string ' ,
94+ '?sub ' => 'string|null ' ,
95+ '?aud ' => 'string ' ,
96+ '?exp ' => 'unsigned ' ,
97+ '?nbf ' => 'unsigned ' ,
98+ '?iat ' => 'unsigned ' ,
99+ '?kid ' => 'unsigned '
100+ ]
101+ ], step_title: "Test structure of 'head' & 'body' " );
102+ ['valid ' => $ valid , 'value ' => $ data , 'error ' => $ error ] = $ dv ->validate ('eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b2tlbi50ZXN0LmNvbSIsImF1ZCI6ImFwcC50b2tlbi5jb20iLCJpYXQiOjE2NDM4OTE3NTgsIm5iZiI6MTY0Mzg5MTc1OCwiZXhwIjoxNjQzODkyMzU4LCJuYW1lIjoiQW5zaHUgS3Jpc2huYSIsImNpdHkiOiJCYW5nYWxvcmUifQ ' )->get_as_array ();
103+ if ($ valid ) {
104+ var_dump (['Data ' => $ data ]);
105+ } else {
106+ echo "<pre>Error on step ' {$ error ['step ' ]}' => " , $ error ['msg ' ]->getFormattedErrors ('; ' ) ,"</pre> " ;
107+ }
108+ } catch (ComplexException $ th ) {
60109 var_dump ($ th ->getInfo ());
61110}
0 commit comments