UkrainianToEnglish   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 106
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transliterate() 0 17 3
A checkForZghException() 0 4 2
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 Ukrainian to English.
17
 *
18
 * According to the rules of transliteration, that are described in the resolution
19
 * of the Cabinet of Ministers of Ukraine №55 dated January 27, 2010.
20
 *
21
 * @author Artem Henvald <[email protected]>
22
 *
23
 * @see http://zakon1.rada.gov.ua/laws/show/55-2010-%D0%BF
24
 */
25
class UkrainianToEnglish implements TransliteratorInterface
26
{
27
    /** @const string[] */
28
    private const UKRAINIAN_TO_ENGLISH_RULES = [
29
        'А' => 'A',
30
        'Б' => 'B',
31
        'В' => 'V',
32
        'Г' => 'H',
33
        'Ґ' => 'G',
34
        'Д' => 'D',
35
        'Е' => 'E',
36
        'Є' => 'Ye',
37
        'Ж' => 'Zh',
38
        'З' => 'Z',
39
        'И' => 'Y',
40
        'І' => 'I',
41
        'Ї' => 'Yi',
42
        'Й' => 'Y',
43
        'К' => 'K',
44
        'Л' => 'L',
45
        'М' => 'M',
46
        'Н' => 'N',
47
        'О' => 'O',
48
        'П' => 'P',
49
        'Р' => 'R',
50
        'С' => 'S',
51
        'Т' => 'T',
52
        'У' => 'U',
53
        'Ф' => 'F',
54
        'Х' => 'Kh',
55
        'Ц' => 'Ts',
56
        'Ч' => 'Ch',
57
        'Ш' => 'Sh',
58
        'Щ' => 'Shch',
59
        'Ь' => '',
60
        'Ю' => 'Yu',
61
        'Я' => 'Ya',
62
        'а' => 'a',
63
        'б' => 'b',
64
        'в' => 'v',
65
        'г' => 'h',
66
        'ґ' => 'g',
67
        'д' => 'd',
68
        'е' => 'e',
69
        'є' => 'ie',
70
        'ж' => 'zh',
71
        'з' => 'z',
72
        'и' => 'y',
73
        'і' => 'i',
74
        'ї' => 'i',
75
        'й' => 'i',
76
        'к' => 'k',
77
        'л' => 'l',
78
        'м' => 'm',
79
        'н' => 'n',
80
        'о' => 'o',
81
        'п' => 'p',
82
        'р' => 'r',
83
        'с' => 's',
84
        'т' => 't',
85
        'у' => 'u',
86
        'ф' => 'f',
87
        'х' => 'kh',
88
        'ц' => 'ts',
89
        'ч' => 'ch',
90
        'ш' => 'sh',
91
        'щ' => 'shch',
92
        'ь' => '',
93
        'ю' => 'iu',
94
        'я' => 'ia',
95
        '\'' => '',
96
    ];
97
98
    /**
99
     * @param string $ukrainianText
100
     *
101
     * @return string
102
     */
103
    public static function transliterate(string $ukrainianText): string
104
    {
105
        $transliteratedText = '';
106
107
        if ('' !== $ukrainianText) {
108
            if (self::checkForZghException($ukrainianText)) {
109
                $ukrainianText = \str_replace(['Зг', 'зг'], ['Zgh', 'zgh'], $ukrainianText);
110
            }
111
            $transliteratedText = \str_replace(
112
                \array_keys(self::UKRAINIAN_TO_ENGLISH_RULES),
113
                \array_values(self::UKRAINIAN_TO_ENGLISH_RULES),
114
                $ukrainianText
115
            );
116
        }
117
118
        return $transliteratedText;
119
    }
120
121
    /**
122
     * @param string $ukrainianText
123
     *
124
     * @return bool
125
     */
126
    private static function checkForZghException(string $ukrainianText): bool
127
    {
128
        return (bool) \mb_substr_count($ukrainianText, 'Зг') || (bool) \mb_substr_count($ukrainianText, 'зг');
129
    }
130
}
131