| @@ 903-929 (lines=27) @@ | ||
| 900 | * |
|
| 901 | * The URL pattern is taken from Symfony: @see https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php |
|
| 902 | */ |
|
| 903 | public static function url($value, $message = '') |
|
| 904 | { |
|
| 905 | $pattern = '~^ |
|
| 906 | (http|https):// # protocol |
|
| 907 | (((?:[\_\.\pL\pN-]|%%[0-9A-Fa-f]{2})+:)?((?:[\_\.\pL\pN-]|%%[0-9A-Fa-f]{2})+)@)? # basic auth |
|
| 908 | ( |
|
| 909 | ([\pL\pN\pS\-\_\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name |
|
| 910 | | # or |
|
| 911 | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address |
|
| 912 | | # or |
|
| 913 | \[ |
|
| 914 | (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(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:[0-9a-f]{1,4})))?::)))) |
|
| 915 | \] # an IPv6 address |
|
| 916 | ) |
|
| 917 | (:[0-9]+)? # a port (optional) |
|
| 918 | (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )* # a path |
|
| 919 | (?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a query (optional) |
|
| 920 | (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional) |
|
| 921 | $~ixu'; |
|
| 922 | ||
| 923 | if (!\preg_match($pattern, $value)) { |
|
| 924 | static::reportInvalidArgument(\sprintf( |
|
| 925 | $message ?: 'Expected a value to be a valid URL. Got %s', |
|
| 926 | static::valueToString($value) |
|
| 927 | )); |
|
| 928 | } |
|
| 929 | } |
|
| 930 | ||
| 931 | /** |
|
| 932 | * @param mixed $value |
|
| @@ 1235-1243 (lines=9) @@ | ||
| 1232 | * |
|
| 1233 | * @throws InvalidArgumentException |
|
| 1234 | */ |
|
| 1235 | public static function notWhitespaceOnly($value, $message = '') |
|
| 1236 | { |
|
| 1237 | if (\preg_match('/^\s*$/', $value)) { |
|
| 1238 | static::reportInvalidArgument(\sprintf( |
|
| 1239 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
| 1240 | static::valueToString($value) |
|
| 1241 | )); |
|
| 1242 | } |
|
| 1243 | } |
|
| 1244 | ||
| 1245 | /** |
|
| 1246 | * @psalm-pure |
|
| @@ 1363-1371 (lines=9) @@ | ||
| 1360 | * |
|
| 1361 | * @throws InvalidArgumentException |
|
| 1362 | */ |
|
| 1363 | public static function regex($value, $pattern, $message = '') |
|
| 1364 | { |
|
| 1365 | if (!\preg_match($pattern, $value)) { |
|
| 1366 | static::reportInvalidArgument(\sprintf( |
|
| 1367 | $message ?: 'The value %s does not match the expected pattern.', |
|
| 1368 | static::valueToString($value) |
|
| 1369 | )); |
|
| 1370 | } |
|
| 1371 | } |
|
| 1372 | ||
| 1373 | /** |
|
| 1374 | * @psalm-pure |
|