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 string $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 static::MALE; |
22
|
3 |
|
} elseif (S::slice($name, -2) == 'на') { |
23
|
3 |
|
return static::FEMALE; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $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 string $name |
47
|
|
|
* @param string $case |
48
|
|
|
* @param string|null $gender |
49
|
|
|
* @return string |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
9 |
|
public static function getCase($name, $case, $gender = null) |
53
|
|
|
{ |
54
|
9 |
|
$case = static::canonizeCase($case); |
55
|
9 |
|
$forms = static::getCases($name, $gender); |
56
|
9 |
|
return $forms[$case]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* @param string|null $gender |
62
|
|
|
* @return string[] |
63
|
|
|
* @phpstan-return array<string, string> |
64
|
|
|
*/ |
65
|
21 |
|
public static function getCases($name, $gender = null) |
66
|
|
|
{ |
67
|
21 |
|
$name = S::lower($name); |
68
|
21 |
|
if (S::slice($name, -2) == 'ич') { |
69
|
|
|
// man rules |
70
|
8 |
|
$name = S::name($name); |
71
|
|
|
return array( |
72
|
8 |
|
Cases::IMENIT => $name, |
73
|
8 |
|
Cases::RODIT => $name.'а', |
74
|
8 |
|
Cases::DAT => $name.'у', |
75
|
8 |
|
Cases::VINIT => $name.'а', |
76
|
8 |
|
Cases::TVORIT => $name.'ем', |
77
|
8 |
|
Cases::PREDLOJ => $name.'е', |
78
|
|
|
); |
79
|
13 |
View Code Duplication |
} elseif (S::slice($name, -2) == 'на') { |
|
|
|
|
80
|
11 |
|
$prefix = S::name(S::slice($name, 0, -1)); |
81
|
|
|
return array( |
82
|
11 |
|
Cases::IMENIT => $prefix.'а', |
83
|
11 |
|
Cases::RODIT => $prefix.'ы', |
84
|
11 |
|
Cases::DAT => $prefix.'е', |
85
|
11 |
|
Cases::VINIT => $prefix.'у', |
86
|
11 |
|
Cases::TVORIT => $prefix.'ой', |
87
|
11 |
|
Cases::PREDLOJ => $prefix.'е', |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// inflect other middle names (foreign) as first names |
92
|
2 |
|
return FirstNamesInflection::getCases($name, $gender); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.