silverstripe-framework/src/View/Parsers/Transliterator.php

99 lines
4.5 KiB
PHP
Raw Permalink Normal View History

<?php
namespace SilverStripe\View\Parsers;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
/**
* Support class for converting unicode strings into a suitable 7-bit ASCII equivalent.
2014-08-15 08:53:05 +02:00
*
* Usage:
2014-08-15 08:53:05 +02:00
*
* <code>
2016-09-09 08:43:05 +02:00
* $tr = new Transliterator();
* $ascii = $tr->toASCII($unicode);
* </code>
*/
class Transliterator
2016-11-29 00:31:16 +01:00
{
use Injectable;
use Configurable;
2016-11-29 00:31:16 +01:00
/**
* @config
* @var boolean Allow the use of iconv() to perform transliteration. Set to false to disable.
* Even if this variable is true, iconv() won't be used if it's not installed.
*/
private static $use_iconv = false;
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Convert the given utf8 string to a safe ASCII source
*
* @param string $source
* @return string
*/
public function toASCII($source)
{
if (function_exists('iconv') && $this->config()->use_iconv) {
return $this->useIconv($source);
} else {
return $this->useStrTr($source);
}
}
2016-11-29 00:31:16 +01:00
/**
* Transliteration using strtr() and a lookup table
*
* @param string $source
* @return string
*/
protected function useStrTr($source)
{
$table = [
2016-11-29 00:31:16 +01:00
'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'Ae', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'Oe', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'Ue', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'ae', 'å'=>'a', 'æ'=>'ae', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'oe', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'ue', 'ý'=>'y',
2016-11-29 00:31:16 +01:00
'þ'=>'b', 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
'Ā'=>'A', 'ā'=>'a', 'Ē'=>'E', 'ē'=>'e', 'Ī'=>'I', 'ī'=>'i', 'Ō'=>'O', 'ō'=>'o', 'Ū'=>'U', 'ū'=>'u',
'œ'=>'oe', 'ij'=>'ij', 'ą'=>'a','ę'=>'e', 'ė'=>'e', 'į'=>'i', 'ų'=>'u', 'Ą'=>'A',
'Ę'=>'E', 'Ė'=>'E', 'Į'=>'I','Ų'=>'U',
2016-11-29 00:31:16 +01:00
"ľ"=>"l", "Ľ"=>"L", "ť"=>"t", "Ť"=>"T", "ů"=>"u", "Ů"=>"U",
'ł'=>'l', 'Ł'=>'L', 'ń'=>'n', 'Ń'=>'N', 'ś'=>'s', 'Ś'=>'S', 'ź'=>'z', 'Ź'=>'Z', 'ż'=>'z', 'Ż'=>'Z',
2017-02-20 22:57:20 +01:00
'а'=>"a",'б'=>"b",'в'=>"v",'г'=>"g",'д'=>"d",'е'=>"e",'ё'=>"yo",'ж'=>"zh",'з'=>"z",'и'=>"i",
'й'=>"y",'к'=>"k",'л'=>"l",'м'=>"m",'н'=>"n",'о'=>"o",'п'=>"p",'р'=>"r",'с'=>"s",'т'=>"t",
'у'=>"u",'ф'=>"f",'х'=>"kh",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'=>'f', 'Φ'=>'F', 'χ'=>'ch', 'Χ'=>'CH', 'ψ'=>'ps', 'Ψ'=>'PS', 'ξ'=>'x', 'Ξ'=>'X',
'ω'=>'w', 'Ω'=>'W', 'ώ'=>'o', 'Ώ'=>'O', 'ὠ'=>'o', 'Ὠ'=>'O',
];
2022-04-14 03:12:59 +02:00
return strtr($source ?? '', $table ?? '');
2016-11-29 00:31:16 +01:00
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Transliteration using iconv()
*
* @param string $source
* @return string
*/
protected function useIconv($source)
{
2022-04-14 03:12:59 +02:00
return iconv("utf-8", "us-ascii//IGNORE//TRANSLIT", $source ?? '');
2016-11-29 00:31:16 +01:00
}
}