- md5
- 2e3605f98dc9a37004c7f618609c692a
- sha1
- ee9290115695c60c1bd0fae70ecac4224871c791
- sha256
- eff5d49f9c4a8790e3de40965775218d58be2f5140c34f4774260cb8389a35ae
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
<?php
declare(strict_types=1);
namespace Algo26\IdnaConvert\Punycode;
use Algo26\IdnaConvert\Exception\AlreadyPunycodeException;
use Algo26\IdnaConvert\Exception\InvalidCharacterException;
use Algo26\IdnaConvert\Exception\InvalidIdnVersionException;
use Algo26\IdnaConvert\Exception\Std3AsciiRulesViolationException;
use Algo26\IdnaConvert\NamePrep\NamePrep;
class ToPunycode extends AbstractPunycode implements PunycodeInterface
{
private NamePrep $namePrep;
/**
* @throws InvalidIdnVersionException
*/
public function __construct(
?int $idnVersion = null,
private readonly ?bool $useStd3AsciiRules = false
) {
$this->namePrep = new NamePrep($idnVersion);
parent::__construct();
}
/**
* @throws AlreadyPunycodeException
* @throws InvalidCharacterException
* @throws Std3AsciiRulesViolationException
*/
public function convert(array $decoded): ?string
{
$decoded = $this->namePrep->do($decoded);
$this->checkConvertPreconditions($decoded);
// We will not try to encode strings consisting of basic code points only
$canEncode = $this->checkForNonBasicCodepoints($decoded);
$decodedLength = count($decoded);
if (!$decodedLength) {
return null; // Empty array
}
$codeCount = 0; // How many chars have been consumed
$encoded = '';
// Copy all basic code points to output
for ($i = 0; $i < $decodedLength; ++$i) {
$test = $decoded[$i];
if (0x01 <= $test && $test <= 0x7f) {
$encoded .= chr($decoded[$i]);
$codeCount++;
}
}
if (!$canEncode) {
return $encoded;
}
if ($codeCount === $decodedLength) {
return $encoded; // All codepoints were basic ones
}
// Start with the prefix
$encoded = self::PUNYCODE_PREFIX . $encoded;
// If we have basic code points in output, add a hyphen to the end
if ($codeCount > 0) {
$encoded .= '-';
}
// Now find and encode all non-basic code points
$isFirst = true;
$currentCode = self::INITIAL_N;
$bias = self::INITIAL_BIAS;
$delta = 0;
while ($codeCount < $decodedLength) {
$nextCode = self::MAX_UCS;
// Find the next largest code point to $currentCode
foreach ($decoded as $nextLargestCandidate) {
if ($nextLargestCandidate >= $currentCode && $nextLargestCandidate <= $nextCode) {
$nextCode = $nextLargestCandidate;
}
}
$codeCountPlusOne = $codeCount + 1;
$delta += ($nextCode - $currentCode) * $codeCountPlusOne;
$currentCode = $nextCode;
// Scan input again and encode all characters whose code point is $currentCode
for ($i = 0; $i < $decodedLength; $i++) {
if ($decoded[$i] < $currentCode) {
$delta++;
}
if ($decoded[$i] === $currentCode) {
for ($q = $delta, $k = self::BASE; 1; $k += self::BASE) {
$t = ($k <= $bias)
? self::T_MIN
: (($k >= $bias + self::T_MAX)
? self::T_MAX
: $k - $bias
);
if ($q < $t) {
break;
}
$encoded .= $this->encodeDigit(intval($t + (($q - $t) % (self::BASE - $t))));
$q = (int) (($q - $t) / (self::BASE - $t));
}
$encoded .= $this->encodeDigit($q);
$bias = $this->adapt($delta, $codeCountPlusOne, $isFirst);
$codeCount++;
$delta = 0;
$isFirst = false;
}
}
$delta++;
$currentCode++;
}
return $encoded;
}
private function encodeDigit(int $digit): string
{
return chr($digit + 22 + 75 * ($digit < 26));
}
/**
* @throws AlreadyPunycodeException
* @throws Std3AsciiRulesViolationException
*/
private function checkConvertPreconditions(array $decoded): void
{
// We cannot encode a domain name containing the Punycode prefix
$checkForPrefix = array_slice($decoded, 0, self::$prefixLength);
if (self::$prefixAsArray === $checkForPrefix) {
throw new AlreadyPunycodeException('This is already a Punycode string', 100);
}
if (!$this->useStd3AsciiRules) {
return;
}
if ($decoded[0] === '-'
|| $decoded[array_key_last($decoded)] === '-'
) {
throw new Std3AsciiRulesViolationException('No trailing / leading hyphens allowed', 103);
}
foreach ($decoded as $index => $codePoint) {
if (!preg_match('[-a-zA-Z0-9]u', chr($codePoint))) {
throw new Std3AsciiRulesViolationException(
sprintf('Character at offset %d is outside the legal range', $index),
104,
);
}
}
}
private function checkForNonBasicCodepoints(array $decoded): bool
{
foreach ($decoded as $codePoint) {
if ($codePoint > 0x7a) {
return true;
}
}
return false;
}
}
The same content is in 50 releases, Joomla 6.0.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.