15
15
* Converts words between singular and plural forms.
16
16
*
17
17
* @author Bernhard Schussek <bschussek@gmail.com>
18
- *
19
- * @internal
20
18
*/
21
19
final class Inflector
22
20
{
23
21
/**
24
22
* Map English plural to singular suffixes.
25
23
*
26
- * @var array
27
- *
28
24
* @see http://english-zone.com/spelling/plurals.html
29
25
*/
30
26
private static $ pluralMap = [
@@ -142,175 +138,171 @@ final class Inflector
142
138
/**
143
139
* Map English singular to plural suffixes.
144
140
*
145
- * @var array
146
- *
147
141
* @see http://english-zone.com/spelling/plurals.html
148
142
*/
149
- private static $ singularMap = array (
143
+ private static $ singularMap = [
150
144
// First entry: singular suffix, reversed
151
145
// Second entry: length of singular suffix
152
146
// Third entry: Whether the suffix may succeed a vocal
153
147
// Fourth entry: Whether the suffix may succeed a consonant
154
148
// Fifth entry: plural suffix, normal
155
149
156
150
// criterion (criteria)
157
- array ( 'airetirc ' , 8 , false , false , 'criterion ' ) ,
151
+ [ 'airetirc ' , 8 , false , false , 'criterion ' ] ,
158
152
159
153
// nebulae (nebula)
160
- array ( 'aluben ' , 6 , false , false , 'nebulae ' ) ,
154
+ [ 'aluben ' , 6 , false , false , 'nebulae ' ] ,
161
155
162
156
// children (child)
163
- array ( 'dlihc ' , 5 , true , true , 'children ' ) ,
157
+ [ 'dlihc ' , 5 , true , true , 'children ' ] ,
164
158
165
159
// prices (price)
166
- array ( 'eci ' , 3 , false , true , 'ices ' ) ,
160
+ [ 'eci ' , 3 , false , true , 'ices ' ] ,
167
161
168
162
// services (service)
169
- array ( 'ecivres ' , 7 , true , true , 'services ' ) ,
163
+ [ 'ecivres ' , 7 , true , true , 'services ' ] ,
170
164
171
165
// lives (life), wives (wife)
172
- array ( 'efi ' , 3 , false , true , 'ives ' ) ,
166
+ [ 'efi ' , 3 , false , true , 'ives ' ] ,
173
167
174
168
// selfies (selfie)
175
- array ( 'eifles ' , 6 , true , true , 'selfies ' ) ,
169
+ [ 'eifles ' , 6 , true , true , 'selfies ' ] ,
176
170
177
171
// movies (movie)
178
- array ( 'eivom ' , 5 , true , true , 'movies ' ) ,
172
+ [ 'eivom ' , 5 , true , true , 'movies ' ] ,
179
173
180
174
// lice (louse)
181
- array ( 'esuol ' , 5 , false , true , 'lice ' ) ,
175
+ [ 'esuol ' , 5 , false , true , 'lice ' ] ,
182
176
183
177
// mice (mouse)
184
- array ( 'esuom ' , 5 , false , true , 'mice ' ) ,
178
+ [ 'esuom ' , 5 , false , true , 'mice ' ] ,
185
179
186
180
// geese (goose)
187
- array ( 'esoo ' , 4 , false , true , 'eese ' ) ,
181
+ [ 'esoo ' , 4 , false , true , 'eese ' ] ,
188
182
189
183
// houses (house), bases (base)
190
- array ( 'es ' , 2 , true , true , 'ses ' ) ,
184
+ [ 'es ' , 2 , true , true , 'ses ' ] ,
191
185
192
186
// geese (goose)
193
- array ( 'esoog ' , 5 , true , true , 'geese ' ) ,
187
+ [ 'esoog ' , 5 , true , true , 'geese ' ] ,
194
188
195
189
// caves (cave)
196
- array ( 'ev ' , 2 , true , true , 'ves ' ) ,
190
+ [ 'ev ' , 2 , true , true , 'ves ' ] ,
197
191
198
192
// drives (drive)
199
- array ( 'evird ' , 5 , false , true , 'drives ' ) ,
193
+ [ 'evird ' , 5 , false , true , 'drives ' ] ,
200
194
201
195
// objectives (objective), alternative (alternatives)
202
- array ( 'evit ' , 4 , true , true , 'tives ' ) ,
196
+ [ 'evit ' , 4 , true , true , 'tives ' ] ,
203
197
204
198
// moves (move)
205
- array ( 'evom ' , 4 , true , true , 'moves ' ) ,
199
+ [ 'evom ' , 4 , true , true , 'moves ' ] ,
206
200
207
201
// staves (staff)
208
- array ( 'ffats ' , 5 , true , true , 'staves ' ) ,
202
+ [ 'ffats ' , 5 , true , true , 'staves ' ] ,
209
203
210
204
// hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
211
- array ( 'ff ' , 2 , true , true , 'ffs ' ) ,
205
+ [ 'ff ' , 2 , true , true , 'ffs ' ] ,
212
206
213
207
// hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
214
- array ( 'f ' , 1 , true , true , array ( 'fs ' , 'ves ' )) ,
208
+ [ 'f ' , 1 , true , true , [ 'fs ' , 'ves ' ]] ,
215
209
216
210
// arches (arch)
217
- array ( 'hc ' , 2 , true , true , 'ches ' ) ,
211
+ [ 'hc ' , 2 , true , true , 'ches ' ] ,
218
212
219
213
// bushes (bush)
220
- array ( 'hs ' , 2 , true , true , 'shes ' ) ,
214
+ [ 'hs ' , 2 , true , true , 'shes ' ] ,
221
215
222
216
// teeth (tooth)
223
- array ( 'htoot ' , 5 , true , true , 'teeth ' ) ,
217
+ [ 'htoot ' , 5 , true , true , 'teeth ' ] ,
224
218
225
219
// bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
226
- array ( 'mu ' , 2 , true , true , 'a ' ) ,
220
+ [ 'mu ' , 2 , true , true , 'a ' ] ,
227
221
228
222
// echoes (echo)
229
- array ( 'ohce ' , 4 , true , true , 'echoes ' ) ,
223
+ [ 'ohce ' , 4 , true , true , 'echoes ' ] ,
230
224
231
225
// men (man), women (woman)
232
- array ( 'nam ' , 3 , true , true , 'men ' ) ,
226
+ [ 'nam ' , 3 , true , true , 'men ' ] ,
233
227
234
228
// people (person)
235
- array ( 'nosrep ' , 6 , true , true , array ( 'persons ' , 'people ' )) ,
229
+ [ 'nosrep ' , 6 , true , true , [ 'persons ' , 'people ' ]] ,
236
230
237
231
// bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
238
- array ( 'noi ' , 3 , true , true , 'ions ' ) ,
232
+ [ 'noi ' , 3 , true , true , 'ions ' ] ,
239
233
240
234
// bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
241
- array ( 'no ' , 2 , true , true , 'a ' ) ,
235
+ [ 'no ' , 2 , true , true , 'a ' ] ,
242
236
243
237
// atlases (atlas)
244
- array ( 'salta ' , 5 , true , true , 'atlases ' ) ,
238
+ [ 'salta ' , 5 , true , true , 'atlases ' ] ,
245
239
246
240
// irises (iris)
247
- array ( 'siri ' , 4 , true , true , 'irises ' ) ,
241
+ [ 'siri ' , 4 , true , true , 'irises ' ] ,
248
242
249
243
// analyses (analysis), ellipses (ellipsis), neuroses (neurosis)
250
244
// theses (thesis), emphases (emphasis), oases (oasis),
251
245
// crises (crisis)
252
- array ( 'sis ' , 3 , true , true , 'ses ' ) ,
246
+ [ 'sis ' , 3 , true , true , 'ses ' ] ,
253
247
254
248
// accesses (access), addresses (address), kisses (kiss)
255
- array ( 'ss ' , 2 , true , false , 'sses ' ) ,
249
+ [ 'ss ' , 2 , true , false , 'sses ' ] ,
256
250
257
251
// syllabi (syllabus)
258
- array ( 'suballys ' , 8 , true , true , 'syllabi ' ) ,
252
+ [ 'suballys ' , 8 , true , true , 'syllabi ' ] ,
259
253
260
254
// buses (bus)
261
- array ( 'sub ' , 3 , true , true , 'buses ' ) ,
255
+ [ 'sub ' , 3 , true , true , 'buses ' ] ,
262
256
263
257
// fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
264
- array ( 'su ' , 2 , true , true , 'i ' ) ,
258
+ [ 'su ' , 2 , true , true , 'i ' ] ,
265
259
266
260
// news (news)
267
- array ( 'swen ' , 4 , true , true , 'news ' ) ,
261
+ [ 'swen ' , 4 , true , true , 'news ' ] ,
268
262
269
263
// feet (foot)
270
- array ( 'toof ' , 4 , true , true , 'feet ' ) ,
264
+ [ 'toof ' , 4 , true , true , 'feet ' ] ,
271
265
272
266
// chateaux (chateau), bureaus (bureau)
273
- array ( 'uae ' , 3 , false , true , array ( 'eaus ' , 'eaux ' )) ,
267
+ [ 'uae ' , 3 , false , true , [ 'eaus ' , 'eaux ' ]] ,
274
268
275
269
// oxen (ox)
276
- array ( 'xo ' , 2 , false , false , 'oxen ' ) ,
270
+ [ 'xo ' , 2 , false , false , 'oxen ' ] ,
277
271
278
272
// hoaxes (hoax)
279
- array ( 'xaoh ' , 4 , true , false , 'hoaxes ' ) ,
273
+ [ 'xaoh ' , 4 , true , false , 'hoaxes ' ] ,
280
274
281
275
// indices (index)
282
- array ( 'xedni ' , 5 , false , true , array ( 'indicies ' , 'indexes ' )) ,
276
+ [ 'xedni ' , 5 , false , true , [ 'indicies ' , 'indexes ' ]] ,
283
277
284
278
// indexes (index), matrixes (matrix)
285
- array ( 'x ' , 1 , true , false , array ( 'cies ' , 'xes ' )) ,
279
+ [ 'x ' , 1 , true , false , [ 'cies ' , 'xes ' ]] ,
286
280
287
281
// appendices (appendix)
288
- array ( 'xi ' , 2 , false , true , 'ices ' ) ,
282
+ [ 'xi ' , 2 , false , true , 'ices ' ] ,
289
283
290
284
// babies (baby)
291
- array ( 'y ' , 1 , false , true , 'ies ' ) ,
285
+ [ 'y ' , 1 , false , true , 'ies ' ] ,
292
286
293
287
// quizzes (quiz)
294
- array ( 'ziuq ' , 4 , true , false , 'quizzes ' ) ,
288
+ [ 'ziuq ' , 4 , true , false , 'quizzes ' ] ,
295
289
296
290
// waltzes (waltz)
297
- array ( 'z ' , 1 , true , false , 'zes ' ) ,
298
- ) ;
291
+ [ 'z ' , 1 , true , false , 'zes ' ] ,
292
+ ] ;
299
293
300
294
/**
301
295
* A list of words which should not be inflected.
302
- *
303
- * @var array
304
296
*/
305
- private static $ uninflected = array (
297
+ private static $ uninflected = [
306
298
'data ' ,
307
299
'deer ' ,
308
300
'feedback ' ,
309
301
'fish ' ,
310
302
'moose ' ,
311
303
'series ' ,
312
304
'sheep ' ,
313
- ) ;
305
+ ] ;
314
306
315
307
/**
316
308
* This class should not be instantiated.
@@ -327,10 +319,7 @@ private function __construct()
327
319
*
328
320
* @param string $plural A word in plural form
329
321
*
330
- * @return string|array The singular form or an array of possible singular
331
- * forms
332
- *
333
- * @internal
322
+ * @return string|array The singular form or an array of possible singular forms
334
323
*/
335
324
public static function singularize (string $ plural )
336
325
{
@@ -339,7 +328,7 @@ public static function singularize(string $plural)
339
328
$ pluralLength = \strlen ($ lowerPluralRev );
340
329
341
330
// Check if the word is one which is not inflected, return early if so
342
- if (in_array (strtolower ($ plural ), self ::$ uninflected , true )) {
331
+ if (\ in_array (strtolower ($ plural ), self ::$ uninflected , true )) {
343
332
return $ plural ;
344
333
}
345
334
@@ -416,19 +405,16 @@ public static function singularize(string $plural)
416
405
*
417
406
* @param string $singular A word in plural form
418
407
*
419
- * @return string|array The plural form or an array of possible plural
420
- * forms
421
- *
422
- * @internal
408
+ * @return string|array The plural form or an array of possible plural forms
423
409
*/
424
410
public static function pluralize (string $ singular )
425
411
{
426
412
$ singularRev = strrev ($ singular );
427
413
$ lowerSingularRev = strtolower ($ singularRev );
428
- $ singularLength = strlen ($ lowerSingularRev );
414
+ $ singularLength = \ strlen ($ lowerSingularRev );
429
415
430
416
// Check if the word is one which is not inflected, return early if so
431
- if (in_array (strtolower ($ singular ), self ::$ uninflected , true )) {
417
+ if (\ in_array (strtolower ($ singular ), self ::$ uninflected , true )) {
432
418
return $ singular ;
433
419
}
434
420
@@ -474,8 +460,8 @@ public static function pluralize(string $singular)
474
460
// the singular suffix too
475
461
$ firstUpper = ctype_upper ($ singularRev [$ j - 1 ]);
476
462
477
- if (is_array ($ newSuffix )) {
478
- $ plurals = array () ;
463
+ if (\ is_array ($ newSuffix )) {
464
+ $ plurals = [] ;
479
465
480
466
foreach ($ newSuffix as $ newSuffixEntry ) {
481
467
$ plurals [] = $ newBase .($ firstUpper ? ucfirst ($ newSuffixEntry ) : $ newSuffixEntry );
0 commit comments