Subversion-Projekte lars-tiefland.laravel_shop

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
/**
15
 * French inflector.
16
 *
17
 * This class does only inflect nouns; not adjectives nor composed words like "soixante-dix".
18
 */
19
final class FrenchInflector implements InflectorInterface
20
{
21
    /**
22
     * A list of all rules for pluralise.
23
     *
24
     * @see https://la-conjugaison.nouvelobs.com/regles/grammaire/le-pluriel-des-noms-121.php
25
     */
26
    private const PLURALIZE_REGEXP = [
27
        // First entry: regexp
28
        // Second entry: replacement
29
 
30
        // Words finishing with "s", "x" or "z" are invariables
31
        // Les mots finissant par "s", "x" ou "z" sont invariables
32
        ['/(s|x|z)$/i', '\1'],
33
 
34
        // Words finishing with "eau" are pluralized with a "x"
35
        // Les mots finissant par "eau" prennent tous un "x" au pluriel
36
        ['/(eau)$/i', '\1x'],
37
 
38
        // Words finishing with "au" are pluralized with a "x" excepted "landau"
39
        // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau"
40
        ['/^(landau)$/i', '\1s'],
41
        ['/(au)$/i', '\1x'],
42
 
43
        // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu"
44
        // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu"
45
        ['/^(pneu|bleu|émeu)$/i', '\1s'],
46
        ['/(eu)$/i', '\1x'],
47
 
48
        // Words finishing with "al" are pluralized with a "aux" excepted
49
        // Les mots finissant en "al" se terminent en "aux" sauf
50
        ['/^(bal|carnaval|caracal|chacal|choral|corral|étal|festival|récital|val)$/i', '\1s'],
51
        ['/al$/i', '\1aux'],
52
 
53
        // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux
54
        ['/^(aspir|b|cor|ém|ferm|soupir|trav|vant|vitr)ail$/i', '\1aux'],
55
 
56
        // Bijou, caillou, chou, genou, hibou, joujou et pou qui prennent un x au pluriel
57
        ['/^(bij|caill|ch|gen|hib|jouj|p)ou$/i', '\1oux'],
58
 
59
        // Invariable words
60
        ['/^(cinquante|soixante|mille)$/i', '\1'],
61
 
62
        // French titles
63
        ['/^(mon|ma)(sieur|dame|demoiselle|seigneur)$/', 'mes\2s'],
64
        ['/^(Mon|Ma)(sieur|dame|demoiselle|seigneur)$/', 'Mes\2s'],
65
    ];
66
 
67
    /**
68
     * A list of all rules for singularize.
69
     */
70
    private const SINGULARIZE_REGEXP = [
71
        // First entry: regexp
72
        // Second entry: replacement
73
 
74
        // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux
75
        ['/((aspir|b|cor|ém|ferm|soupir|trav|vant|vitr))aux$/i', '\1ail'],
76
 
77
        // Words finishing with "eau" are pluralized with a "x"
78
        // Les mots finissant par "eau" prennent tous un "x" au pluriel
79
        ['/(eau)x$/i', '\1'],
80
 
81
        // Words finishing with "al" are pluralized with a "aux" expected
82
        // Les mots finissant en "al" se terminent en "aux" sauf
83
        ['/(amir|anim|arsen|boc|can|capit|capor|chev|crist|génér|hopit|hôpit|idé|journ|littor|loc|m|mét|minér|princip|radic|termin)aux$/i', '\1al'],
84
 
85
        // Words finishing with "au" are pluralized with a "x" excepted "landau"
86
        // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau"
87
        ['/(au)x$/i', '\1'],
88
 
89
        // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu"
90
        // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu"
91
        ['/(eu)x$/i', '\1'],
92
 
93
        //  Words finishing with "ou" are pluralized with a "s" excepted bijou, caillou, chou, genou, hibou, joujou, pou
94
        // Les mots finissant par "ou" prennent un "s" sauf bijou, caillou, chou, genou, hibou, joujou, pou
95
        ['/(bij|caill|ch|gen|hib|jouj|p)oux$/i', '\1ou'],
96
 
97
        // French titles
98
        ['/^mes(dame|demoiselle)s$/', 'ma\1'],
99
        ['/^Mes(dame|demoiselle)s$/', 'Ma\1'],
100
        ['/^mes(sieur|seigneur)s$/', 'mon\1'],
101
        ['/^Mes(sieur|seigneur)s$/', 'Mon\1'],
102
 
103
        // Default rule
104
        ['/s$/i', ''],
105
    ];
106
 
107
    /**
108
     * A list of words which should not be inflected.
109
     * This list is only used by singularize.
110
     */
111
    private const UNINFLECTED = '/^(abcès|accès|abus|albatros|anchois|anglais|autobus|bois|brebis|carquois|cas|chas|colis|concours|corps|cours|cyprès|décès|devis|discours|dos|embarras|engrais|entrelacs|excès|fils|fois|gâchis|gars|glas|héros|intrus|jars|jus|kermès|lacis|legs|lilas|marais|mars|matelas|mépris|mets|mois|mors|obus|os|palais|paradis|parcours|pardessus|pays|plusieurs|poids|pois|pouls|printemps|processus|progrès|puits|pus|rabais|radis|recors|recours|refus|relais|remords|remous|rictus|rhinocéros|repas|rubis|sans|sas|secours|sens|souris|succès|talus|tapis|tas|taudis|temps|tiers|univers|velours|verglas|vernis|virus)$/i';
112
 
113
    public function singularize(string $plural): array
114
    {
115
        if ($this->isInflectedWord($plural)) {
116
            return [$plural];
117
        }
118
 
119
        foreach (self::SINGULARIZE_REGEXP as $rule) {
120
            [$regexp, $replace] = $rule;
121
 
122
            if (1 === preg_match($regexp, $plural)) {
123
                return [preg_replace($regexp, $replace, $plural)];
124
            }
125
        }
126
 
127
        return [$plural];
128
    }
129
 
130
    public function pluralize(string $singular): array
131
    {
132
        if ($this->isInflectedWord($singular)) {
133
            return [$singular];
134
        }
135
 
136
        foreach (self::PLURALIZE_REGEXP as $rule) {
137
            [$regexp, $replace] = $rule;
138
 
139
            if (1 === preg_match($regexp, $singular)) {
140
                return [preg_replace($regexp, $replace, $singular)];
141
            }
142
        }
143
 
144
        return [$singular.'s'];
145
    }
146
 
147
    private function isInflectedWord(string $word): bool
148
    {
149
        return 1 === preg_match(self::UNINFLECTED, $word);
150
    }
151
}