1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\Question; |
4
|
|
|
|
5
|
|
|
final class CommonValidators |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param string[]|null $additionalCharacters |
9
|
|
|
* @param null|string $hint |
10
|
|
|
* @return \Closure |
11
|
|
|
*/ |
12
|
|
|
public static function getAlphaValidator(?array $additionalCharacters = null, ?string $hint = null): \Closure |
13
|
|
|
{ |
14
|
|
|
return function (string $value) use ($additionalCharacters, $hint) { |
15
|
|
|
$value = trim($value); |
16
|
|
|
$pattern = '/^[a-zA-Z0-9'; |
17
|
|
|
|
18
|
|
|
if (!empty($additionalCharacters)) { |
19
|
|
|
foreach ($additionalCharacters as $character) { |
20
|
|
|
$pattern .= $character; |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$pattern .= ']+$/'; |
25
|
|
|
|
26
|
|
|
if (!\preg_match($pattern, $value)) { |
27
|
|
|
$message = 'Invalid value "' . $value . '".'; |
28
|
|
|
if (!empty($hint)) { |
29
|
|
|
$message .= "Hint: $hint"; |
30
|
|
|
} |
31
|
|
|
throw new \InvalidArgumentException($message); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $value; |
35
|
|
|
}; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function getAbsolutePathValidator(): \Closure |
39
|
|
|
{ |
40
|
|
|
return function (string $value) { |
41
|
|
|
$value = trim($value); |
42
|
|
|
if (!\preg_match('/^[\'"]?(?:\/[^\/\n]+)*[\'"]?$/', $value)) { |
43
|
|
|
throw new \InvalidArgumentException('Invalid value "' . $value . '". Hint: path has to be absolute without trailing "/".'); |
44
|
|
|
} |
45
|
|
|
return $value; |
46
|
|
|
}; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function getDomainNameValidator(): \Closure |
50
|
|
|
{ |
51
|
|
|
return function (string $value) { |
52
|
|
|
$value = trim($value); |
53
|
|
|
if (!\preg_match('/^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{2,11}?$/im', $value)) { |
54
|
|
|
throw new \InvalidArgumentException('Invalid value "' . $value . '". Hint: the domain name must not start with "http(s)://".'); |
55
|
|
|
} |
56
|
|
|
return $value; |
57
|
|
|
}; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function getDomainNameWithPortValidator(): \Closure |
61
|
|
|
{ |
62
|
|
|
return function (string $value) { |
63
|
|
|
$value = trim($value); |
64
|
|
|
if (!\preg_match('/^(?!:\/\/)([a-zA-Z0-9-_]+\.)*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{2,11}?:[0-9]*$/im', $value)) { |
65
|
|
|
throw new \InvalidArgumentException('Invalid value "' . $value . '". Hint: the domain name must not start with "http(s)://".'); |
66
|
|
|
} |
67
|
|
|
return $value; |
68
|
|
|
}; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function getIPv4Validator(): \Closure |
72
|
|
|
{ |
73
|
|
|
return function (string $value) { |
74
|
|
|
$value = trim($value); |
75
|
|
|
if (!\preg_match('/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $value)) { |
76
|
|
|
throw new \InvalidArgumentException('Invalid value "' . $value . '".'); |
77
|
|
|
} |
78
|
|
|
return $value; |
79
|
|
|
}; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|