Completed
Push — master ( e03b80...7156da )
by Daniel
09:23
created

Transliterator::useStrTr()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 32
nc 1
nop 1
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\View\Parsers;
4
5
use SilverStripe\Core\Object;
6
7
/**
8
 * Support class for converting unicode strings into a suitable 7-bit ASCII equivalent.
9
 *
10
 * Usage:
11
 *
12
 * <code>
13
 * $tr = new Transliterator();
14
 * $ascii = $tr->toASCII($unicode);
15
 * </code>
16
 */
17
class Transliterator extends Object
18
{
19
    /**
20
     * @config
21
     * @var boolean Allow the use of iconv() to perform transliteration.  Set to false to disable.
22
     * Even if this variable is true, iconv() won't be used if it's not installed.
23
     */
24
    private static $use_iconv = false;
25
26
    /**
27
     * Convert the given utf8 string to a safe ASCII source
28
     *
29
     * @param string $source
30
     * @return string
31
     */
32
    public function toASCII($source)
33
    {
34
        if (function_exists('iconv') && $this->config()->use_iconv) {
35
            return $this->useIconv($source);
36
        } else {
37
            return $this->useStrTr($source);
38
        }
39
    }
40
41
    /**
42
     * Transliteration using strtr() and a lookup table
43
     *
44
     * @param string $source
45
     * @return string
46
     */
47
    protected function useStrTr($source)
48
    {
49
        $table = array(
50
            'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
51
            'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'Ae', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
52
            'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
53
            'Õ'=>'O', 'Ö'=>'Oe', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'Ue', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'ss',
54
            'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'ae', 'å'=>'a', 'æ'=>'ae', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
55
            'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
56
            'ô'=>'o', 'õ'=>'o', 'ö'=>'oe', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'ue', 'ý'=>'y', 'ý'=>'y',
57
            'þ'=>'b', 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
58
            'Ā'=>'A', 'ā'=>'a', 'Ē'=>'E', 'ē'=>'e', 'Ī'=>'I', 'ī'=>'i', 'Ō'=>'O', 'ō'=>'o', 'Ū'=>'U', 'ū'=>'u',
59
            'œ'=>'oe', 'ß'=>'ss', 'ij'=>'ij', 'ą'=>'a','ę'=>'e', 'ė'=>'e', 'į'=>'i','ų'=>'u','ū'=>'u', 'Ą'=>'A',
60
            'Ę'=>'E', 'Ė'=>'E', 'Į'=>'I','Ų'=>'U','Ū'=>'U',
61
            "ľ"=>"l", "Ľ"=>"L", "ť"=>"t", "Ť"=>"T", "ů"=>"u", "Ů"=>"U",
62
            'ł'=>'l', 'Ł'=>'L', 'ń'=>'n', 'Ń'=>'N', 'ś'=>'s', 'Ś'=>'S', 'ź'=>'z', 'Ź'=>'Z', 'ż'=>'z', 'Ż'=>'Z',
63
            'а'=>"a",'б'=>"b",'в'=>"v",'г'=>"g",'д'=>"d",'е'=>"e",'ё'=>"yo",'ж'=>"zh",'з'=>"z",'и'=>"i",
64
            'й'=>"y",'к'=>"k",'л'=>"l",'м'=>"m",'н'=>"n",'о'=>"o",'п'=>"p",'р'=>"r",'с'=>"s",'т'=>"t",
65
            'у'=>"u",'ф'=>"f",'х'=>"kh",'ц'=>"ts",'ч'=>"ch",'ш'=>"sh",'щ'=>"shch",'ы'=>"y",'э'=>"e",'ю'=>"yu",
66
            'я'=>"ya",
67
            'А'=>"A",'Б'=>"B",'В'=>"V",'Г'=>"G",'Д'=>"D",'Е'=>"E",'Ё'=>"YO",'Ж'=>"ZH",'З'=>"Z",'И'=>"I",
68
            'Й'=>"Y",'К'=>"K",'Л'=>"L",'М'=>"M",'Н'=>"N",'О'=>"O",'П'=>"P",'Р'=>"R",'С'=>"S",'Т'=>"T",
69
            'У'=>"U",'Ф'=>"F",'Х'=>"KH",'Ц'=>"TS",'Ч'=>"CH",'Ш'=>"SH",'Щ'=>"SHCH",'Ы'=>"Y",'Э'=>"E",'Ю'=>"YU",
70
            'Я'=>"YA",
71
            'α'=>'a', 'Α'=>'A', 'ά'=>'a', 'Ά'=>'A', 'β'=>'v', 'Β'=>'V', 'γ'=>'g', 'Γ'=>'G', 'δ'=>'d', 'Δ'=>'D',
72
            'ε'=>'e', 'ϵ'=>'e', 'Ε'=>'E', 'έ'=>'e', 'Έ'=>'E', 'ζ'=>'z', 'Ζ'=>'Z', 'η'=>'i', 'Η'=>'I',
73
            'θ'=>'th', 'ϑ' => 'th', 'Θ'=>'TH', 'ι'=>'i', 'Ι'=>'I', 'ί'=>'i', 'Ί'=>'I', 'κ'=>'k', 'ϰ'=>'k', 'Κ'=>'K',
74
            'λ'=>'l', 'Λ'=>'L', 'μ'=>'m', 'Μ'=>'M', 'ν'=>'n', 'Ν'=>'N', 'ή'=>'n', 'Ή'=>'N', 'ἠ'=>'n', 'Ἠ'=>'N',
75
            'ο'=>'o', 'Ο'=>'O', 'ό'=>'o', 'Ό'=>'O', 'π'=>'p', 'Π'=>'P', 'ρ'=>'r', 'ϱ'=>'r', 'Ρ'=>'R', 'ῤ'=>'rh',
76
            'σ'=>'s', 'ς'=>'s', 'Σ'=>'S', 'τ'=>'t', 'Τ'=>'T', 'υ'=>'y', 'Υ'=>'Y', 'ύ'=>'y', 'Ύ'=>'Y', 'ὐ'=>'y',
77
            'φ'=>'f', 'ϕ'=>'f', 'Φ'=>'F', 'χ'=>'ch', 'Χ'=>'CH', 'ψ'=>'ps', 'Ψ'=>'PS', 'ξ'=>'x', 'Ξ'=>'X',
78
            'ω'=>'w', 'Ω'=>'W', 'ώ'=>'o', 'Ώ'=>'O', 'ὠ'=>'o', 'Ὠ'=>'O',
79
        );
80
81
        return strtr($source, $table);
82
    }
83
84
    /**
85
     * Transliteration using iconv()
86
     *
87
     * @param string $source
88
     * @return string
89
     */
90
    protected function useIconv($source)
91
    {
92
        return iconv("utf-8", "us-ascii//IGNORE//TRANSLIT", $source);
93
    }
94
}
95