|
| 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 | + * @see https://la-conjugaison.nouvelobs.com/regles/grammaire/le-pluriel-des-noms-121.php |
| 24 | + */ |
| 25 | + private static $pluralizeRegexp = [ |
| 26 | + // First entry: regexp |
| 27 | + // Second entry: replacement |
| 28 | + |
| 29 | + // Words finishing with "s", "x" or "z" are invariables |
| 30 | + // Les mots finissant par "s", "x" ou "z" sont invariables |
| 31 | + ['/(s|x|z)$/i', '\1'], |
| 32 | + |
| 33 | + // Words finishing with "eau" are pluralized with a "x" |
| 34 | + // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 35 | + ['/(eau)$/i', '\1x'], |
| 36 | + |
| 37 | + // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 38 | + // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 39 | + ['/^(landau)$/i', '\1s'], |
| 40 | + ['/(au)$/i', '\1x'], |
| 41 | + |
| 42 | + // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 43 | + // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 44 | + ['/^(pneu|bleu|émeu)$/i', '\1s'], |
| 45 | + ['/(eu)$/i', '\1x'], |
| 46 | + |
| 47 | + // Words finishing with "al" are pluralized with a "aux" excepted |
| 48 | + // Les mots finissant en "al" se terminent en "aux" sauf |
| 49 | + ['/^(bal|carnaval|caracal|chacal|choral|corral|étal|festival|récital|val)$/i', '\1s'], |
| 50 | + ['/al$/i', '\1aux'], |
| 51 | + |
| 52 | + // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 53 | + ['/^(aspir|b|cor|ém|ferm|soupir|trav|vant|vitr)ail$/i', '\1aux'], |
| 54 | + |
| 55 | + // Bijou, caillou, chou, genou, hibou, joujou et pou qui prennent un x au pluriel |
| 56 | + ['/^(bij|caill|ch|gen|hib|jouj|p)ou$/i', '\1oux'], |
| 57 | + |
| 58 | + // Invariable words |
| 59 | + ['/^(cinquante|soixante|mille)$/i', '\1'], |
| 60 | + |
| 61 | + // French titles |
| 62 | + ['/^(mon|ma)(sieur|dame|demoiselle|seigneur)$/', 'mes\2s'], |
| 63 | + ['/^(Mon|Ma)(sieur|dame|demoiselle|seigneur)$/', 'Mes\2s'], |
| 64 | + ]; |
| 65 | + |
| 66 | + /** |
| 67 | + * A list of all rules for singularize. |
| 68 | + */ |
| 69 | + private static $singularizeRegexp = [ |
| 70 | + // First entry: regexp |
| 71 | + // Second entry: replacement |
| 72 | + |
| 73 | + // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 74 | + ['/((aspir|b|cor|ém|ferm|soupir|trav|vant|vitr))aux$/i', '\1ail'], |
| 75 | + |
| 76 | + // Words finishing with "eau" are pluralized with a "x" |
| 77 | + // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 78 | + ['/(eau)x$/i', '\1'], |
| 79 | + |
| 80 | + // Words finishing with "al" are pluralized with a "aux" expected |
| 81 | + // Les mots finissant en "al" se terminent en "aux" sauf |
| 82 | + ['/(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'], |
| 83 | + |
| 84 | + // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 85 | + // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 86 | + ['/(au)x$/i', '\1'], |
| 87 | + |
| 88 | + // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 89 | + // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 90 | + ['/(eu)x$/i', '\1'], |
| 91 | + |
| 92 | + // Words finishing with "ou" are pluralized with a "s" excepted bijou, caillou, chou, genou, hibou, joujou, pou |
| 93 | + // Les mots finissant par "ou" prennent un "s" sauf bijou, caillou, chou, genou, hibou, joujou, pou |
| 94 | + ['/(bij|caill|ch|gen|hib|jouj|p)oux$/i', '\1ou'], |
| 95 | + |
| 96 | + // French titles |
| 97 | + ['/^mes(dame|demoiselle)s$/', 'ma\1'], |
| 98 | + ['/^Mes(dame|demoiselle)s$/', 'Ma\1'], |
| 99 | + ['/^mes(sieur|seigneur)s$/', 'mon\1'], |
| 100 | + ['/^Mes(sieur|seigneur)s$/', 'Mon\1'], |
| 101 | + |
| 102 | + //Default rule |
| 103 | + ['/s$/i', ''], |
| 104 | + ]; |
| 105 | + |
| 106 | + /** |
| 107 | + * A list of words which should not be inflected. |
| 108 | + * This list is only used by singularize. |
| 109 | + */ |
| 110 | + private static $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|sas|secours|sens|souris|succès|talus|tapis|tas|taudis|temps|tiers|univers|velours|verglas|vernis|virus)$/i'; |
| 111 | + |
| 112 | + /** |
| 113 | + * {@inheritdoc} |
| 114 | + */ |
| 115 | + public function singularize(string $plural): array |
| 116 | + { |
| 117 | + if ($this->isInflectedWord($plural)) { |
| 118 | + return [$plural]; |
| 119 | + } |
| 120 | + |
| 121 | + foreach (self::$singularizeRegexp as $rule) { |
| 122 | + [$regexp, $replace] = $rule; |
| 123 | + |
| 124 | + if (1 === preg_match($regexp, $plural)) { |
| 125 | + return [preg_replace($regexp, $replace, $plural)]; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return [$plural]; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * {@inheritdoc} |
| 134 | + */ |
| 135 | + public function pluralize(string $singular): array |
| 136 | + { |
| 137 | + if ($this->isInflectedWord($singular)) { |
| 138 | + return [$singular]; |
| 139 | + } |
| 140 | + |
| 141 | + foreach (self::$pluralizeRegexp as $rule) { |
| 142 | + [$regexp, $replace] = $rule; |
| 143 | + |
| 144 | + if (1 === preg_match($regexp, $singular)) { |
| 145 | + return [preg_replace($regexp, $replace, $singular)]; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return [$singular.'s']; |
| 150 | + } |
| 151 | + |
| 152 | + private function isInflectedWord(string $word): bool |
| 153 | + { |
| 154 | + return 1 === preg_match(self::$uninflected, $word); |
| 155 | + } |
| 156 | +} |
0 commit comments