1
|
|
|
<?php |
2
|
|
|
namespace morphos\English; |
3
|
|
|
|
4
|
|
|
use morphos\S; |
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
class NounPluralization extends \morphos\BasePluralization |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string[] |
11
|
|
|
* @phpstan-var array<string, string> |
12
|
|
|
*/ |
13
|
|
|
private static $exceptions = [ |
14
|
|
|
'chief' => 'chiefs', |
15
|
|
|
'basis' => 'bases', |
16
|
|
|
'crisis' => 'crises', |
17
|
|
|
'radius' => 'radii', |
18
|
|
|
'nucleus' => 'nuclei', |
19
|
|
|
'curriculum' => 'curricula', |
20
|
|
|
'man' => 'men', |
21
|
|
|
'woman' => 'women', |
22
|
|
|
'child' => 'children', |
23
|
|
|
'foot' => 'feet', |
24
|
|
|
'tooth' => 'teeth', |
25
|
|
|
'ox' => 'oxen', |
26
|
|
|
'goose' => 'geese', |
27
|
|
|
'mouse' => 'mice' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** @var string[] */ |
31
|
|
|
private static $without_paired_form = [ |
32
|
|
|
'knowledge', |
33
|
|
|
'progress', |
34
|
|
|
'advise', |
35
|
|
|
'ink', |
36
|
|
|
'money', |
37
|
|
|
'scissors', |
38
|
|
|
'spectacles', |
39
|
|
|
'trousers', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** @var string[] */ |
43
|
|
|
public static $consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w']; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $word |
47
|
|
|
* @param int $count |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
50 |
|
public static function pluralize($word, $count = 2) |
51
|
|
|
{ |
52
|
50 |
|
if ($count == 1) { |
53
|
9 |
|
return $word; |
54
|
|
|
} |
55
|
|
|
|
56
|
47 |
|
$word = S::lower($word); |
57
|
47 |
|
if (in_array($word, static::$without_paired_form)) { |
|
|
|
|
58
|
8 |
|
return $word; |
59
|
39 |
|
} elseif (isset(static::$exceptions[$word])) { |
|
|
|
|
60
|
14 |
|
return static::$exceptions[$word]; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
25 |
|
if (in_array(S::slice($word, -1), ['s', 'x']) || in_array(S::slice($word, -2), array('sh', 'ch'))) { |
64
|
2 |
|
return $word.'es'; |
65
|
23 |
|
} elseif (S::slice($word, -1) == 'o') { |
66
|
1 |
|
return $word.'es'; |
67
|
22 |
|
} elseif (S::slice($word, -1) == 'y' && in_array(S::slice($word, -2, -1), static::$consonants)) { |
68
|
2 |
|
return S::slice($word, 0, -1).'ies'; |
69
|
20 |
|
} elseif (S::slice($word, -2) == 'fe' || S::slice($word, -1) == 'f') { |
70
|
2 |
|
if (S::slice($word, -1) == 'f') { |
71
|
1 |
|
return S::slice($word, 0, -1).'ves'; |
72
|
|
|
} else { |
73
|
1 |
|
return S::slice($word, 0, -2).'ves'; |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
18 |
|
return $word.'s'; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function getCase($word, $case, $animateness = false) |
81
|
|
|
{ |
82
|
|
|
throw new RuntimeException('Not implemented'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function getCases($word, $animateness = false) |
86
|
|
|
{ |
87
|
|
|
throw new RuntimeException('Not implemented'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: