|
1
|
|
|
<?php |
|
2
|
|
|
namespace morphos\Russian; |
|
3
|
|
|
|
|
4
|
|
|
use morphos\S; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Rules are from http://surnameonline.ru/patronymic.html |
|
8
|
|
|
*/ |
|
9
|
|
|
class MiddleNamesInflection extends \morphos\NamesInflection implements Cases |
|
10
|
|
|
{ |
|
11
|
|
|
use RussianLanguage, CasesHelper; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param $name |
|
15
|
|
|
* @return null|string |
|
16
|
|
|
*/ |
|
17
|
5 |
|
public static function detectGender($name) |
|
18
|
|
|
{ |
|
19
|
5 |
|
$name = S::lower($name); |
|
20
|
5 |
|
if (S::slice($name, -2) == 'ич') { |
|
|
|
|
|
|
21
|
3 |
|
return self::MALE; |
|
22
|
3 |
|
} elseif (S::slice($name, -2) == 'на') { |
|
|
|
|
|
|
23
|
3 |
|
return self::FEMALE; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $name |
|
31
|
|
|
* @param null $gender |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public static function isMutable($name, $gender = null) |
|
35
|
|
|
{ |
|
36
|
2 |
|
$name = S::lower($name); |
|
37
|
2 |
View Code Duplication |
if (in_array(S::slice($name, -2), ['ич', 'на'], true)) { |
|
|
|
|
|
|
38
|
2 |
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// it's foreign middle name, inflect it as a first name |
|
42
|
|
|
return FirstNamesInflection::isMutable($name, $gender); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param $name |
|
47
|
|
|
* @param $case |
|
48
|
|
|
* @param null $gender |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public static function getCase($name, $case, $gender = null) |
|
53
|
|
|
{ |
|
54
|
9 |
|
$case = self::canonizeCase($case); |
|
55
|
9 |
|
$forms = self::getCases($name, $gender); |
|
56
|
9 |
|
return $forms[$case]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $name |
|
61
|
|
|
* @param null $gender |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
21 |
|
public static function getCases($name, $gender = null) |
|
65
|
|
|
{ |
|
66
|
21 |
|
$name = S::lower($name); |
|
67
|
21 |
|
if (S::slice($name, -2) == 'ич') { |
|
|
|
|
|
|
68
|
|
|
// man rules |
|
69
|
8 |
|
$name = S::name($name); |
|
70
|
|
|
return array( |
|
71
|
8 |
|
Cases::IMENIT => $name, |
|
72
|
8 |
|
Cases::RODIT => $name.'а', |
|
73
|
8 |
|
Cases::DAT => $name.'у', |
|
74
|
8 |
|
Cases::VINIT => $name.'а', |
|
75
|
8 |
|
Cases::TVORIT => $name.'ем', |
|
76
|
8 |
|
Cases::PREDLOJ => $name.'е', |
|
77
|
|
|
); |
|
78
|
13 |
View Code Duplication |
} elseif (S::slice($name, -2) == 'на') { |
|
|
|
|
|
|
79
|
11 |
|
$prefix = S::name(S::slice($name, 0, -1)); |
|
|
|
|
|
|
80
|
|
|
return array( |
|
81
|
11 |
|
Cases::IMENIT => $prefix.'а', |
|
82
|
11 |
|
Cases::RODIT => $prefix.'ы', |
|
83
|
11 |
|
Cases::DAT => $prefix.'е', |
|
84
|
11 |
|
Cases::VINIT => $prefix.'у', |
|
85
|
11 |
|
Cases::TVORIT => $prefix.'ой', |
|
86
|
11 |
|
Cases::PREDLOJ => $prefix.'е', |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// inflect other middle names (foreign) as first names |
|
91
|
2 |
|
return FirstNamesInflection::getCases($name, $gender); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.