Transliter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 82
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultAfter() 0 5 1
B translate() 0 21 7
1
<?php
2
namespace inblank\transliter;
3
4
use yii\base\Component;
5
6
/**
7
 * Transliteration
8
 *
9
 * @link https://github.com/inblank/yii2-transliter
10
 * @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]>
11
 * @license http://opensource.org/licenses/MIT
12
 */
13
class Transliter extends Component
14
{
15
    /**
16
     * Transliteration table for Russian characters
17
     * CAUTION: for this table you always must run callback Transliter::defaultAfter() because
18
     * russian `х` replaced on english `x` for future use in special rules which applies in `defaultAfter`
19
     * After applying `defaultAfter` russian `х` will be replaced on `h` or `kh` if follows after c,s,e,h
20
     * @var array
21
     */
22
    public $table = [
23
        'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z',
24
        'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r',
25
        'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'x', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'shch',
26
        'ы' => 'y', 'э' => 'eh', 'ю' => 'yu', 'я' => 'ya', 'ь' => '', 'ъ' => '',
27
28
        'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh', 'З' => 'Z',
29
        'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
30
        'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'X', 'Ц' => 'C', 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Shch',
31
        'Ы' => 'Y', 'Э' => 'Eh', 'Ю' => 'Yu', 'Я' => 'Ya', 'Ь' => '', 'Ъ' => '',
32
    ];
33
    /**
34
     * @var bool a sign of permissibility of dots in the transliterate result
35
     */
36
    public $useDot = false;
37
    /**
38
     * @var string space replace character
39
     */
40
    public $spacer = "-";
41
    /**
42
     * @var callable function|method calling with result string after transliteration
43
     */
44
    public $after = ["self", "defaultAfter"];
45
    /**
46
     * @var bool replace character for non alphanumeric characters. If false - special chars cuts
47
     */
48
    public $replaceSpecial = false;
49
50
    /**
51
     * @var bool convert result to lowercase
52
     */
53
    public $toLower = true;
54
55
    /**
56
     * Default after table translate method for Russian characters
57
     * @param string $str transliterated string
58
     * @return mixed
59
     */
60 1
    public static function defaultAfter($str)
61
    {
62
        // replace special cases for h for Russian language
63 1
        return preg_replace('/[x]/uim', 'h', preg_replace('/([cseh]){1}x/uim', '${1}kh', $str));
64
    }
65
66
    /**
67
     * Transliterate method
68
     * @param string $str input string
69
     * @param null|string $spacer space replacer. If null uses Transliter::$spacer
70
     * @param null|bool $toLower convert result to lowercase. If null uses Transliter::$toLower
71
     * @return string
72
     */
73 1
    public function translate($str, $spacer = null, $toLower = null)
74
    {
75 1
        if ($spacer === null) {
76 1
            $spacer = $this->spacer;
77 1
        }
78 1
        if ($toLower === null) {
79 1
            $toLower = $this->toLower;
80 1
        }
81 1
        $str = strtr($str, $this->table);
82 1
        if (is_callable($this->after)) {
83 1
            $str = call_user_func($this->after, $str);
84 1
        }
85
        // replace all non alpha-numeric characters
86 1
        $str = preg_replace('~[^' . ($this->useDot ? '\.' : '') . '\s\w\d_\-]~uism', empty($this->replaceSpecial) ? '' : $this->replaceSpecial, $str);
87
        // replace spaces
88 1
        $str = preg_replace('/\s/u', $spacer, $str);
89 1
        if ($toLower) {
90 1
            $str = strtolower($str);
91 1
        }
92 1
        return $str;
93
    }
94
}
95