Completed
Push — master ( 13b26c...ac5926 )
by Gordon
07:17 queued 06:10
created

LanguageHelper::pluralizeNoun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\RandomEnglish\Helper;
4
5
use Doctrine\Inflector\InflectorFactory;
6
7
class LanguageHelper
8
{
9
    private const DOUBLE_LAST_CHARS = ['b', 'd', 'g', 'l', 'm', 'n', 'p', 't'];
10
11
    private const DO_NOT_DOUBLE_LAST_CHAR_TWO_CHARS = [
12
        'mb',
13
        'ad',
14
        'ld',
15
        'dd',
16
        'ed',
17
        'nd',
18
        'rd',
19
        'gg',
20
        'ng',
21
        'nt',
22
        'en',
23
        'wn',
24
        'rn',
25
        'am',
26
        'om',
27
        'rm',
28
        'rn',
29
        'lp',
30
        'ep',
31
        'mp',
32
        'op',
33
        'rp',
34
        'up',
35
        'ct',
36
        'at',
37
        'et',
38
        'ht',
39
        'lm',
40
        'lt',
41
        'ut',
42
        'ot',
43
        'pt',
44
        'rt',
45
        'st',
46
    ];
47
48
    private const DO_NOT_DOUBLE_LAST_CHAR_THREE_CHARS = [
49
        'ain',
50
        'ait',
51
        'ion',
52
        'oin',
53
        'ool',
54
        'son',
55
        'oap',
56
        'oon',
57
        'ron',
58
        // visit
59
        'sit',
60
        'tim',
61
    ];
62
63
64
    /** @var \Doctrine\Inflector\Inflector */
65
    private $inflector;
66
67
68
    public function __construct()
69
    {
70
        // English only
71
        $this->inflector = InflectorFactory::create()->build();
72
    }
73
74
75
    /**
76
     * @param string $noun A noun
77
     * @return string the plurarl of that noun
78
     */
79
    public function pluralizeNoun(string $noun): string
80
    {
81
        return $this->inflector->pluralize($noun);
82
    }
83
84
85
    /**
86
     * Attempt to convert a verb into a verb with ing.
87
     *
88
     * @param string $verb a verb
89
     * @return string the ing version of the verb
90
     */
91
    public function ingVerb(string $verb): string
92
    {
93
        $lastChar = \substr($verb, -1);
94
        $lastTwoChars = \substr($verb, -2);
95
        $lastThreeChars = \substr($verb, -3);
96
97
        $verbPart = $verb;
98
        if (\substr($verb, -1) === 'e' && \substr($verb, -2) !== 'ee') {
99
            $verbPart = \rtrim($verb, 'e');
100
        }
101
102
        if (\in_array($lastChar, self::DOUBLE_LAST_CHARS, true)
103
            && !\in_array($lastTwoChars, self::DO_NOT_DOUBLE_LAST_CHAR_TWO_CHARS, true)
104
            && !\in_array($lastThreeChars, self::DO_NOT_DOUBLE_LAST_CHAR_THREE_CHARS, true)
105
        ) {
106
            $verbPart .= $lastChar;
107
        }
108
109
        $result = $verbPart . 'ing';
110
111
        // deal with lie ->lying
112
        $result = \str_replace('iing', 'ying', $result);
113
114
        // deal with special cases
115
        switch ($verb) {
116
            case 'sit':
117
                $result = 'sitting';
118
119
                break;
120
        }
121
122
        return $result;
123
    }
124
}
125