Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\String\Inflector;
13
 
14
final class EnglishInflector implements InflectorInterface
15
{
16
    /**
17
     * Map English plural to singular suffixes.
18
     *
19
     * @see http://english-zone.com/spelling/plurals.html
20
     */
21
    private const PLURAL_MAP = [
22
        // First entry: plural suffix, reversed
23
        // Second entry: length of plural suffix
24
        // Third entry: Whether the suffix may succeed a vocal
25
        // Fourth entry: Whether the suffix may succeed a consonant
26
        // Fifth entry: singular suffix, normal
27
 
28
        // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
29
        ['a', 1, true, true, ['on', 'um']],
30
 
31
        // nebulae (nebula)
32
        ['ea', 2, true, true, 'a'],
33
 
34
        // services (service)
35
        ['secivres', 8, true, true, 'service'],
36
 
37
        // mice (mouse), lice (louse)
38
        ['eci', 3, false, true, 'ouse'],
39
 
40
        // geese (goose)
41
        ['esee', 4, false, true, 'oose'],
42
 
43
        // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
44
        ['i', 1, true, true, 'us'],
45
 
46
        // men (man), women (woman)
47
        ['nem', 3, true, true, 'man'],
48
 
49
        // children (child)
50
        ['nerdlihc', 8, true, true, 'child'],
51
 
52
        // oxen (ox)
53
        ['nexo', 4, false, false, 'ox'],
54
 
55
        // indices (index), appendices (appendix), prices (price)
56
        ['seci', 4, false, true, ['ex', 'ix', 'ice']],
57
 
58
        // selfies (selfie)
59
        ['seifles', 7, true, true, 'selfie'],
60
 
61
        // zombies (zombie)
62
        ['seibmoz', 7, true, true, 'zombie'],
63
 
64
        // movies (movie)
65
        ['seivom', 6, true, true, 'movie'],
66
 
67
        // conspectuses (conspectus), prospectuses (prospectus)
68
        ['sesutcep', 8, true, true, 'pectus'],
69
 
70
        // feet (foot)
71
        ['teef', 4, true, true, 'foot'],
72
 
73
        // geese (goose)
74
        ['eseeg', 5, true, true, 'goose'],
75
 
76
        // teeth (tooth)
77
        ['hteet', 5, true, true, 'tooth'],
78
 
79
        // news (news)
80
        ['swen', 4, true, true, 'news'],
81
 
82
        // series (series)
83
        ['seires', 6, true, true, 'series'],
84
 
85
        // babies (baby)
86
        ['sei', 3, false, true, 'y'],
87
 
88
        // accesses (access), addresses (address), kisses (kiss)
89
        ['sess', 4, true, false, 'ss'],
90
 
91
        // analyses (analysis), ellipses (ellipsis), fungi (fungus),
92
        // neuroses (neurosis), theses (thesis), emphases (emphasis),
93
        // oases (oasis), crises (crisis), houses (house), bases (base),
94
        // atlases (atlas)
95
        ['ses', 3, true, true, ['s', 'se', 'sis']],
96
 
97
        // objectives (objective), alternative (alternatives)
98
        ['sevit', 5, true, true, 'tive'],
99
 
100
        // drives (drive)
101
        ['sevird', 6, false, true, 'drive'],
102
 
103
        // lives (life), wives (wife)
104
        ['sevi', 4, false, true, 'ife'],
105
 
106
        // moves (move)
107
        ['sevom', 5, true, true, 'move'],
108
 
109
        // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf), caves (cave), staves (staff)
110
        ['sev', 3, true, true, ['f', 've', 'ff']],
111
 
112
        // axes (axis), axes (ax), axes (axe)
113
        ['sexa', 4, false, false, ['ax', 'axe', 'axis']],
114
 
115
        // indexes (index), matrixes (matrix)
116
        ['sex', 3, true, false, 'x'],
117
 
118
        // quizzes (quiz)
119
        ['sezz', 4, true, false, 'z'],
120
 
121
        // bureaus (bureau)
122
        ['suae', 4, false, true, 'eau'],
123
 
124
        // fees (fee), trees (tree), employees (employee)
125
        ['see', 3, true, true, 'ee'],
126
 
127
        // edges (edge)
128
        ['segd', 4, true, true, 'dge'],
129
 
130
        // roses (rose), garages (garage), cassettes (cassette),
131
        // waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
132
        // shoes (shoe)
133
        ['se', 2, true, true, ['', 'e']],
134
 
135
        // tags (tag)
136
        ['s', 1, true, true, ''],
137
 
138
        // chateaux (chateau)
139
        ['xuae', 4, false, true, 'eau'],
140
 
141
        // people (person)
142
        ['elpoep', 6, true, true, 'person'],
143
    ];
144
 
145
    /**
146
     * Map English singular to plural suffixes.
147
     *
148
     * @see http://english-zone.com/spelling/plurals.html
149
     */
150
    private const SINGULAR_MAP = [
151
        // First entry: singular suffix, reversed
152
        // Second entry: length of singular suffix
153
        // Third entry: Whether the suffix may succeed a vocal
154
        // Fourth entry: Whether the suffix may succeed a consonant
155
        // Fifth entry: plural suffix, normal
156
 
157
        // criterion (criteria)
158
        ['airetirc', 8, false, false, 'criterion'],
159
 
160
        // nebulae (nebula)
161
        ['aluben', 6, false, false, 'nebulae'],
162
 
163
        // children (child)
164
        ['dlihc', 5, true, true, 'children'],
165
 
166
        // prices (price)
167
        ['eci', 3, false, true, 'ices'],
168
 
169
        // services (service)
170
        ['ecivres', 7, true, true, 'services'],
171
 
172
        // lives (life), wives (wife)
173
        ['efi', 3, false, true, 'ives'],
174
 
175
        // selfies (selfie)
176
        ['eifles', 6, true, true, 'selfies'],
177
 
178
        // movies (movie)
179
        ['eivom', 5, true, true, 'movies'],
180
 
181
        // lice (louse)
182
        ['esuol', 5, false, true, 'lice'],
183
 
184
        // mice (mouse)
185
        ['esuom', 5, false, true, 'mice'],
186
 
187
        // geese (goose)
188
        ['esoo', 4, false, true, 'eese'],
189
 
190
        // houses (house), bases (base)
191
        ['es', 2, true, true, 'ses'],
192
 
193
        // geese (goose)
194
        ['esoog', 5, true, true, 'geese'],
195
 
196
        // caves (cave)
197
        ['ev', 2, true, true, 'ves'],
198
 
199
        // drives (drive)
200
        ['evird', 5, false, true, 'drives'],
201
 
202
        // objectives (objective), alternative (alternatives)
203
        ['evit', 4, true, true, 'tives'],
204
 
205
        // moves (move)
206
        ['evom', 4, true, true, 'moves'],
207
 
208
        // staves (staff)
209
        ['ffats', 5, true, true, 'staves'],
210
 
211
        // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
212
        ['ff', 2, true, true, 'ffs'],
213
 
214
        // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf)
215
        ['f', 1, true, true, ['fs', 'ves']],
216
 
217
        // arches (arch)
218
        ['hc', 2, true, true, 'ches'],
219
 
220
        // bushes (bush)
221
        ['hs', 2, true, true, 'shes'],
222
 
223
        // teeth (tooth)
224
        ['htoot', 5, true, true, 'teeth'],
225
 
226
        // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
227
        ['mu', 2, true, true, 'a'],
228
 
229
        // men (man), women (woman)
230
        ['nam', 3, true, true, 'men'],
231
 
232
        // people (person)
233
        ['nosrep', 6, true, true, ['persons', 'people']],
234
 
235
        // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
236
        ['noi', 3, true, true, 'ions'],
237
 
238
        // coupon (coupons)
239
        ['nop', 3, true, true, 'pons'],
240
 
241
        // seasons (season), treasons (treason), poisons (poison), lessons (lesson)
242
        ['nos', 3, true, true, 'sons'],
243
 
244
        // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
245
        ['no', 2, true, true, 'a'],
246
 
247
        // echoes (echo)
248
        ['ohce', 4, true, true, 'echoes'],
249
 
250
        // heroes (hero)
251
        ['oreh', 4, true, true, 'heroes'],
252
 
253
        // atlases (atlas)
254
        ['salta', 5, true, true, 'atlases'],
255
 
256
        // irises (iris)
257
        ['siri', 4, true, true, 'irises'],
258
 
259
        // analyses (analysis), ellipses (ellipsis), neuroses (neurosis)
260
        // theses (thesis), emphases (emphasis), oases (oasis),
261
        // crises (crisis)
262
        ['sis', 3, true, true, 'ses'],
263
 
264
        // accesses (access), addresses (address), kisses (kiss)
265
        ['ss', 2, true, false, 'sses'],
266
 
267
        // syllabi (syllabus)
268
        ['suballys', 8, true, true, 'syllabi'],
269
 
270
        // buses (bus)
271
        ['sub', 3, true, true, 'buses'],
272
 
273
        // circuses (circus)
274
        ['suc', 3, true, true, 'cuses'],
275
 
276
        // conspectuses (conspectus), prospectuses (prospectus)
277
        ['sutcep', 6, true, true, 'pectuses'],
278
 
279
        // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
280
        ['su', 2, true, true, 'i'],
281
 
282
        // news (news)
283
        ['swen', 4, true, true, 'news'],
284
 
285
        // feet (foot)
286
        ['toof', 4, true, true, 'feet'],
287
 
288
        // chateaux (chateau), bureaus (bureau)
289
        ['uae', 3, false, true, ['eaus', 'eaux']],
290
 
291
        // oxen (ox)
292
        ['xo', 2, false, false, 'oxen'],
293
 
294
        // hoaxes (hoax)
295
        ['xaoh', 4, true, false, 'hoaxes'],
296
 
297
        // indices (index)
298
        ['xedni', 5, false, true, ['indicies', 'indexes']],
299
 
300
        // boxes (box)
301
        ['xo', 2, false, true, 'oxes'],
302
 
303
        // indexes (index), matrixes (matrix)
304
        ['x', 1, true, false, ['cies', 'xes']],
305
 
306
        // appendices (appendix)
307
        ['xi', 2, false, true, 'ices'],
308
 
309
        // babies (baby)
310
        ['y', 1, false, true, 'ies'],
311
 
312
        // quizzes (quiz)
313
        ['ziuq', 4, true, false, 'quizzes'],
314
 
315
        // waltzes (waltz)
316
        ['z', 1, true, true, 'zes'],
317
    ];
318
 
319
    /**
320
     * A list of words which should not be inflected, reversed.
321
     */
322
    private const UNINFLECTED = [
323
        '',
324
 
325
        // data
326
        'atad',
327
 
328
        // deer
329
        'reed',
330
 
331
        // feedback
332
        'kcabdeef',
333
 
334
        // fish
335
        'hsif',
336
 
337
        // info
338
        'ofni',
339
 
340
        // moose
341
        'esoom',
342
 
343
        // series
344
        'seires',
345
 
346
        // sheep
347
        'peehs',
348
 
349
        // species
350
        'seiceps',
351
    ];
352
 
353
    public function singularize(string $plural): array
354
    {
355
        $pluralRev = strrev($plural);
356
        $lowerPluralRev = strtolower($pluralRev);
357
        $pluralLength = \strlen($lowerPluralRev);
358
 
359
        // Check if the word is one which is not inflected, return early if so
360
        if (\in_array($lowerPluralRev, self::UNINFLECTED, true)) {
361
            return [$plural];
362
        }
363
 
364
        // The outer loop iterates over the entries of the plural table
365
        // The inner loop $j iterates over the characters of the plural suffix
366
        // in the plural table to compare them with the characters of the actual
367
        // given plural suffix
368
        foreach (self::PLURAL_MAP as $map) {
369
            $suffix = $map[0];
370
            $suffixLength = $map[1];
371
            $j = 0;
372
 
373
            // Compare characters in the plural table and of the suffix of the
374
            // given plural one by one
375
            while ($suffix[$j] === $lowerPluralRev[$j]) {
376
                // Let $j point to the next character
377
                ++$j;
378
 
379
                // Successfully compared the last character
380
                // Add an entry with the singular suffix to the singular array
381
                if ($j === $suffixLength) {
382
                    // Is there any character preceding the suffix in the plural string?
383
                    if ($j < $pluralLength) {
384
                        $nextIsVocal = str_contains('aeiou', $lowerPluralRev[$j]);
385
 
386
                        if (!$map[2] && $nextIsVocal) {
387
                            // suffix may not succeed a vocal but next char is one
388
                            break;
389
                        }
390
 
391
                        if (!$map[3] && !$nextIsVocal) {
392
                            // suffix may not succeed a consonant but next char is one
393
                            break;
394
                        }
395
                    }
396
 
397
                    $newBase = substr($plural, 0, $pluralLength - $suffixLength);
398
                    $newSuffix = $map[4];
399
 
400
                    // Check whether the first character in the plural suffix
401
                    // is uppercased. If yes, uppercase the first character in
402
                    // the singular suffix too
403
                    $firstUpper = ctype_upper($pluralRev[$j - 1]);
404
 
405
                    if (\is_array($newSuffix)) {
406
                        $singulars = [];
407
 
408
                        foreach ($newSuffix as $newSuffixEntry) {
409
                            $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
410
                        }
411
 
412
                        return $singulars;
413
                    }
414
 
415
                    return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
416
                }
417
 
418
                // Suffix is longer than word
419
                if ($j === $pluralLength) {
420
                    break;
421
                }
422
            }
423
        }
424
 
425
        // Assume that plural and singular is identical
426
        return [$plural];
427
    }
428
 
429
    public function pluralize(string $singular): array
430
    {
431
        $singularRev = strrev($singular);
432
        $lowerSingularRev = strtolower($singularRev);
433
        $singularLength = \strlen($lowerSingularRev);
434
 
435
        // Check if the word is one which is not inflected, return early if so
436
        if (\in_array($lowerSingularRev, self::UNINFLECTED, true)) {
437
            return [$singular];
438
        }
439
 
440
        // The outer loop iterates over the entries of the singular table
441
        // The inner loop $j iterates over the characters of the singular suffix
442
        // in the singular table to compare them with the characters of the actual
443
        // given singular suffix
444
        foreach (self::SINGULAR_MAP as $map) {
445
            $suffix = $map[0];
446
            $suffixLength = $map[1];
447
            $j = 0;
448
 
449
            // Compare characters in the singular table and of the suffix of the
450
            // given plural one by one
451
 
452
            while ($suffix[$j] === $lowerSingularRev[$j]) {
453
                // Let $j point to the next character
454
                ++$j;
455
 
456
                // Successfully compared the last character
457
                // Add an entry with the plural suffix to the plural array
458
                if ($j === $suffixLength) {
459
                    // Is there any character preceding the suffix in the plural string?
460
                    if ($j < $singularLength) {
461
                        $nextIsVocal = str_contains('aeiou', $lowerSingularRev[$j]);
462
 
463
                        if (!$map[2] && $nextIsVocal) {
464
                            // suffix may not succeed a vocal but next char is one
465
                            break;
466
                        }
467
 
468
                        if (!$map[3] && !$nextIsVocal) {
469
                            // suffix may not succeed a consonant but next char is one
470
                            break;
471
                        }
472
                    }
473
 
474
                    $newBase = substr($singular, 0, $singularLength - $suffixLength);
475
                    $newSuffix = $map[4];
476
 
477
                    // Check whether the first character in the singular suffix
478
                    // is uppercased. If yes, uppercase the first character in
479
                    // the singular suffix too
480
                    $firstUpper = ctype_upper($singularRev[$j - 1]);
481
 
482
                    if (\is_array($newSuffix)) {
483
                        $plurals = [];
484
 
485
                        foreach ($newSuffix as $newSuffixEntry) {
486
                            $plurals[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
487
                        }
488
 
489
                        return $plurals;
490
                    }
491
 
492
                    return [$newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix)];
493
                }
494
 
495
                // Suffix is longer than word
496
                if ($j === $singularLength) {
497
                    break;
498
                }
499
            }
500
        }
501
 
502
        // Assume that plural is singular with a trailing `s`
503
        return [$singular.'s'];
504
    }
505
}