| Total Complexity | 152 |
| Total Lines | 680 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Iconv often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Iconv, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | final class Iconv |
||
| 38 | { |
||
| 39 | public const ERROR_ILLEGAL_CHARACTER = 'iconv(): Detected an illegal character in input string'; |
||
| 40 | public const ERROR_WRONG_CHARSET = 'iconv(): Wrong charset, conversion from `%s\' to `%s\' is not allowed'; |
||
| 41 | |||
| 42 | public static $inputEncoding = 'utf-8'; |
||
| 43 | public static $outputEncoding = 'utf-8'; |
||
| 44 | public static $internalEncoding = 'utf-8'; |
||
| 45 | |||
| 46 | private static $alias = [ |
||
| 47 | 'utf8' => 'utf-8', |
||
| 48 | 'ascii' => 'us-ascii', |
||
| 49 | 'tis-620' => 'iso-8859-11', |
||
| 50 | 'cp1250' => 'windows-1250', |
||
| 51 | 'cp1251' => 'windows-1251', |
||
| 52 | 'cp1252' => 'windows-1252', |
||
| 53 | 'cp1253' => 'windows-1253', |
||
| 54 | 'cp1254' => 'windows-1254', |
||
| 55 | 'cp1255' => 'windows-1255', |
||
| 56 | 'cp1256' => 'windows-1256', |
||
| 57 | 'cp1257' => 'windows-1257', |
||
| 58 | 'cp1258' => 'windows-1258', |
||
| 59 | 'shift-jis' => 'cp932', |
||
| 60 | 'shift_jis' => 'cp932', |
||
| 61 | 'latin1' => 'iso-8859-1', |
||
| 62 | 'latin2' => 'iso-8859-2', |
||
| 63 | 'latin3' => 'iso-8859-3', |
||
| 64 | 'latin4' => 'iso-8859-4', |
||
| 65 | 'latin5' => 'iso-8859-9', |
||
| 66 | 'latin6' => 'iso-8859-10', |
||
| 67 | 'latin7' => 'iso-8859-13', |
||
| 68 | 'latin8' => 'iso-8859-14', |
||
| 69 | 'latin9' => 'iso-8859-15', |
||
| 70 | 'latin10' => 'iso-8859-16', |
||
| 71 | 'iso8859-1' => 'iso-8859-1', |
||
| 72 | 'iso8859-2' => 'iso-8859-2', |
||
| 73 | 'iso8859-3' => 'iso-8859-3', |
||
| 74 | 'iso8859-4' => 'iso-8859-4', |
||
| 75 | 'iso8859-5' => 'iso-8859-5', |
||
| 76 | 'iso8859-6' => 'iso-8859-6', |
||
| 77 | 'iso8859-7' => 'iso-8859-7', |
||
| 78 | 'iso8859-8' => 'iso-8859-8', |
||
| 79 | 'iso8859-9' => 'iso-8859-9', |
||
| 80 | 'iso8859-10' => 'iso-8859-10', |
||
| 81 | 'iso8859-11' => 'iso-8859-11', |
||
| 82 | 'iso8859-12' => 'iso-8859-12', |
||
| 83 | 'iso8859-13' => 'iso-8859-13', |
||
| 84 | 'iso8859-14' => 'iso-8859-14', |
||
| 85 | 'iso8859-15' => 'iso-8859-15', |
||
| 86 | 'iso8859-16' => 'iso-8859-16', |
||
| 87 | 'iso_8859-1' => 'iso-8859-1', |
||
| 88 | 'iso_8859-2' => 'iso-8859-2', |
||
| 89 | 'iso_8859-3' => 'iso-8859-3', |
||
| 90 | 'iso_8859-4' => 'iso-8859-4', |
||
| 91 | 'iso_8859-5' => 'iso-8859-5', |
||
| 92 | 'iso_8859-6' => 'iso-8859-6', |
||
| 93 | 'iso_8859-7' => 'iso-8859-7', |
||
| 94 | 'iso_8859-8' => 'iso-8859-8', |
||
| 95 | 'iso_8859-9' => 'iso-8859-9', |
||
| 96 | 'iso_8859-10' => 'iso-8859-10', |
||
| 97 | 'iso_8859-11' => 'iso-8859-11', |
||
| 98 | 'iso_8859-12' => 'iso-8859-12', |
||
| 99 | 'iso_8859-13' => 'iso-8859-13', |
||
| 100 | 'iso_8859-14' => 'iso-8859-14', |
||
| 101 | 'iso_8859-15' => 'iso-8859-15', |
||
| 102 | 'iso_8859-16' => 'iso-8859-16', |
||
| 103 | 'iso88591' => 'iso-8859-1', |
||
| 104 | 'iso88592' => 'iso-8859-2', |
||
| 105 | 'iso88593' => 'iso-8859-3', |
||
| 106 | 'iso88594' => 'iso-8859-4', |
||
| 107 | 'iso88595' => 'iso-8859-5', |
||
| 108 | 'iso88596' => 'iso-8859-6', |
||
| 109 | 'iso88597' => 'iso-8859-7', |
||
| 110 | 'iso88598' => 'iso-8859-8', |
||
| 111 | 'iso88599' => 'iso-8859-9', |
||
| 112 | 'iso885910' => 'iso-8859-10', |
||
| 113 | 'iso885911' => 'iso-8859-11', |
||
| 114 | 'iso885912' => 'iso-8859-12', |
||
| 115 | 'iso885913' => 'iso-8859-13', |
||
| 116 | 'iso885914' => 'iso-8859-14', |
||
| 117 | 'iso885915' => 'iso-8859-15', |
||
| 118 | 'iso885916' => 'iso-8859-16', |
||
| 119 | ]; |
||
| 120 | private static $translitMap = []; |
||
| 121 | private static $convertMap = []; |
||
| 122 | private static $errorHandler; |
||
|
|
|||
| 123 | private static $lastError; |
||
| 124 | |||
| 125 | private static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4]; |
||
| 126 | private static $isValidUtf8; |
||
| 127 | |||
| 128 | public static function iconv($inCharset, $outCharset, $str) |
||
| 129 | { |
||
| 130 | $str = (string) $str; |
||
| 131 | if ('' === $str) { |
||
| 132 | return ''; |
||
| 133 | } |
||
| 134 | |||
| 135 | // Prepare for //IGNORE and //TRANSLIT |
||
| 136 | |||
| 137 | $translit = $ignore = ''; |
||
| 138 | |||
| 139 | $outCharset = strtolower($outCharset); |
||
| 140 | $inCharset = strtolower($inCharset); |
||
| 141 | |||
| 142 | if ('' === $outCharset) { |
||
| 143 | $outCharset = 'iso-8859-1'; |
||
| 144 | } |
||
| 145 | if ('' === $inCharset) { |
||
| 146 | $inCharset = 'iso-8859-1'; |
||
| 147 | } |
||
| 148 | |||
| 149 | do { |
||
| 150 | $loop = false; |
||
| 151 | |||
| 152 | if ('//translit' === substr($outCharset, -10)) { |
||
| 153 | $loop = $translit = true; |
||
| 154 | $outCharset = substr($outCharset, 0, -10); |
||
| 155 | } |
||
| 156 | |||
| 157 | if ('//ignore' === substr($outCharset, -8)) { |
||
| 158 | $loop = $ignore = true; |
||
| 159 | $outCharset = substr($outCharset, 0, -8); |
||
| 160 | } |
||
| 161 | } while ($loop); |
||
| 162 | |||
| 163 | do { |
||
| 164 | $loop = false; |
||
| 165 | |||
| 166 | if ('//translit' === substr($inCharset, -10)) { |
||
| 167 | $loop = true; |
||
| 168 | $inCharset = substr($inCharset, 0, -10); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ('//ignore' === substr($inCharset, -8)) { |
||
| 172 | $loop = true; |
||
| 173 | $inCharset = substr($inCharset, 0, -8); |
||
| 174 | } |
||
| 175 | } while ($loop); |
||
| 176 | |||
| 177 | if (isset(self::$alias[$inCharset])) { |
||
| 178 | $inCharset = self::$alias[$inCharset]; |
||
| 179 | } |
||
| 180 | if (isset(self::$alias[$outCharset])) { |
||
| 181 | $outCharset = self::$alias[$outCharset]; |
||
| 182 | } |
||
| 183 | |||
| 184 | // Load charset maps |
||
| 185 | |||
| 186 | if (('utf-8' !== $inCharset && !self::loadMap('from.', $inCharset, $inMap)) |
||
| 187 | || ('utf-8' !== $outCharset && !self::loadMap('to.', $outCharset, $outMap))) { |
||
| 188 | trigger_error(sprintf(self::ERROR_WRONG_CHARSET, $inCharset, $outCharset)); |
||
| 189 | |||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | if ('utf-8' !== $inCharset) { |
||
| 194 | // Convert input to UTF-8 |
||
| 195 | $result = ''; |
||
| 196 | if (self::mapToUtf8($result, $inMap, $str, $ignore)) { |
||
| 197 | $str = $result; |
||
| 198 | } else { |
||
| 199 | $str = false; |
||
| 200 | } |
||
| 201 | self::$isValidUtf8 = true; |
||
| 202 | } else { |
||
| 203 | self::$isValidUtf8 = preg_match('//u', $str); |
||
| 204 | |||
| 205 | if (!self::$isValidUtf8 && !$ignore) { |
||
| 206 | trigger_error(self::ERROR_ILLEGAL_CHARACTER); |
||
| 207 | |||
| 208 | return false; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ('utf-8' === $outCharset) { |
||
| 212 | // UTF-8 validation |
||
| 213 | $str = self::utf8ToUtf8($str, $ignore); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | if ('utf-8' !== $outCharset && false !== $str) { |
||
| 218 | // Convert output to UTF-8 |
||
| 219 | $result = ''; |
||
| 220 | if (self::mapFromUtf8($result, $outMap, $str, $ignore, $translit)) { |
||
| 221 | return $result; |
||
| 222 | } |
||
| 223 | |||
| 224 | return false; |
||
| 225 | } |
||
| 226 | |||
| 227 | return $str; |
||
| 228 | } |
||
| 229 | |||
| 230 | public static function iconv_mime_decode_headers($str, $mode = 0, $charset = null) |
||
| 231 | { |
||
| 232 | if (null === $charset) { |
||
| 233 | $charset = self::$internalEncoding; |
||
| 234 | } |
||
| 235 | |||
| 236 | if (false !== strpos($str, "\r")) { |
||
| 237 | $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); |
||
| 238 | } |
||
| 239 | $str = explode("\n\n", $str, 2); |
||
| 240 | |||
| 241 | $headers = []; |
||
| 242 | |||
| 243 | $str = preg_split('/\n(?![ \t])/', $str[0]); |
||
| 244 | foreach ($str as $str) { |
||
| 245 | $str = self::iconv_mime_decode($str, $mode, $charset); |
||
| 246 | if (false === $str) { |
||
| 247 | return false; |
||
| 248 | } |
||
| 249 | $str = explode(':', $str, 2); |
||
| 250 | |||
| 251 | if (2 === \count($str)) { |
||
| 252 | if (isset($headers[$str[0]])) { |
||
| 253 | if (!\is_array($headers[$str[0]])) { |
||
| 254 | $headers[$str[0]] = [$headers[$str[0]]]; |
||
| 255 | } |
||
| 256 | $headers[$str[0]][] = ltrim($str[1]); |
||
| 257 | } else { |
||
| 258 | $headers[$str[0]] = ltrim($str[1]); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | return $headers; |
||
| 264 | } |
||
| 265 | |||
| 266 | public static function iconv_mime_decode($str, $mode = 0, $charset = null) |
||
| 267 | { |
||
| 268 | if (null === $charset) { |
||
| 269 | $charset = self::$internalEncoding; |
||
| 270 | } |
||
| 271 | if (\ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) { |
||
| 272 | $charset .= '//IGNORE'; |
||
| 273 | } |
||
| 274 | |||
| 275 | if (false !== strpos($str, "\r")) { |
||
| 276 | $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n"); |
||
| 277 | } |
||
| 278 | $str = preg_split('/\n(?![ \t])/', rtrim($str), 2); |
||
| 279 | $str = preg_replace('/[ \t]*\n[ \t]+/', ' ', rtrim($str[0])); |
||
| 280 | $str = preg_split('/=\?([^?]+)\?([bqBQ])\?(.*?)\?=/', $str, -1, \PREG_SPLIT_DELIM_CAPTURE); |
||
| 281 | |||
| 282 | $result = self::iconv('utf-8', $charset, $str[0]); |
||
| 283 | if (false === $result) { |
||
| 284 | return false; |
||
| 285 | } |
||
| 286 | |||
| 287 | $i = 1; |
||
| 288 | $len = \count($str); |
||
| 289 | |||
| 290 | while ($i < $len) { |
||
| 291 | $c = strtolower($str[$i]); |
||
| 292 | if ((\ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) |
||
| 293 | && 'utf-8' !== $c |
||
| 294 | && !isset(self::$alias[$c]) |
||
| 295 | && !self::loadMap('from.', $c, $d)) { |
||
| 296 | $d = false; |
||
| 297 | } elseif ('B' === strtoupper($str[$i + 1])) { |
||
| 298 | $d = base64_decode($str[$i + 2]); |
||
| 299 | } else { |
||
| 300 | $d = rawurldecode(strtr(str_replace('%', '%25', $str[$i + 2]), '=_', '% ')); |
||
| 301 | } |
||
| 302 | |||
| 303 | if (false !== $d) { |
||
| 304 | if ('' !== $d) { |
||
| 305 | if ('' === $d = self::iconv($c, $charset, $d)) { |
||
| 306 | $str[$i + 3] = substr($str[$i + 3], 1); |
||
| 307 | } else { |
||
| 308 | $result .= $d; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | $d = self::iconv('utf-8', $charset, $str[$i + 3]); |
||
| 312 | if ('' !== trim($d)) { |
||
| 313 | $result .= $d; |
||
| 314 | } |
||
| 315 | } elseif (\ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) { |
||
| 316 | $result .= "=?{$str[$i]}?{$str[$i + 1]}?{$str[$i + 2]}?={$str[$i + 3]}"; |
||
| 317 | } else { |
||
| 318 | $result = false; |
||
| 319 | break; |
||
| 320 | } |
||
| 321 | |||
| 322 | $i += 4; |
||
| 323 | } |
||
| 324 | |||
| 325 | return $result; |
||
| 326 | } |
||
| 327 | |||
| 328 | public static function iconv_get_encoding($type = 'all') |
||
| 329 | { |
||
| 330 | switch ($type) { |
||
| 331 | case 'input_encoding': return self::$inputEncoding; |
||
| 332 | case 'output_encoding': return self::$outputEncoding; |
||
| 333 | case 'internal_encoding': return self::$internalEncoding; |
||
| 334 | } |
||
| 335 | |||
| 336 | return [ |
||
| 337 | 'input_encoding' => self::$inputEncoding, |
||
| 338 | 'output_encoding' => self::$outputEncoding, |
||
| 339 | 'internal_encoding' => self::$internalEncoding, |
||
| 340 | ]; |
||
| 341 | } |
||
| 342 | |||
| 343 | public static function iconv_set_encoding($type, $charset) |
||
| 353 | } |
||
| 354 | |||
| 355 | public static function iconv_mime_encode($fieldName, $fieldValue, $pref = null) |
||
| 356 | { |
||
| 357 | if (!\is_array($pref)) { |
||
| 429 | } |
||
| 430 | |||
| 431 | public static function iconv_strlen($s, $encoding = null) |
||
| 453 | } |
||
| 454 | |||
| 455 | public static function iconv_strpos($haystack, $needle, $offset = 0, $encoding = null) |
||
| 456 | { |
||
| 457 | if (null === $encoding) { |
||
| 458 | $encoding = self::$internalEncoding; |
||
| 459 | } |
||
| 460 | |||
| 461 | if (0 !== stripos($encoding, 'utf-8')) { |
||
| 462 | if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) { |
||
| 463 | return false; |
||
| 464 | } |
||
| 465 | if (false === $needle = self::iconv($encoding, 'utf-8', $needle)) { |
||
| 466 | return false; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | if ($offset = (int) $offset) { |
||
| 471 | $haystack = self::iconv_substr($haystack, $offset, 2147483647, 'utf-8'); |
||
| 472 | } |
||
| 473 | $pos = strpos($haystack, $needle); |
||
| 474 | |||
| 475 | return false === $pos ? false : ($offset + ($pos ? self::iconv_strlen(substr($haystack, 0, $pos), 'utf-8') : 0)); |
||
| 476 | } |
||
| 477 | |||
| 478 | public static function iconv_strrpos($haystack, $needle, $encoding = null) |
||
| 479 | { |
||
| 480 | if (null === $encoding) { |
||
| 481 | $encoding = self::$internalEncoding; |
||
| 482 | } |
||
| 483 | |||
| 484 | if (0 !== stripos($encoding, 'utf-8')) { |
||
| 485 | if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) { |
||
| 486 | return false; |
||
| 487 | } |
||
| 488 | if (false === $needle = self::iconv($encoding, 'utf-8', $needle)) { |
||
| 489 | return false; |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | $pos = isset($needle[0]) ? strrpos($haystack, $needle) : false; |
||
| 494 | |||
| 495 | return false === $pos ? false : self::iconv_strlen($pos ? substr($haystack, 0, $pos) : $haystack, 'utf-8'); |
||
| 496 | } |
||
| 497 | |||
| 498 | public static function iconv_substr($s, $start, $length = 2147483647, $encoding = null) |
||
| 499 | { |
||
| 500 | if (null === $encoding) { |
||
| 501 | $encoding = self::$internalEncoding; |
||
| 502 | } |
||
| 503 | if (0 !== stripos($encoding, 'utf-8')) { |
||
| 504 | $encoding = null; |
||
| 505 | } elseif (false === $s = self::iconv($encoding, 'utf-8', $s)) { |
||
| 506 | return false; |
||
| 507 | } |
||
| 508 | |||
| 509 | $s = (string) $s; |
||
| 510 | $slen = self::iconv_strlen($s, 'utf-8'); |
||
| 511 | $start = (int) $start; |
||
| 512 | |||
| 513 | if (0 > $start) { |
||
| 514 | $start += $slen; |
||
| 515 | } |
||
| 516 | if (0 > $start) { |
||
| 517 | if (\PHP_VERSION_ID < 80000) { |
||
| 518 | return false; |
||
| 519 | } |
||
| 520 | |||
| 521 | $start = 0; |
||
| 522 | } |
||
| 523 | if ($start >= $slen) { |
||
| 524 | return \PHP_VERSION_ID >= 80000 ? '' : false; |
||
| 525 | } |
||
| 526 | |||
| 527 | $rx = $slen - $start; |
||
| 528 | |||
| 529 | if (0 > $length) { |
||
| 530 | $length += $rx; |
||
| 531 | } |
||
| 532 | if (0 === $length) { |
||
| 533 | return ''; |
||
| 534 | } |
||
| 535 | if (0 > $length) { |
||
| 536 | return \PHP_VERSION_ID >= 80000 ? '' : false; |
||
| 537 | } |
||
| 538 | |||
| 539 | if ($length > $rx) { |
||
| 540 | $length = $rx; |
||
| 541 | } |
||
| 542 | |||
| 543 | $rx = '/^'.($start ? self::pregOffset($start) : '').'('.self::pregOffset($length).')/u'; |
||
| 544 | |||
| 545 | $s = preg_match($rx, $s, $s) ? $s[1] : ''; |
||
| 546 | |||
| 547 | if (null === $encoding) { |
||
| 548 | return $s; |
||
| 549 | } |
||
| 550 | |||
| 551 | return self::iconv('utf-8', $encoding, $s); |
||
| 552 | } |
||
| 553 | |||
| 554 | private static function loadMap($type, $charset, &$map) |
||
| 555 | { |
||
| 556 | if (!isset(self::$convertMap[$type.$charset])) { |
||
| 557 | if (false === $map = self::getData($type.$charset)) { |
||
| 558 | if ('to.' === $type && self::loadMap('from.', $charset, $map)) { |
||
| 559 | $map = array_flip($map); |
||
| 560 | } else { |
||
| 561 | return false; |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | self::$convertMap[$type.$charset] = $map; |
||
| 566 | } else { |
||
| 567 | $map = self::$convertMap[$type.$charset]; |
||
| 568 | } |
||
| 569 | |||
| 570 | return true; |
||
| 571 | } |
||
| 572 | |||
| 573 | private static function utf8ToUtf8($str, $ignore) |
||
| 574 | { |
||
| 575 | $ulenMask = self::$ulenMask; |
||
| 576 | $valid = self::$isValidUtf8; |
||
| 577 | |||
| 578 | $u = $str; |
||
| 579 | $i = $j = 0; |
||
| 580 | $len = \strlen($str); |
||
| 581 | |||
| 582 | while ($i < $len) { |
||
| 583 | if ($str[$i] < "\x80") { |
||
| 584 | $u[$j++] = $str[$i++]; |
||
| 585 | } else { |
||
| 586 | $ulen = $str[$i] & "\xF0"; |
||
| 587 | $ulen = $ulenMask[$ulen] ?? 1; |
||
| 588 | $uchr = substr($str, $i, $ulen); |
||
| 589 | |||
| 590 | if (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr))) { |
||
| 591 | if ($ignore) { |
||
| 592 | ++$i; |
||
| 593 | continue; |
||
| 594 | } |
||
| 595 | |||
| 596 | trigger_error(self::ERROR_ILLEGAL_CHARACTER); |
||
| 597 | |||
| 598 | return false; |
||
| 599 | } |
||
| 600 | |||
| 601 | $i += $ulen; |
||
| 602 | |||
| 603 | $u[$j++] = $uchr[0]; |
||
| 604 | |||
| 605 | isset($uchr[1]) && 0 !== ($u[$j++] = $uchr[1]) |
||
| 606 | && isset($uchr[2]) && 0 !== ($u[$j++] = $uchr[2]) |
||
| 607 | && isset($uchr[3]) && 0 !== ($u[$j++] = $uchr[3]); |
||
| 608 | } |
||
| 609 | } |
||
| 610 | |||
| 611 | return substr($u, 0, $j); |
||
| 612 | } |
||
| 613 | |||
| 614 | private static function mapToUtf8(&$result, array $map, $str, $ignore) |
||
| 615 | { |
||
| 616 | $len = \strlen($str); |
||
| 617 | for ($i = 0; $i < $len; ++$i) { |
||
| 618 | if (isset($str[$i + 1], $map[$str[$i].$str[$i + 1]])) { |
||
| 619 | $result .= $map[$str[$i].$str[++$i]]; |
||
| 620 | } elseif (isset($map[$str[$i]])) { |
||
| 621 | $result .= $map[$str[$i]]; |
||
| 622 | } elseif (!$ignore) { |
||
| 623 | trigger_error(self::ERROR_ILLEGAL_CHARACTER); |
||
| 624 | |||
| 625 | return false; |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | return true; |
||
| 630 | } |
||
| 631 | |||
| 632 | private static function mapFromUtf8(&$result, array $map, $str, $ignore, $translit) |
||
| 633 | { |
||
| 634 | $ulenMask = self::$ulenMask; |
||
| 635 | $valid = self::$isValidUtf8; |
||
| 636 | |||
| 637 | if ($translit && !self::$translitMap) { |
||
| 638 | self::$translitMap = self::getData('translit'); |
||
| 639 | } |
||
| 640 | |||
| 641 | $i = 0; |
||
| 642 | $len = \strlen($str); |
||
| 643 | |||
| 644 | while ($i < $len) { |
||
| 645 | if ($str[$i] < "\x80") { |
||
| 646 | $uchr = $str[$i++]; |
||
| 647 | } else { |
||
| 648 | $ulen = $str[$i] & "\xF0"; |
||
| 649 | $ulen = $ulenMask[$ulen] ?? 1; |
||
| 650 | $uchr = substr($str, $i, $ulen); |
||
| 651 | |||
| 652 | if ($ignore && (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr)))) { |
||
| 653 | ++$i; |
||
| 654 | continue; |
||
| 655 | } |
||
| 656 | |||
| 657 | $i += $ulen; |
||
| 658 | } |
||
| 659 | |||
| 660 | if (isset($map[$uchr])) { |
||
| 661 | $result .= $map[$uchr]; |
||
| 662 | } elseif ($translit) { |
||
| 663 | if (isset(self::$translitMap[$uchr])) { |
||
| 664 | $uchr = self::$translitMap[$uchr]; |
||
| 665 | } elseif ($uchr >= "\xC3\x80") { |
||
| 666 | $uchr = \Normalizer::normalize($uchr, \Normalizer::NFD); |
||
| 667 | |||
| 668 | if ($uchr[0] < "\x80") { |
||
| 669 | $uchr = $uchr[0]; |
||
| 670 | } elseif ($ignore) { |
||
| 671 | continue; |
||
| 672 | } else { |
||
| 673 | return false; |
||
| 674 | } |
||
| 675 | } elseif ($ignore) { |
||
| 676 | continue; |
||
| 677 | } else { |
||
| 678 | return false; |
||
| 679 | } |
||
| 680 | |||
| 681 | $str = $uchr.substr($str, $i); |
||
| 682 | $len = \strlen($str); |
||
| 683 | $i = 0; |
||
| 684 | } elseif (!$ignore) { |
||
| 685 | return false; |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | return true; |
||
| 690 | } |
||
| 691 | |||
| 692 | private static function qpByteCallback(array $m) |
||
| 693 | { |
||
| 694 | return '='.strtoupper(dechex(\ord($m[0]))); |
||
| 695 | } |
||
| 696 | |||
| 697 | private static function pregOffset($offset) |
||
| 708 | } |
||
| 709 | |||
| 710 | private static function getData($file) |
||
| 717 | } |
||
| 718 | } |
||
| 719 |