@@ -30,18 +30,24 @@ final class StringSchema extends AbstractSchema implements SchemaInterface
3030 public const string ERROR_ENDSWITH_CODE = 'string.endsWith ' ;
3131 public const string ERROR_ENDSWITH_TEMPLATE = '{{given}} does not ends with {{endsWith}} ' ;
3232
33- public const string ERROR_MATCH_CODE = 'string.match ' ;
34- public const string ERROR_MATCH_TEMPLATE = '{{given}} does not match {{match}} ' ;
35-
36- public const string ERROR_REGEXP_CODE = 'string.regexp ' ;
37- public const string ERROR_REGEXP_TEMPLATE = '{{given}} does not regexp {{regexp}} ' ;
33+ public const string ERROR_DOMAIN_CODE = 'string.domain ' ;
34+ public const string ERROR_DOMAIN_TEMPLATE = 'Invalid domain {{given}} ' ;
3835
3936 public const string ERROR_EMAIL_CODE = 'string.email ' ;
4037 public const string ERROR_EMAIL_TEMPLATE = 'Invalid email {{given}} ' ;
4138
4239 public const string ERROR_IP_CODE = 'string.ip ' ;
4340 public const string ERROR_IP_TEMPLATE = 'Invalid ip {{version}} {{given}} ' ;
4441
42+ public const string ERROR_MAC_CODE = 'string.mac ' ;
43+ public const string ERROR_MAC_TEMPLATE = 'Invalid mac {{given}} ' ;
44+
45+ public const string ERROR_MATCH_CODE = 'string.match ' ;
46+ public const string ERROR_MATCH_TEMPLATE = '{{given}} does not match {{match}} ' ;
47+
48+ public const string ERROR_REGEXP_CODE = 'string.regexp ' ;
49+ public const string ERROR_REGEXP_TEMPLATE = '{{given}} does not regexp {{regexp}} ' ;
50+
4551 public const string ERROR_URL_CODE = 'string.url ' ;
4652 public const string ERROR_URL_TEMPLATE = 'Invalid url {{given}} ' ;
4753
@@ -200,49 +206,15 @@ public function endsWith(string $endsWith): static
200206 });
201207 }
202208
203- public function regexp ( string $ regexp ): static
209+ public function domain ( ): static
204210 {
205- if (false === @preg_match ($ regexp , '' )) {
206- throw new \InvalidArgumentException (\sprintf ('Invalid regexp "%s" given ' , $ regexp ));
207- }
208-
209- return $ this ->postParse (static function (string $ string ) use ($ regexp ) {
210- $ doesMatch = filter_var ($ string , FILTER_VALIDATE_REGEXP , ['options ' => ['regexp ' => $ regexp ]]);
211-
212- if (false === $ doesMatch ) {
213- throw new ErrorsException (
214- new Error (
215- self ::ERROR_REGEXP_CODE ,
216- self ::ERROR_REGEXP_TEMPLATE ,
217- ['regexp ' => $ regexp , 'given ' => $ string ]
218- )
219- );
220- }
221-
222- return $ string ;
223- });
224- }
225-
226- /**
227- * @deprecated: use regexp
228- */
229- public function match (string $ match ): static
230- {
231- @trigger_error ('Use regexp instead ' , E_USER_DEPRECATED );
232-
233- if (false === @preg_match ($ match , '' )) {
234- throw new \InvalidArgumentException (\sprintf ('Invalid match "%s" given ' , $ match ));
235- }
236-
237- return $ this ->postParse (static function (string $ string ) use ($ match ) {
238- $ doesMatch = filter_var ($ string , FILTER_VALIDATE_REGEXP , ['options ' => ['regexp ' => $ match ]]);
239-
240- if (false === $ doesMatch ) {
211+ return $ this ->postParse (static function (string $ string ) {
212+ if (!filter_var ($ string , FILTER_VALIDATE_DOMAIN )) {
241213 throw new ErrorsException (
242214 new Error (
243- self ::ERROR_MATCH_CODE ,
244- self ::ERROR_MATCH_TEMPLATE ,
245- ['match ' => $ match , ' given ' => $ string ]
215+ self ::ERROR_DOMAIN_CODE ,
216+ self ::ERROR_DOMAIN_TEMPLATE ,
217+ ['given ' => $ string ]
246218 )
247219 );
248220 }
@@ -302,6 +274,74 @@ public function ipV6(): static
302274 });
303275 }
304276
277+ public function mac (): static
278+ {
279+ return $ this ->postParse (static function (string $ string ) {
280+ if (!filter_var ($ string , FILTER_VALIDATE_MAC )) {
281+ throw new ErrorsException (
282+ new Error (
283+ self ::ERROR_MAC_CODE ,
284+ self ::ERROR_MAC_TEMPLATE ,
285+ ['given ' => $ string ]
286+ )
287+ );
288+ }
289+
290+ return $ string ;
291+ });
292+ }
293+
294+ /**
295+ * @deprecated: use regexp
296+ */
297+ public function match (string $ match ): static
298+ {
299+ @trigger_error ('Use regexp instead ' , E_USER_DEPRECATED );
300+
301+ if (false === @preg_match ($ match , '' )) {
302+ throw new \InvalidArgumentException (\sprintf ('Invalid match "%s" given ' , $ match ));
303+ }
304+
305+ return $ this ->postParse (static function (string $ string ) use ($ match ) {
306+ $ doesMatch = filter_var ($ string , FILTER_VALIDATE_REGEXP , ['options ' => ['regexp ' => $ match ]]);
307+
308+ if (false === $ doesMatch ) {
309+ throw new ErrorsException (
310+ new Error (
311+ self ::ERROR_MATCH_CODE ,
312+ self ::ERROR_MATCH_TEMPLATE ,
313+ ['match ' => $ match , 'given ' => $ string ]
314+ )
315+ );
316+ }
317+
318+ return $ string ;
319+ });
320+ }
321+
322+ public function regexp (string $ regexp ): static
323+ {
324+ if (false === @preg_match ($ regexp , '' )) {
325+ throw new \InvalidArgumentException (\sprintf ('Invalid regexp "%s" given ' , $ regexp ));
326+ }
327+
328+ return $ this ->postParse (static function (string $ string ) use ($ regexp ) {
329+ $ doesMatch = filter_var ($ string , FILTER_VALIDATE_REGEXP , ['options ' => ['regexp ' => $ regexp ]]);
330+
331+ if (false === $ doesMatch ) {
332+ throw new ErrorsException (
333+ new Error (
334+ self ::ERROR_REGEXP_CODE ,
335+ self ::ERROR_REGEXP_TEMPLATE ,
336+ ['regexp ' => $ regexp , 'given ' => $ string ]
337+ )
338+ );
339+ }
340+
341+ return $ string ;
342+ });
343+ }
344+
305345 public function url (): static
306346 {
307347 return $ this ->postParse (static function (string $ string ) {
0 commit comments