00001 <?php
00002
00003
00004
00005
00006
00007
00008 class ConverterUnidecode implements IConverter {
00009 private static $groups = array();
00010
00011
00012
00013
00014
00015
00016
00017 public function encode($value, $params = false) {
00018
00019 if (empty($params)) {
00020 $params = GyroLocale::get_charset();
00021 }
00022 $value = String::convert($value, $params, 'UTF-16');
00023 $value = $this->unidecode($value);
00024
00025 return $value;
00026 }
00027
00028
00029
00030
00031 public function decode($value, $params = false) {
00032 return $this->encode($value, $params);
00033 }
00034
00035
00036
00037
00038 protected function unidecode($value) {
00039 $ret = '';
00040 foreach(unpack('n*', $value) as $uchar) {
00041 $ret .= $this->unidecode_uchar($uchar);
00042 }
00043 return $ret;
00044 }
00045
00046
00047
00048
00049 protected function unidecode_uchar($uchar) {
00050 if ($uchar <= 0x007f) {
00051 return chr($uchar);
00052 }
00053
00054 $high = $uchar >> 8;
00055 $low = $uchar & 0x00ff;
00056
00057
00058 $group = Arr::get_item(self::$groups, $high, false);
00059 if ($group === false) {
00060
00061 $hex = substr('00' . dechex($high), -2);
00062 $file = dirname(__FILE__) . '/data/x' . $hex . '.php';
00063 if (file_exists($file)) {
00064 include($file);
00065 $group = $data;
00066 }
00067 else {
00068
00069 $group = array_fill(0, 0x100, '');
00070 }
00071 self::$groups[$high] = $group;
00072 }
00073
00074 return Arr::get_item($group, $low, '');
00075 }
00076
00077 }
00078