Completed
Push — master ( 2c1e58...2722e4 )
by f
02:18
created

LastNamesDeclension::isMutable()   C

Complexity

Conditions 9
Paths 14

Size

Total Lines 22
Code Lines 16

Duplication

Lines 2
Ratio 9.09 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 14
nop 2
dl 2
loc 22
rs 6.412
c 0
b 0
f 0
1
<?php
2
namespace morphos\Russian;
3
4
/**
5
 * Rules are from http://gramma.ru/SPR/?id=2.8
6
 */
7
class LastNamesDeclension extends \morphos\NamesDeclension implements Cases {
8
    use RussianLanguage, CasesHelper;
9
10
    static protected $menPostfixes = array('ов', 'ев' ,'ин' ,'ын', 'ой', 'ий');
11
    static protected $womenPostfixes = array('ва', 'на', 'ая', 'яя');
12
13
    public function isMutable($name, $gender = null) {
14
        $name = lower($name);
15
        if ($gender === null) $gender = $this->detectGender($name);
16
17 View Code Duplication
        if (in_array(slice($name, -1), array('а', 'я')))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
18
            return true;
19
20
        if ($gender == self::MAN) {
21
            if (in_array(slice($name, -2), array('ов', 'ев', 'ин', 'ын', 'ий', 'ой')))
22
                return true;
23
            if (in_array(upper(slice($name, -1)), RussianLanguage::$consonants))
24
                return true;
25
26
            if (slice($name, -1) == 'ь')
27
                return true;
28
        } else {
29
            if (in_array(slice($name, -2), array('ва', 'на')) || in_array(slice($name, -4), array('ская')))
30
                return true;
31
        }
32
33
        return false;
34
    }
35
36
    public function detectGender($name) {
37
        $name = lower($name);
38
        if (in_array(slice($name, -2), self::$menPostfixes))
39
            return self::MAN;
40
        if (in_array(slice($name, -2), self::$womenPostfixes))
41
            return self::WOMAN;
42
43
        return null;
44
    }
45
46
    public function getCases($name, $gender = null) {
47
        $name = lower($name);
48
        if ($gender === null) $gender = $this->detectGender($name);
49
        if ($gender == self::MAN) {
50
            if (in_array(slice($name, -2), array('ов', 'ев', 'ин', 'ын'))) {
51
                $prefix = name($name);
52
                return array(
53
                    self::IMENIT => $prefix,
54
                    self::RODIT => $prefix.'а',
55
                    self::DAT => $prefix.'у',
56
                    self::VINIT => $prefix.'а',
57
                    self::TVORIT => $prefix.'ым',
58
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
59
                );
60
            }
61
            else if (in_array(slice($name, -4), array('ский', 'ской', 'цкий', 'цкой'))) {
62
                $prefix = name(slice($name, 0, -2));
63
                return array(
64
                    self::IMENIT => name($name),
65
                    self::RODIT => $prefix.'ого',
66
                    self::DAT => $prefix.'ому',
67
                    self::VINIT => $prefix.'ого',
68
                    self::TVORIT => $prefix.'им',
69
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ом'
70
                );
71
            }
72
        } else {
73
            if (in_array(slice($name, -3), array('ова', 'ева', 'ина', 'ына'))) {
74
                $prefix = name(slice($name, 0, -1));
75
                return array(
76
                    self::IMENIT => name($name),
77
                    self::RODIT => $prefix.'ой',
78
                    self::DAT => $prefix.'ой',
79
                    self::VINIT => $prefix.'у',
80
                    self::TVORIT => $prefix.'ой',
81
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ой'
82
                );
83
            }
84
85 View Code Duplication
            if (in_array(slice($name, -4), array('ская'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
86
                $prefix = name(slice($name, 0, -2));
87
                return array(
88
                    self::IMENIT => name($name),
89
                    self::RODIT => $prefix.'ой',
90
                    self::DAT => $prefix.'ой',
91
                    self::VINIT => $prefix.'ую',
92
                    self::TVORIT => $prefix.'ой',
93
                    self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ой'
94
                );
95
            }
96
        }
97
98
        if (slice($name, -1) == 'я') {
99
            $prefix = name(slice($name, 0, -1));
100
            return array(
101
                self::IMENIT => name($name),
102
                self::RODIT => $prefix.'и',
103
                self::DAT => $prefix.'е',
104
                self::VINIT => $prefix.'ю',
105
                self::TVORIT => $prefix.'ей',
106
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
107
            );
108
        } else if (slice($name, -1) == 'а') {
109
            $prefix = name(slice($name, 0, -1));
110
            return array(
111
                self::IMENIT => name($name),
112
                self::RODIT => $prefix.'ы',
113
                self::DAT => $prefix.'е',
114
                self::VINIT => $prefix.'у',
115
                self::TVORIT => $prefix.'ой',
116
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
117
            );
118
        } else if (in_array(upper(slice($name, -1)), RussianLanguage::$consonants)) {
119
            $prefix = name($name);
120
            return array(
121
                self::IMENIT => name($name),
122
                self::RODIT => $prefix.'а',
123
                self::DAT => $prefix.'у',
124
                self::VINIT => $prefix.'а',
125
                self::TVORIT => $prefix.'ом',
126
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
127
            );
128 View Code Duplication
        } else if (slice($name, -1) == 'ь') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
129
            $prefix = name(slice($name, 0, -1));
130
            return array(
131
                self::IMENIT => name($name),
132
                self::RODIT => $prefix.'я',
133
                self::DAT => $prefix.'ю',
134
                self::VINIT => $prefix.'я',
135
                self::TVORIT => $prefix.'ем',
136
                self::PREDLOJ => $this->choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'е'
137
            );
138
        }
139
140
        $name = name($name);
141
        return array_fill_keys(array(self::IMENIT, self::RODIT, self::DAT, self::VINIT, self::TVORIT), $name) + array(self::PREDLOJ => $this->choosePrepositionByFirstLetter($name, 'об', 'о').' '.$name);
142
    }
143
144
    public function getCase($name, $case, $gender = null) {
145
        $case = self::canonizeCase($case);
146
        $forms = $this->getCases($name, $gender);
147
        return $forms[$case];
148
    }
149
}
150