Skip to content

Commit

Permalink
Adding throwable method for IDN conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 7, 2023
1 parent 1be22d0 commit e2ef23d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Idna/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ final class Converter
^(?:(?&reg_name)\.)*(?&reg_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.
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit e2ef23d

Please sign in to comment.