@@ -22,4 +22,90 @@ protected function validateType(mixed $value, string $key): mixed
2222 }
2323 return $ value ;
2424 }
25+
26+ public function email (?string $ message = null ): static
27+ {
28+ return $ this ->addValidation (
29+ fn ($ value , $ key = null , $ input = null ) => filter_var ($ value , FILTER_VALIDATE_EMAIL ) !== false ,
30+ $ message ?? 'Value must be a valid email address. '
31+ );
32+ }
33+
34+ public function url (?string $ message = null ): static
35+ {
36+ return $ this ->addValidation (
37+ fn ($ value , $ key = null , $ input = null ) => filter_var ($ value , FILTER_VALIDATE_URL ) !== false ,
38+ $ message ?? 'Value must be a valid URL. '
39+ );
40+ }
41+
42+ public function uuid (?string $ message = null ): static
43+ {
44+ return $ this ->addValidation (
45+ fn ($ value , $ key = null , $ input = null ) => preg_match ('/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i ' , $ value ) === 1 ,
46+ $ message ?? 'Value must be a valid UUID. '
47+ );
48+ }
49+
50+ public function ip (?string $ message = null ): static
51+ {
52+ return $ this ->addValidation (
53+ fn ($ value , $ key = null , $ input = null ) => filter_var ($ value , FILTER_VALIDATE_IP ) !== false ,
54+ $ message ?? 'Value must be a valid IP address. '
55+ );
56+ }
57+
58+ public function minLength (int $ min , ?string $ message = null ): static
59+ {
60+ return $ this ->addValidation (
61+ fn ($ value , $ key = null , $ input = null ) => mb_strlen ($ value ) >= $ min ,
62+ $ message ?? "Value must be at least {$ min } characters long. "
63+ );
64+ }
65+
66+ public function maxLength (int $ max , ?string $ message = null ): static
67+ {
68+ return $ this ->addValidation (
69+ fn ($ value , $ key = null , $ input = null ) => mb_strlen ($ value ) <= $ max ,
70+ $ message ?? "Value must be at most {$ max } characters long. "
71+ );
72+ }
73+
74+ public function length (int $ exact , ?string $ message = null ): static
75+ {
76+ return $ this ->addValidation (
77+ fn ($ value , $ key = null , $ input = null ) => mb_strlen ($ value ) === $ exact ,
78+ $ message ?? "Value must be exactly {$ exact } characters long. "
79+ );
80+ }
81+
82+ public function pattern (string $ regex , ?string $ message = null ): static
83+ {
84+ return $ this ->addValidation (
85+ fn ($ value , $ key = null , $ input = null ) => preg_match ($ regex , $ value ) === 1 ,
86+ $ message ?? 'Value does not match the required pattern. '
87+ );
88+ }
89+
90+ public function datetime (string $ format = 'Y-m-d\TH:i:s ' , ?string $ message = null ): static
91+ {
92+ return $ this ->addValidation (
93+ function ($ value , $ key = null , $ input = null ) use ($ format ) {
94+ $ date = \DateTime::createFromFormat ($ format , $ value );
95+ return $ date !== false && $ date ->format ($ format ) === $ value ;
96+ },
97+ $ message ?? "Value must be a valid datetime in format ' {$ format }'. "
98+ );
99+ }
100+
101+ public function date (string $ format = 'Y-m-d ' , ?string $ message = null ): static
102+ {
103+ return $ this ->addValidation (
104+ function ($ value , $ key = null , $ input = null ) use ($ format ) {
105+ $ date = \DateTime::createFromFormat ($ format , $ value );
106+ return $ date !== false && $ date ->format ($ format ) === $ value ;
107+ },
108+ $ message ?? "Value must be a valid date in format ' {$ format }'. "
109+ );
110+ }
25111}
0 commit comments