1
|
|
|
<?php |
2
|
|
|
namespace morphos\Russian; |
3
|
|
|
|
4
|
|
|
use morphos\Gender; |
5
|
|
|
use morphos\S; |
6
|
|
|
|
7
|
|
|
trait RussianLanguage |
8
|
|
|
{ |
9
|
|
|
public static $vowels = array( |
10
|
|
|
'а', |
11
|
|
|
'е', |
12
|
|
|
'ё', |
13
|
|
|
'и', |
14
|
|
|
'о', |
15
|
|
|
'у', |
16
|
|
|
'ы', |
17
|
|
|
'э', |
18
|
|
|
'ю', |
19
|
|
|
'я', |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
public static $consonants = array( |
23
|
|
|
'б', |
24
|
|
|
'в', |
25
|
|
|
'г', |
26
|
|
|
'д', |
27
|
|
|
'ж', |
28
|
|
|
'з', |
29
|
|
|
'й', |
30
|
|
|
'к', |
31
|
|
|
'л', |
32
|
|
|
'м', |
33
|
|
|
'н', |
34
|
|
|
'п', |
35
|
|
|
'р', |
36
|
|
|
'с', |
37
|
|
|
'т', |
38
|
|
|
'ф', |
39
|
|
|
'х', |
40
|
|
|
'ц', |
41
|
|
|
'ч', |
42
|
|
|
'ш', |
43
|
|
|
'щ', |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
public static $pairs = array( |
47
|
|
|
'б' => 'п', |
48
|
|
|
'в' => 'ф', |
49
|
|
|
'г' => 'к', |
50
|
|
|
'д' => 'т', |
51
|
|
|
'ж' => 'ш', |
52
|
|
|
'з' => 'с', |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
public static $deafConsonants = ['х', 'ч', 'щ']; |
56
|
|
|
public static $sonorousConsonants = ['л', 'м', 'н', 'р']; |
57
|
|
|
|
58
|
|
|
public static function isVowel($char) |
59
|
|
|
{ |
60
|
|
|
return in_array($char, self::$vowels); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function isConsonant($char) |
64
|
|
|
{ |
65
|
|
|
return in_array($char, self::$consonants); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function isDeafConsonant($char) |
69
|
|
|
{ |
70
|
|
|
return in_array($char, self::$deafConsonants); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function isSonorousConsonant($char) |
74
|
|
|
{ |
75
|
|
|
return in_array($char, self::$sonorousConsonants); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function isHissingConsonant($consonant) |
79
|
|
|
{ |
80
|
|
|
return in_array(S::lower($consonant), array('ж', 'ш', 'ч', 'щ')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected static function isVelarConsonant($consonant) |
84
|
|
|
{ |
85
|
|
|
return in_array(S::lower($consonant), array('г', 'к', 'х')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function countSyllables($string) |
89
|
|
|
{ |
90
|
|
|
return S::chars_count($string, self::$vowels); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function isPaired($consonant) |
94
|
|
|
{ |
95
|
|
|
$consonant = S::lower($consonant); |
96
|
|
|
return array_key_exists($consonant, self::$pairs) || (array_search($consonant, self::$pairs) !== false); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function checkLastConsonantSoftness($word) |
100
|
|
|
{ |
101
|
|
|
if (($substring = S::last_position_for_one_of_chars(S::lower($word), self::$consonants)) !== false) { |
102
|
|
|
if (in_array(S::slice($substring, 0, 1), ['й', 'ч', 'щ'])) { // always soft consonants |
103
|
|
|
return true; |
104
|
|
|
} elseif (S::length($substring) > 1 && in_array(S::slice($substring, 1, 2), ['е', 'ё', 'и', 'ю', 'я', 'ь'])) { // consonants are soft if they are trailed with these vowels |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public static function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition) |
112
|
|
|
{ |
113
|
|
View Code Duplication |
if (in_array(S::upper(S::slice($word, 0, 1)), array('А', 'О', 'И', 'У', 'Э'))) { |
|
|
|
|
114
|
|
|
return $prepositionWithVowel; |
115
|
|
|
} else { |
116
|
|
|
return $preposition; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function chooseVowelAfterConsonant($last, $soft_last, $after_soft, $after_hard) |
121
|
|
|
{ |
122
|
|
|
if ((RussianLanguage::isHissingConsonant($last) && !in_array($last, array('ж', 'ч'))) || /*self::isVelarConsonant($last) ||*/ $soft_last) { |
123
|
|
|
return $after_soft; |
124
|
|
|
} else { |
125
|
|
|
return $after_hard; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $verb Verb to modify if gender is female |
131
|
|
|
* @param string $gender If not `m`, verb will be modified |
132
|
|
|
* @return string Correct verb |
133
|
|
|
*/ |
134
|
|
|
public static function verb($verb, $gender) |
135
|
|
|
{ |
136
|
|
|
$verb = S::lower($verb); |
137
|
|
|
// возвратный глагол |
138
|
|
|
if (S::slice($verb, -2) == 'ся') { |
139
|
|
|
return ($gender == Gender::MALE ? $verb : mb_substr($verb, 0, -2).'ась'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// обычный глагол |
143
|
|
|
return ($gender == Gender::MALE ? $verb : $verb.'а'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add 'в' or 'во' prepositional before the word |
148
|
|
|
* @param string $word |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public static function in($word) |
152
|
|
|
{ |
153
|
|
|
$normalized = trim(S::lower($word)); |
154
|
|
|
if (in_array(S::slice($normalized, 0, 1), ['в', 'ф'])) |
155
|
|
|
return 'во '.$word; |
156
|
|
|
return 'в '.$word; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Add 'с' or 'со' prepositional before the word |
161
|
|
|
* @param string $word |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public static function with($word) |
165
|
|
|
{ |
166
|
|
|
$normalized = trim(S::lower($word)); |
167
|
|
|
if (in_array(S::slice($normalized, 0, 1), ['c', 'з', 'ш', 'ж']) && static::isConsonant(S::slice($normalized, 1, 2)) || S::slice($normalized, 0, 1) == 'щ') |
168
|
|
|
return 'со '.$word; |
169
|
|
|
return 'с '.$word; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Add 'о' or 'об' or 'обо' prepositional before the word |
174
|
|
|
* @param string $word |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public static function about($word) |
178
|
|
|
{ |
179
|
|
|
$normalized = trim(S::lower($word)); |
180
|
|
|
if (static::isVowel(S::slice($normalized, 0, 1)) && !in_array(S::slice($normalized, 0, 1), ['е', 'ё', 'ю', 'я'])) |
181
|
|
|
return 'об '.$word; |
182
|
|
|
|
183
|
|
View Code Duplication |
if (in_array(S::slice($normalized, 0, 3), ['все', 'всё', 'всю', 'что', 'мне'])) |
|
|
|
|
184
|
|
|
return 'обо '.$word; |
185
|
|
|
|
186
|
|
|
return 'о '.$word; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.