[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace Algo26\IdnaConvert\Punycode; 4 5 class FromPunycode extends AbstractPunycode implements PunycodeInterface 6 { 7 public function __construct(?string $idnVersion = null) 8 { 9 parent::__construct(); 10 } 11 12 /** 13 * The actual decoding algorithm 14 * @param string 15 * @return mixed 16 */ 17 public function convert($encoded) 18 { 19 if (!$this->validate($encoded)) { 20 return false; 21 } 22 23 $decoded = []; 24 // Find last occurrence of the delimiter 25 $delimiterPosition = strrpos($encoded, '-'); 26 if ($delimiterPosition > self::byteLength(self::punycodePrefix)) { 27 for ($k = $this->byteLength(self::punycodePrefix); $k < $delimiterPosition; ++$k) { 28 $decoded[] = ord($encoded[$k]); 29 } 30 } 31 $decodedLength = count($decoded); 32 $encodedLength = $this->byteLength($encoded); 33 34 // Wandering through the strings; init 35 $isFirst = true; 36 $bias = self::initialBias; 37 $currentIndex = 0; 38 $char = self::initialN; 39 40 for ($encodedIndex = ($delimiterPosition) ? ($delimiterPosition + 1) : 0; $encodedIndex < $encodedLength; ++$decodedLength) { 41 for ($oldIndex = $currentIndex, $w = 1, $k = self::base; 1; $k += self::base) { 42 $digit = $this->decodeDigit($encoded[$encodedIndex++]); 43 $currentIndex += $digit * $w; 44 $t = ($k <= $bias) 45 ? self::tMin 46 : ( 47 ($k >= $bias + self::tMax) 48 ? self::tMax 49 : ($k - $bias) 50 ); 51 if ($digit < $t) { 52 break; 53 } 54 $w = (int) ($w * (self::base - $t)); 55 } 56 $bias = $this->adapt($currentIndex - $oldIndex, $decodedLength + 1, $isFirst); 57 $isFirst = false; 58 $char += (int) ($currentIndex / ($decodedLength + 1)); 59 $currentIndex %= ($decodedLength + 1); 60 if ($decodedLength > 0) { 61 // Make room for the decoded char 62 for ($i = $decodedLength; $i > $currentIndex; $i--) { 63 $decoded[$i] = $decoded[($i - 1)]; 64 } 65 } 66 $decoded[$currentIndex++] = $char; 67 } 68 69 return $this->unicodeTransCoder->convert( 70 $decoded, 71 $this->unicodeTransCoder::FORMAT_UCS4_ARRAY, 72 $this->unicodeTransCoder::FORMAT_UTF8 73 ); 74 } 75 76 77 /** 78 * Checks, whether or not the provided string is a valid punycode string 79 * @param string $encoded 80 * @return boolean 81 */ 82 private function validate($encoded): bool 83 { 84 // Check for existence of the prefix 85 if (strpos($encoded, self::punycodePrefix) !== 0) { 86 return false; 87 } 88 89 // If nothing is left after the prefix, it is hopeless 90 if (strlen(trim($encoded)) <= strlen(self::punycodePrefix)) { 91 return false; 92 } 93 94 return true; 95 } 96 97 private function decodeDigit(string $cp): int 98 { 99 $cp = ord($cp); 100 if ($cp - 48 < 10) { 101 return $cp - 22; 102 } 103 104 if ($cp - 65 < 26) { 105 return $cp - 65; 106 } 107 108 if ($cp - 97 < 26) { 109 return $cp - 97; 110 } 111 112 return self::base; 113 } 114 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |