| Total Complexity | 18 |
| Total Lines | 128 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | abstract class ValidationUtils |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Checks that $mixed value is an email. |
||
| 11 | * |
||
| 12 | * @param mixed $mixed |
||
| 13 | * |
||
| 14 | * @return bool |
||
| 15 | */ |
||
| 16 | public static function isEmail($mixed): bool |
||
| 17 | { |
||
| 18 | return |
||
| 19 | is_string($mixed) && |
||
| 20 | // Use of preg_replace instead of preg_match, as preg_match acts unexpectedly with this regular expression |
||
| 21 | preg_replace('#[a-zA-Z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+(?:\.[a-zA-Z0-9\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+)*\@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+(?:[a-z]{2}|aero|asia|biz|cat|com|coop|info|int|jobs|mobi|museum|name|net|org|pro|tel|travel|xxx|edu|gov|mil)$#', 'ok', $mixed) === 'ok' |
||
| 22 | ; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Checks that $mixed value is a hexadecimal value. |
||
| 27 | * |
||
| 28 | * @param mixed $mixed |
||
| 29 | * |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | public static function isHexadecimal($mixed) |
||
| 33 | { |
||
| 34 | return |
||
| 35 | is_string($mixed) && |
||
| 36 | preg_match('/^\b[0-9A-F]{6}\b$/i', $mixed) |
||
| 37 | ; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Checks that $mixed value is an IP (v4 or v6). |
||
| 42 | * |
||
| 43 | * @param mixed $mixed |
||
| 44 | * |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | public static function isIp($mixed): bool |
||
| 48 | { |
||
| 49 | return |
||
| 50 | is_string($mixed) && |
||
| 51 | ( |
||
| 52 | // IPv4 check |
||
| 53 | preg_match('#^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|0?[0-9]?[1-9]|0?[1-9][0-9])\.)((25[0-5]|(2[0-4]|(1|0)?[0-9])?[0-9])\.){2}(25[0-5]|(2[0-4]|(1|0)?[0-9])?[0-9])$#', $mixed) || |
||
| 54 | // IPv6 check |
||
| 55 | preg_match('#^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$#', $mixed) |
||
| 56 | ) |
||
| 57 | ; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Checks that $mixed value is a decimal number (float or integer). |
||
| 62 | * |
||
| 63 | * @param mixed $mixed |
||
| 64 | * |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public static function isNumber($mixed): bool |
||
| 68 | { |
||
| 69 | if (!is_numeric($mixed)) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | return (bool) preg_match('/^\-?[0-9]+\.?[0-9]*$/', (string) $mixed); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Checks that $string value is a phone number. |
||
| 78 | * |
||
| 79 | * @param string $string |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public static function isPhoneNumber(string $string): bool |
||
| 84 | { |
||
| 85 | // Sanitize string before the test itself |
||
| 86 | $string = preg_replace('/(\(([0-9]+)\))+/', '$2', $string); |
||
| 87 | $string = preg_replace('/([0-9]+)([ \\\–\-\.\/]{1})/', '$1$3', $string); |
||
| 88 | |||
| 89 | return (bool) preg_match('/^(\(?\+\ ?[0-9]{1,4}\)?)?\ ?[0-9]{7,15}$/', $string); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Checks that $mixed value is a positive integer (primary key). |
||
| 94 | * |
||
| 95 | * @param mixed $mixed |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public static function isPositiveInt($mixed): bool |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Checks that $mixed value is an URL. |
||
| 110 | * |
||
| 111 | * @param mixed $mixed |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public static function isURL($mixed): bool |
||
| 116 | { |
||
| 117 | return |
||
| 118 | is_string($mixed) && |
||
| 119 | preg_match('_^(https?|ftp)://(\S+(:\S*)?@)?(([1-9]|[1-9]\d|1\d\d|2[0-1]\d|22[0-3])(\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])){2}(\.([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-4]))|(([a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(\.([a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(\.([a-z\x{00a1}-\x{ffff}]{2,})))(:\d{2,5})?(/[^\s]*)?$_iuS', $mixed) |
||
| 120 | ; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Checks that $mixed value is magnet URL. |
||
| 125 | * |
||
| 126 | * @param mixed $mixed |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public static function isURLMagnet($mixed): bool |
||
| 135 | ; |
||
| 136 | } |
||
| 137 | } |
||
| 138 |