forked from MikeBazhenov/petrovich
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Petrovich.php
232 lines (200 loc) · 7.03 KB
/
Petrovich.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
class Petrovich {
private $rules; //Правила
const CASE_NOMENATIVE = -1; //именительный
const CASE_GENITIVE = 0; //родительный
const CASE_DATIVE = 1; //дательный
const CASE_ACCUSATIVE = 2; //винительный
const CASE_INSTRUMENTAL = 3; //творительный
const CASE_PREPOSITIONAL = 4; //предложный
const GENDER_ANDROGYNOUS = 0; // Пол не определен
const GENDER_MALE = 1; // Мужской
const GENDER_FEMALE = 2; // Женский
private $gender = Petrovich::GENDER_ANDROGYNOUS; //Пол male/мужской female/женский
/**
* Конструтор класса Петрович
* загружаем правила из файла rules.json
*/
function __construct($gender = Petrovich::GENDER_ANDROGYNOUS, $rules_dir = __DIR__) {
$rules_path = $rules_dir.'/rules/rules.json';
$rules_resourse = fopen($rules_path, 'r');
if($rules_resourse == false)
throw new Exception('Rules file not found.');
$rules_array = fread($rules_resourse,filesize($rules_path));
fclose($rules_resourse);
$this->rules = get_object_vars(json_decode($rules_array));
if (isset($gender) && $gender != Petrovich::GENDER_ANDROGYNOUS)
$this->gender = $gender;
}
/**
* Определяет пол по отчеству
* @param $middlename
* @return integer
* @throws Exception
*/
public function detectGender($middlename)
{
if(empty($middlename))
throw new Exception('Middlename cannot be empty.');
switch ( mb_substr( mb_strtolower($middlename) , -4))
{
case 'оглы': return Petrovich::GENDER_MALE; break;
case 'кызы': return Petrovich::GENDER_FEMALE; break;
}
switch ( mb_substr( mb_strtolower($middlename) , -2))
{
case 'ич': return Petrovich::GENDER_MALE; break;
case 'на': return Petrovich::GENDER_FEMALE; break;
default: return Petrovich::GENDER_ANDROGYNOUS; break;
}
}
/**
* Задаём имя и слоняем его
*
* @param $firstname
* @param $case
* @return bool|string
* @throws Exception
*/
public function firstname($firstname, $case = Petrovich::CASE_NOMENATIVE) {
if(empty($firstname))
throw new Exception('Firstname cannot be empty.');
if ($case === Petrovich::CASE_NOMENATIVE) {
return $firstname;
}
return $this->inflect($firstname,$case,__FUNCTION__);
}
/**
* Задём отчество и склоняем его
*
* @param $middlename
* @param $case
* @return bool|string
* @throws Exception
*/
public function middlename($middlename, $case = Petrovich::CASE_NOMENATIVE) {
if(empty($middlename))
throw new Exception('Middlename cannot be empty.');
if ($case === Petrovich::CASE_NOMENATIVE) {
return $middlename;
}
return $this->inflect($middlename,$case,__FUNCTION__);
}
/**
* Задаём фамилию и слоняем её
*
* @param $lastname
* @param $case
* @return bool|string
* @throws Exception
*/
public function lastname($lastname, $case = Petrovich::CASE_NOMENATIVE) {
if(empty($lastname))
throw new Exception('Lastname cannot be empty.');
if ($case === Petrovich::CASE_NOMENATIVE) {
return $lastname;
}
return $this->inflect($lastname,$case,__FUNCTION__);
}
/**
* Функция проверяет заданное имя,фамилию или отчество на исключение
* и склоняет
*
* @param $name
* @param $case
* @param $type
* @return bool|string
*/
private function inflect($name,$case,$type) {
$names_arr = explode('-',$name);
$result = array();
foreach($names_arr as $arr_name) {
if(($exception = $this->checkException($arr_name,$case,$type)) !== false) {
$result[] = $exception;
}
else {
$result[] = $this->findInRules($arr_name,$case,$type);
}
}
return implode('-',$result);
}
/**
* Поиск в массиве правил
*
* @param $name
* @param $case
* @param $type
* @return string
*/
private function findInRules($name,$case,$type) {
foreach($this->rules[$type]->suffixes as $rule) {
if ( ! $this->checkGender($rule->gender) )
continue;
foreach($rule->test as $last_char) {
$last_name_char = mb_substr($name,mb_strlen($name)-mb_strlen($last_char),mb_strlen($last_char));
if($last_char == $last_name_char) {
if($rule->mods[$case] == '.')
return $name;
return $this->applyRule($rule->mods,$name,$case);
}
}
}
return $name;
}
/**
* Проверка на совпадение в исключениях
*
* @param $name
* @param $case
* @param $type
* @return bool|string
*/
private function checkException($name,$case,$type) {
if(!isset($this->rules[$type]->exceptions))
return false;
$lower_name = mb_strtolower($name);
foreach($this->rules[$type]->exceptions as $rule) {
if ( ! $this->checkGender($rule->gender) )
continue;
if(array_search($lower_name,$rule->test) !== false) {
if($rule->mods[$case] == '.')
return $name;
return $this->applyRule($rule->mods,$name,$case);
}
}
return false;
}
/**
* Склоняем заданное слово
*
* @param $mods
* @param $name
* @param $case
* @return string
*/
private function applyRule($mods,$name,$case) {
$result = mb_substr($name,0,mb_strlen($name) - mb_substr_count($mods[$case],'-'));
$result .= str_replace('-','',$mods[$case]);
return $result;
}
/**
* Преобразует строковое обозначение пола в числовое
* @param string
* @return integer
*/
private function getGender($gender) {
switch($gender) {
case 'male': return Petrovich::GENDER_MALE; break;
case 'female': return Petrovich::GENDER_FEMALE; break;
case 'androgynous': return Petrovich::GENDER_ANDROGYNOUS; break;
}
}
/**
* Проверяет переданный пол на соответствие установленному
* @param string
* @return bool
*/
private function checkGender($gender) {
return $this->gender === $this->getGender($gender) || $this->getGender($gender) === Petrovich::GENDER_ANDROGYNOUS;
}
}