Test Failed
Branch master (a0b8e2)
by Konstantin
02:59
created

TranslitTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 79
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A wordTranslit() 0 7 1
A arrayTranslit() 0 9 2
A removeSymbols() 0 4 1
A removeSpecifications() 0 6 1
1
<?php
2
3
namespace GeoFixer\traits;
4
5
/**
6
 * Class TranslitTrait
7
 *
8
 * @package GeoFixer\traits
9
 */
10
trait TranslitTrait {
11
12
    /**
13
     * Массив букв для транслитерации
14
     *
15
     * @var array
16
     */
17
    public $alphabet = [
18
        "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
19
        "Д"=>"D","Е"=>"E","Ж"=>"J","З"=>"Z","И"=>"I",
20
        "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
21
        "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
22
        "У"=>"U","Ф"=>"F","Х"=>"Kh","Ц"=>"Ts","Ч"=>"Ch",
23
        "Ш"=>"Sh","Щ"=>"Sch","Ъ"=>"","Ы"=>"Yi","Ь"=>"",
24
        "Э"=>"E","Ю"=>"Yu","Я"=>"Ya","а"=>"a","б"=>"b",
25
        "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
26
        "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
27
        "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
28
        "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
29
        "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
30
        "ы"=>"yi","ь"=>"'","э"=>"e","ю"=>"yu","я"=>"ya"
31
    ];
32
33
    /**
34
     * Транслитерируем слово
35
     *
36
     * @param $word
37
     *
38
     * @return string
39
     */
40
    public function wordTranslit($word) {
41
        $word = $this->removeSpecifications($word);
42
        $word = mb_strtolower($word);
43
        $word = $this->removeSymbols($word);
44
45
        return strtr($word, $this->alphabet);
46
    }
47
48
    /**
49
     * Транслитерируем массив слов
50
     *
51
     * @param $array
52
     *
53
     * @return array
54
     */
55
    public function arrayTranslit($array) {
56
        $result = [];
57
58
        foreach ($array as $word) {
59
            $result[$word] = $this->wordTranslit($word);
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * Убираем пробелы и лишние символы, оставляем только кириллицу
67
     *
68
     * @param $word
69
     * @return mixed
70
     */
71
    public function removeSymbols($word)
72
    {
73
        return preg_replace('/[^а-яё]+/iu', '', $word);
74
    }
75
76
    /**
77
     * Убираем префиксы
78
     *
79
     * @param $word
80
     * @return mixed
81
     */
82
    public function removeSpecifications($word)
83
    {
84
        $search = include dirname(dirname(__FILE__)) . '/config/specifications.php';
85
86
        return str_ireplace($search, '', $word);
87
    }
88
}