Passed
Push — master ( b977eb...5bdf76 )
by Dāvis
02:58
created

Transliterator::translit2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
class Transliterator
6
{
7
    // @formatter:off
8
    /**
9
     * Cyrillic mapping.
10
     *
11
     * @var array
12
     */
13
    protected static $cyrMap = array(
14
        'е', 'ё', 'ж', 'х', 'ц', 'ч', 'ш', 'щ', 'ю', 'я',
15
        'Е', 'Ё', 'Ж', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ю', 'Я',
16
        'а', 'б', 'в', 'г', 'д', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'ъ', 'ы', 'ь', 'э',
17
        'А', 'Б', 'В', 'Г', 'Д', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Ъ', 'Ы', 'Ь', 'Э'
18
    );
19
20
    /**
21
     * Latin mapping.
22
     *
23
     * @var array
24
     */
25
    protected static $latMap = array(
26
        'ye', 'ye', 'zh', 'kh', 'ts', 'ch', 'sh', 'shch', 'yu', 'ya',
27
        'Ye', 'Ye', 'Zh', 'Kh', 'Ts', 'Ch', 'Sh', 'Shch', 'Yu', 'Ya',
28
        'a', 'b', 'v', 'g', 'd', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ʺ', 'y', '–', 'e',
29
        'A', 'B', 'V', 'G', 'D', 'Z', 'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'ʺ', 'Y', '–', 'E'
30
    );
31
    // @formatter:on
32
33
    /**
34
     * Transliterates cyrillic text to latin.
35
     *
36
     * @param  string $text cyrillic text
37
     *
38
     * @return string latin text
39
     */
40
    public static function translit2($text)
41
    {
42
        return self::transliterate($text, true);
43
    }
44
45
    /**
46
     * Transliterates latin text to cyrillic.
47
     *
48
     * @param  string $text latin text
49
     *
50
     * @return string cyrillic text
51
     */
52
    public static function translit4($text)
53
    {
54
        return self::transliterate($text, false);
55
    }
56
57
    /**
58
     * Transliterates cyrillic text to latin and vice versa
59
     * depending on $direction parameter.
60
     *
61
     * @param  string $text      latin text
62
     * @param  bool   $direction if true transliterates cyrillic text to latin, if false latin to cyrillic
63
     *
64
     * @return string transliterated text
65
     */
66
    public static function transliterate($text, $direction = true)
67
    {
68
        $from = self::$cyrMap;
69
        $to = self::$latMap;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $to. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
70
71
        if ($direction) {
72
            Helper::swap($from, $to);
73
        }
74
75
        return str_replace($from, $to, $text);
76
    }
77
}