/** * @param $hostname * @return bool */ public static function validate(string $hostname) : bool { if (in_array($hostname, ['@', '*'])) { return true; } if (strpos($hostname, '@') !== false && strlen($hostname) > 1) { return false; } $hostname = preg_replace('/^\\*\\./', '', $hostname); // wild card allowed in hostname $hostnameValidation = HostnameValidator::validate($hostname); if ($hostnameValidation) { return true; } $ipValidation = Ip4Validator::validate($hostname); if ($ipValidation) { return true; } return false; }
/** * Process domain part validation * * @param string $value The domain part of an email address to validate * @param bool $send_errors Does the function must throw exceptions on validation failures ? * @return bool TRUE if $value pass the Email validation * @throws \Exception for each invalid part if `$send_errors` is true */ public function validateDomainPart($value, $send_errors = false) { // the domain name must be an IP address between brackets ... if (substr($value, 0, 1) == '[' && substr($value, -1, 1) == ']') { $ip_domain_part = substr($value, 1, strlen($value) - 2); // is it an IPv6 if (substr($ip_domain_part, 0, strlen('IPv6:')) == 'IPv6:') { $ip6_domain_part = substr($ip_domain_part, strlen('IPv6:')); $ip6HostnameValidator = new InternetProtocolValidator('v6'); try { if (false === ($ip6domain_name_valid = $ip6HostnameValidator->validate($ip6_domain_part, $send_errors))) { return false; } } catch (\Exception $e) { throw $e; } } else { $ip4HostnameValidator = new InternetProtocolValidator(); try { if (false === ($ip4domain_name_valid = $ip4HostnameValidator->validate($ip_domain_part, $send_errors))) { return false; } } catch (\Exception $e) { throw $e; } } } else { $hostnameValidator = new HostnameValidator(); try { if (false === ($hostdomain_name_valid = $hostnameValidator->validate($value, $send_errors))) { return false; } } catch (\Exception $e) { throw $e; } } return true; }