1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Inflector\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait InflectionSingularizeTrait |
7
|
|
|
* @package Nip\Inflector\Traits |
8
|
|
|
* |
9
|
|
|
* @method singularize($string) |
10
|
|
|
*/ |
11
|
|
|
trait InflectionSingularizeTrait |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected $singular = [ |
15
|
|
|
'rules' => [ |
16
|
|
|
'/(s)tatuses$/i' => '\1\2tatus', |
17
|
|
|
'/^(.*)(menu)s$/i' => '\1\2', |
18
|
|
|
'/(quiz)zes$/i' => '\\1', |
19
|
|
|
'/(matr)ices$/i' => '\1ix', |
20
|
|
|
'/(vert|ind)ices$/i' => '\1ex', |
21
|
|
|
'/^(ox)en/i' => '\1', |
22
|
|
|
'/(alias)(es)*$/i' => '\1', |
23
|
|
|
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', |
24
|
|
|
'/([ftw]ax)es/i' => '\1', |
25
|
|
|
'/(cris|ax|test)es$/i' => '\1is', |
26
|
|
|
'/(shoe)s$/i' => '\1', |
27
|
|
|
'/(o)es$/i' => '\1', |
28
|
|
|
'/ouses$/' => 'ouse', |
29
|
|
|
'/([^a])uses$/' => '\1us', |
30
|
|
|
'/([m|l])ice$/i' => '\1ouse', |
31
|
|
|
'/(x|ch|ss|sh)es$/i' => '\1', |
32
|
|
|
'/(m)ovies$/i' => '\1\2ovie', |
33
|
|
|
'/(s)eries$/i' => '\1\2eries', |
34
|
|
|
'/([^aeiouy]|qu)ies$/i' => '\1y', |
35
|
|
|
'/(tive)s$/i' => '\1', |
36
|
|
|
'/(hive)s$/i' => '\1', |
37
|
|
|
'/(drive)s$/i' => '\1', |
38
|
|
|
'/([le])ves$/i' => '\1f', |
39
|
|
|
'/([^rfoa])ves$/i' => '\1fe', |
40
|
|
|
'/(^analy)ses$/i' => '\1sis', |
41
|
|
|
'/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', |
42
|
|
|
'/([ti])a$/i' => '\1um', |
43
|
|
|
'/(p)eople$/i' => '\1\2erson', |
44
|
|
|
'/(m)en$/i' => '\1an', |
45
|
|
|
'/(c)hildren$/i' => '\1\2hild', |
46
|
|
|
'/(n)ews$/i' => '\1\2ews', |
47
|
|
|
'/eaus$/' => 'eau', |
48
|
|
|
'/^(.*us)$/' => '\\1', |
49
|
|
|
'/s$/i' => '' |
50
|
|
|
], |
51
|
|
|
'uncountable' => [ |
52
|
|
|
'.*[nrlm]ese', |
53
|
|
|
'.*data', |
54
|
|
|
'.*deer', |
55
|
|
|
'.*fish', |
56
|
|
|
'.*measles', |
57
|
|
|
'.*ois', |
58
|
|
|
'.*pox', |
59
|
|
|
'.*sheep', |
60
|
|
|
'people', |
61
|
|
|
'feedback', |
62
|
|
|
'stadia', |
63
|
|
|
'.*?media', |
64
|
|
|
'chassis', |
65
|
|
|
'clippers', |
66
|
|
|
'debris', |
67
|
|
|
'diabetes', |
68
|
|
|
'equipment', |
69
|
|
|
'gallows', |
70
|
|
|
'graffiti', |
71
|
|
|
'headquarters', |
72
|
|
|
'information', |
73
|
|
|
'innings', |
74
|
|
|
'news', |
75
|
|
|
'nexus', |
76
|
|
|
'pokemon', |
77
|
|
|
'proceedings', |
78
|
|
|
'research', |
79
|
|
|
'sea[- ]bass', |
80
|
|
|
'series', |
81
|
|
|
'species', |
82
|
|
|
'weather' |
83
|
|
|
], |
84
|
|
|
'irregular' => [ |
85
|
|
|
'atlas' => 'atlases', |
86
|
|
|
'beef' => 'beefs', |
87
|
|
|
'brief' => 'briefs', |
88
|
|
|
'brother' => 'brothers', |
89
|
|
|
'cafe' => 'cafes', |
90
|
|
|
'child' => 'children', |
91
|
|
|
'cookie' => 'cookies', |
92
|
|
|
'corpus' => 'corpuses', |
93
|
|
|
'cow' => 'cows', |
94
|
|
|
'criterion' => 'criteria', |
95
|
|
|
'ganglion' => 'ganglions', |
96
|
|
|
'genie' => 'genies', |
97
|
|
|
'genus' => 'genera', |
98
|
|
|
'graffito' => 'graffiti', |
99
|
|
|
'hoof' => 'hoofs', |
100
|
|
|
'loaf' => 'loaves', |
101
|
|
|
'man' => 'men', |
102
|
|
|
'money' => 'monies', |
103
|
|
|
'mongoose' => 'mongooses', |
104
|
|
|
'move' => 'moves', |
105
|
|
|
'mythos' => 'mythoi', |
106
|
|
|
'niche' => 'niches', |
107
|
|
|
'numen' => 'numina', |
108
|
|
|
'occiput' => 'occiputs', |
109
|
|
|
'octopus' => 'octopuses', |
110
|
|
|
'opus' => 'opuses', |
111
|
|
|
'ox' => 'oxen', |
112
|
|
|
'penis' => 'penises', |
113
|
|
|
'person' => 'people', |
114
|
|
|
'sex' => 'sexes', |
115
|
|
|
'soliloquy' => 'soliloquies', |
116
|
|
|
'testis' => 'testes', |
117
|
|
|
'trilby' => 'trilbys', |
118
|
|
|
'turf' => 'turfs', |
119
|
|
|
'potato' => 'potatoes', |
120
|
|
|
'hero' => 'heroes', |
121
|
|
|
'tooth' => 'teeth', |
122
|
|
|
'goose' => 'geese', |
123
|
|
|
'foot' => 'feet', |
124
|
|
|
'foe' => 'foes', |
125
|
|
|
'sieve' => 'sieves' |
126
|
|
|
] |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $word |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
protected function doSingularize($word) |
134
|
|
|
{ |
135
|
|
|
$lowercased_word = strtolower($word); |
136
|
|
|
foreach ($this->singular['uncountable'] as $_uncountable) { |
137
|
|
|
if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
138
|
|
|
return $word; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
foreach ($this->singular['irregular'] as $_plural => $_singular) { |
|
|
|
|
143
|
|
|
if (preg_match('/(' . $_singular . ')$/i', $word, $arr)) { |
144
|
|
|
return preg_replace('/(' . $_singular . ')$/i', substr($arr[0], 0, 1) . substr($_plural, 1), $word); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
foreach ($this->singular['rules'] as $rule => $replacement) { |
149
|
|
|
if (preg_match($rule, $word)) { |
150
|
|
|
return preg_replace($rule, $replacement, $word); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $word; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.