RussianToEnglish::transliterate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the Transliteration library
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\Transliteration;
14
15
/**
16
 * Transliteration from Russian to English.
17
 *
18
 * @author Artem Henvald <[email protected]>
19
 * @author Mykhailo Vilshansky <[email protected]>
20
 * @author Yevgeniy Zholkevskiy <[email protected]>
21
 *
22
 * @see http://www.ufms.spb.ru/desc/pravila-transliteracii-dind-1009.html
23
 */
24
class RussianToEnglish implements TransliteratorInterface
25
{
26
    /** @const string[] */
27
    private const RUSSIAN_TO_ENGLISH_RULES = [
28
        'а' => 'a',
29
        'б' => 'b',
30
        'в' => 'v',
31
        'г' => 'g',
32
        'д' => 'd',
33
        'е' => 'e',
34
        'ё' => 'e',
35
        'ж' => 'zh',
36
        'з' => 'z',
37
        'и' => 'i',
38
        'й' => 'y',
39
        'к' => 'k',
40
        'л' => 'l',
41
        'м' => 'm',
42
        'н' => 'n',
43
        'о' => 'o',
44
        'п' => 'p',
45
        'р' => 'r',
46
        'с' => 's',
47
        'т' => 't',
48
        'у' => 'u',
49
        'ф' => 'f',
50
        'х' => 'h',
51
        'ц' => 'ts',
52
        'ч' => 'ch',
53
        'ш' => 'sh',
54
        'щ' => 'sht',
55
        'ь' => '',
56
        'ы' => 'y',
57
        'ъ' => '',
58
        'э' => 'e',
59
        'ю' => 'yu',
60
        'я' => 'ya',
61
        'А' => 'A',
62
        'Б' => 'B',
63
        'В' => 'V',
64
        'Г' => 'G',
65
        'Д' => 'D',
66
        'Е' => 'E',
67
        'Ж' => 'Zh',
68
        'З' => 'Z',
69
        'И' => 'I',
70
        'Й' => 'Y',
71
        'К' => 'K',
72
        'Л' => 'L',
73
        'М' => 'M',
74
        'Н' => 'N',
75
        'О' => 'O',
76
        'П' => 'P',
77
        'Р' => 'R',
78
        'С' => 'S',
79
        'Т' => 'T',
80
        'У' => 'U',
81
        'Ф' => 'F',
82
        'Х' => 'H',
83
        'Ц' => 'Ts',
84
        'Ч' => 'Ch',
85
        'Ш' => 'Sh',
86
        'Щ' => 'Sht',
87
        'Ь' => '',
88
        'Ы' => 'Y',
89
        'Ъ' => '',
90
        'Э' => 'E',
91
        'Ю' => 'Yu',
92
        'Я' => 'Ya',
93
        "'" => '',
94
    ];
95
96
    /**
97
     * @param string $russianText
98
     *
99
     * @return string
100
     */
101
    public static function transliterate(string $russianText): string
102
    {
103
        $transliteratedText = '';
104
105
        if ('' !== $russianText) {
106
            $transliteratedText = \str_replace(
107
                \array_keys(self::RUSSIAN_TO_ENGLISH_RULES),
108
                \array_values(self::RUSSIAN_TO_ENGLISH_RULES),
109
                $russianText
110
            );
111
        }
112
113
        return $transliteratedText;
114
    }
115
}
116