1 | <?php |
||
9 | class Punycode |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Bootstring parameter values |
||
14 | * |
||
15 | */ |
||
16 | const BASE = 36; |
||
17 | const TMIN = 1; |
||
18 | const TMAX = 26; |
||
19 | const SKEW = 38; |
||
20 | const DAMP = 700; |
||
21 | const INITIAL_BIAS = 72; |
||
22 | const INITIAL_N = 128; |
||
23 | const PREFIX = 'xn--'; |
||
24 | const DELIMITER = '-'; |
||
25 | |||
26 | /** |
||
27 | * Encode table |
||
28 | * |
||
29 | * @param array |
||
30 | */ |
||
31 | protected static $encodeTable = array( |
||
32 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', |
||
33 | 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', |
||
34 | 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * Decode table |
||
39 | * |
||
40 | * @param array |
||
41 | */ |
||
42 | protected static $decodeTable = array( |
||
43 | 'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5, |
||
44 | 'g' => 6, 'h' => 7, 'i' => 8, 'j' => 9, 'k' => 10, 'l' => 11, |
||
45 | 'm' => 12, 'n' => 13, 'o' => 14, 'p' => 15, 'q' => 16, 'r' => 17, |
||
46 | 's' => 18, 't' => 19, 'u' => 20, 'v' => 21, 'w' => 22, 'x' => 23, |
||
47 | 'y' => 24, 'z' => 25, '0' => 26, '1' => 27, '2' => 28, '3' => 29, |
||
48 | '4' => 30, '5' => 31, '6' => 32, '7' => 33, '8' => 34, '9' => 35 |
||
49 | ); |
||
50 | |||
51 | /** |
||
52 | * Character encoding |
||
53 | * |
||
54 | * @param string |
||
55 | */ |
||
56 | protected $encoding; |
||
57 | |||
58 | /** |
||
59 | * Constructor |
||
60 | * |
||
61 | * @param string $encoding Character encoding |
||
62 | */ |
||
63 | 76 | public function __construct($encoding = 'UTF-8') |
|
67 | |||
68 | /** |
||
69 | * Encode a domain to its Punycode version |
||
70 | * |
||
71 | * @param string $input Domain name in Unicode to be encoded |
||
72 | * @return string Punycode representation in ASCII |
||
73 | */ |
||
74 | 38 | public function encode($input) |
|
97 | |||
98 | /** |
||
99 | * Encode non code code points |
||
100 | * |
||
101 | * @param string $input |
||
102 | * @param array $codePoints |
||
103 | * @return string |
||
104 | */ |
||
105 | 38 | protected function encodeNonBasic($input, $codePoints) |
|
154 | |||
155 | /** |
||
156 | * Decode a Punycode domain name to its Unicode counterpart |
||
157 | * |
||
158 | * @param string $input Domain name in Punycode |
||
159 | * @return string Unicode domain name |
||
160 | */ |
||
161 | 38 | public function decode($input) |
|
176 | |||
177 | /** |
||
178 | * Decode a part of domain name, such as tld |
||
179 | * |
||
180 | * @param string $input Part of a domain name |
||
181 | * @return string Unicode domain part |
||
182 | */ |
||
183 | 38 | protected function decodePart($input) |
|
225 | |||
226 | /** |
||
227 | * Calculate the bias threshold to fall between TMIN and TMAX |
||
228 | * |
||
229 | * @param integer $k |
||
230 | * @param integer $bias |
||
231 | * @return integer |
||
232 | */ |
||
233 | 76 | protected function calculateThreshold($k, $bias) |
|
242 | |||
243 | /** |
||
244 | * Bias adaptation |
||
245 | * |
||
246 | * @param integer $delta |
||
247 | * @param integer $numPoints |
||
248 | * @param boolean $firstTime |
||
249 | * @return integer |
||
250 | */ |
||
251 | 76 | protected function adapt($delta, $numPoints, $firstTime) |
|
269 | |||
270 | /** |
||
271 | * List code points for a given input |
||
272 | * |
||
273 | * @param string $input |
||
274 | * @return array Multi-dimension array with basic, non-basic and aggregated code points |
||
275 | */ |
||
276 | 38 | protected function listCodePoints($input) |
|
297 | |||
298 | /** |
||
299 | * Convert a single or multi-byte character to its code point |
||
300 | * |
||
301 | * @param string $char |
||
302 | * @return integer |
||
303 | */ |
||
304 | 38 | protected function charToCodePoint($char) |
|
317 | |||
318 | /** |
||
319 | * Convert a code point to its single or multi-byte character |
||
320 | * |
||
321 | * @param integer $code |
||
322 | * @return string |
||
323 | */ |
||
324 | 44 | protected function codePointToChar($code) |
|
336 | } |
||
337 |