public static function isValidDomain($domain)
    {
        $valid = mb_strlen($domain, 'UTF-8') <= self::PHP_IDN_HANDLEABLE_LENGTH;

        if ($valid) {
            $result = idn_to_ascii(
                $domain,
                IDNA_USE_STD3_RULES | IDNA_NONTRANSITIONAL_TO_ASCII,
                INTL_IDNA_VARIANT_UTS46
            );

            if (!is_string($result)) {
                $valid = false;
            }
        }
        
        if ($valid) {
            $domainNameLength = strlen($result);
            if ($domainNameLength < 1 || $domainNameLength > 253) {
                $valid = false;
            }
        }
        
        if ($valid) {
            foreach (explode('.', $result) as $label) {
                $labelLength = strlen($label);
                if ($labelLength < 1 || $labelLength > 63) {
                    $valid = false;
                    break;
                }
            }
        }
        
        if ($valid) {
            $result = idn_to_utf8($result, IDNA_USE_STD3_RULES, INTL_IDNA_VARIANT_UTS46, $idna_info);
            if ($idna_info['errors'] !== 0) {
                $valid = false;
            }
        }
        
        return $valid;
    }