diff --git a/Idna/Converter.php b/Idna/Converter.php index 2289c1f..42013cd 100644 --- a/Idna/Converter.php +++ b/Idna/Converter.php @@ -45,6 +45,25 @@ final class Converter ^(?:(?®_name)\.)*(?®_name)\.?$ /ix'; + + /** + * Converts the input to its IDNA ASCII form or throw on failure. + * + * @see Converter::toAscii() + * + * @throws SyntaxError if the string can not be converted to UNICODE using IDN UTS46 algorithm + * @throws ConversionFailed if the conversion returns error + */ + public static function toAsciiOrFail(string $domain, Option|int $options = null): Result + { + $result = self::toAscii($domain, $options); + + return match (true) { + $result->hasErrors() => throw ConversionFailed::dueToError($domain, $result), + default => $result, + }; + } + /** * Converts the input to its IDNA ASCII form. * @@ -90,6 +109,23 @@ public static function toAscii(string $domain, Option|int $options = null): Resu ]); } + /** + * Converts the input to its IDNA UNICODE form or throw on failure. + * + * @see Converter::toUnicode() + * + * @throws ConversionFailed if the conversion returns error + */ + public static function toUnicodeOrFail(string $domain, Option|int $options = null): Result + { + $result = self::toUnicode($domain, $options); + + return match (true) { + $result->hasErrors() => throw ConversionFailed::dueToError($domain, $result), + default => $result, + }; + } + /** * Converts the input to its IDNA UNICODE form. *