Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.67% covered (danger)
46.67%
14 / 30
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SrConverter
46.67% covered (danger)
46.67%
14 / 30
57.14% covered (warning)
57.14%
4 / 7
21.29
0.00% covered (danger)
0.00%
0 / 1
 getMainCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLanguageVariants
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getVariantsFallbacks
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getAdditionalFlags
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 loadDefaultTables
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 translate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 guessVariant
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21/**
22 * Serbian (Српски / Srpski) specific code.
23 *
24 * There are two levels of conversion for Serbian: the script level
25 * (Cyrillics <-> Latin), and the variant level (ekavian
26 * <->iyekavian). The two are orthogonal. So we really only need two
27 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
28 * iyekavian.
29 *
30 * @ingroup Languages
31 */
32class SrConverter extends LanguageConverterSpecific {
33
34    /**
35     * @var string[]
36     */
37    public $mToLatin = [
38        'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
39        'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
40        'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
41        'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
42        'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
43        'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
44
45        'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
46        'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
47        'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
48        'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
49        'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
50        'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
51    ];
52
53    /**
54     * @var string[]
55     */
56    public $mToCyrillics = [
57        'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
58        'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
59        'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
60        'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
61        'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
62        't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
63
64        'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
65        'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
66        'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
67        'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
68        'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
69        'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
70
71        'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
72        'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
73        'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
74    ];
75
76    public function getMainCode(): string {
77        return 'sr';
78    }
79
80    public function getLanguageVariants(): array {
81        return [ 'sr', 'sr-ec', 'sr-el' ];
82    }
83
84    public function getVariantsFallbacks(): array {
85        return [
86            'sr' => 'sr-ec',
87            'sr-ec' => 'sr',
88            'sr-el' => 'sr',
89        ];
90    }
91
92    protected function getAdditionalFlags(): array {
93        return [
94            'S' => 'S',
95            'писмо' => 'S',
96            'pismo' => 'S',
97            'W' => 'W',
98            'реч' => 'W',
99            'reč' => 'W',
100            'ријеч' => 'W',
101            'riječ' => 'W'
102        ];
103    }
104
105    protected function loadDefaultTables(): array {
106        return [
107            'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
108            'sr-el' => new ReplacementArray( $this->mToLatin ),
109            'sr' => new ReplacementArray()
110        ];
111    }
112
113    /**
114     * Omits roman numbers
115     *
116     * @inheritDoc
117     */
118    public function translate( $text, $variant ) {
119        return $this->translateWithoutRomanNumbers( $text, $variant );
120    }
121
122    public function guessVariant( $text, $variant ) {
123        $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
124        $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
125
126        if ( $variant == 'sr-ec' ) {
127            return $numCyrillic > $numLatin;
128        } elseif ( $variant == 'sr-el' ) {
129            return $numLatin > $numCyrillic;
130        } else {
131            return false;
132        }
133    }
134}