| Total Complexity | 8 |
| Total Lines | 71 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class Validate |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Regular expression for MAC tests |
||
| 13 | */ |
||
| 14 | const REGEXP_MAC = '/^([a-fA-F0-9]{2}[-:]){5}[0-9A-Fa-f]{2}|([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}/i'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Regular expression for UUID tests |
||
| 18 | */ |
||
| 19 | const REGEXP_UUID = '/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Check if MAC-address is valid |
||
| 23 | * |
||
| 24 | * @param string $mac mac-address for check |
||
| 25 | * @return bool |
||
| 26 | */ |
||
| 27 | public static function isValidMAC(string $mac): bool |
||
| 28 | { |
||
| 29 | return preg_match(self::REGEXP_MAC, $mac) === 1; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Check if URL is valid |
||
| 34 | * |
||
| 35 | * @param string $url |
||
| 36 | * @param bool $query |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | public static function isValidURL(string $url, bool $query = false): bool |
||
| 40 | { |
||
| 41 | if ( |
||
| 42 | true === $query && |
||
| 43 | !filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === false |
||
| 44 | ) { |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!filter_var($url, FILTER_VALIDATE_URL) === false) { |
||
| 49 | return true; |
||
| 50 | } |
||
| 51 | |||
| 52 | return false; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Simple email validation |
||
| 57 | * |
||
| 58 | * @param string $email |
||
| 59 | * @param bool $sanitize enable email sanitize filter |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | public static function isValidEmail(string $email, bool $sanitize = true): bool |
||
| 63 | { |
||
| 64 | // Remove all illegal characters from email |
||
| 65 | if (true === $sanitize) { |
||
| 66 | $email = filter_var($email, FILTER_SANITIZE_EMAIL); |
||
| 67 | } |
||
| 68 | return !filter_var($email, FILTER_VALIDATE_EMAIL) === false; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Check if UUID is valid |
||
| 73 | * |
||
| 74 | * @param string $uuid |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public static function isValidUUID(string $uuid): bool |
||
| 80 | } |
||
| 81 | |||
| 82 | } |
||
| 83 |