EnglishInflector.php
9 months ago
FrenchInflector.php
2 years ago
InflectorInterface.php
2 years ago
index.php
2 years ago
FrenchInflector.php
97 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\String\Inflector; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | final class FrenchInflector implements InflectorInterface |
| 5 | { |
| 6 | private const PLURALIZE_REGEXP = [ |
| 7 | // First entry: regexp |
| 8 | // Second entry: replacement |
| 9 | // Words finishing with "s", "x" or "z" are invariables |
| 10 | // Les mots finissant par "s", "x" ou "z" sont invariables |
| 11 | ['/(s|x|z)$/i', '\\1'], |
| 12 | // Words finishing with "eau" are pluralized with a "x" |
| 13 | // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 14 | ['/(eau)$/i', '\\1x'], |
| 15 | // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 16 | // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 17 | ['/^(landau)$/i', '\\1s'], |
| 18 | ['/(au)$/i', '\\1x'], |
| 19 | // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 20 | // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 21 | ['/^(pneu|bleu|émeu)$/i', '\\1s'], |
| 22 | ['/(eu)$/i', '\\1x'], |
| 23 | // Words finishing with "al" are pluralized with a "aux" excepted |
| 24 | // Les mots finissant en "al" se terminent en "aux" sauf |
| 25 | ['/^(bal|carnaval|caracal|chacal|choral|corral|étal|festival|récital|val)$/i', '\\1s'], |
| 26 | ['/al$/i', '\\1aux'], |
| 27 | // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 28 | ['/^(aspir|b|cor|ém|ferm|soupir|trav|vant|vitr)ail$/i', '\\1aux'], |
| 29 | // Bijou, caillou, chou, genou, hibou, joujou et pou qui prennent un x au pluriel |
| 30 | ['/^(bij|caill|ch|gen|hib|jouj|p)ou$/i', '\\1oux'], |
| 31 | // Invariable words |
| 32 | ['/^(cinquante|soixante|mille)$/i', '\\1'], |
| 33 | // French titles |
| 34 | ['/^(mon|ma)(sieur|dame|demoiselle|seigneur)$/', 'MailPoetVendor\\mes\\2s'], |
| 35 | ['/^(Mon|Ma)(sieur|dame|demoiselle|seigneur)$/', 'MailPoetVendor\\Mes\\2s'], |
| 36 | ]; |
| 37 | private const SINGULARIZE_REGEXP = [ |
| 38 | // First entry: regexp |
| 39 | // Second entry: replacement |
| 40 | // Aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail font leur pluriel en -aux |
| 41 | ['/((aspir|b|cor|ém|ferm|soupir|trav|vant|vitr))aux$/i', '\\1ail'], |
| 42 | // Words finishing with "eau" are pluralized with a "x" |
| 43 | // Les mots finissant par "eau" prennent tous un "x" au pluriel |
| 44 | ['/(eau)x$/i', '\\1'], |
| 45 | // Words finishing with "al" are pluralized with a "aux" expected |
| 46 | // Les mots finissant en "al" se terminent en "aux" sauf |
| 47 | ['/(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'], |
| 48 | // Words finishing with "au" are pluralized with a "x" excepted "landau" |
| 49 | // Les mots finissant par "au" prennent un "x" au pluriel sauf "landau" |
| 50 | ['/(au)x$/i', '\\1'], |
| 51 | // Words finishing with "eu" are pluralized with a "x" excepted "pneu", "bleu", "émeu" |
| 52 | // Les mots finissant en "eu" prennent un "x" au pluriel sauf "pneu", "bleu", "émeu" |
| 53 | ['/(eu)x$/i', '\\1'], |
| 54 | // Words finishing with "ou" are pluralized with a "s" excepted bijou, caillou, chou, genou, hibou, joujou, pou |
| 55 | // Les mots finissant par "ou" prennent un "s" sauf bijou, caillou, chou, genou, hibou, joujou, pou |
| 56 | ['/(bij|caill|ch|gen|hib|jouj|p)oux$/i', '\\1ou'], |
| 57 | // French titles |
| 58 | ['/^mes(dame|demoiselle)s$/', 'MailPoetVendor\\ma\\1'], |
| 59 | ['/^Mes(dame|demoiselle)s$/', 'MailPoetVendor\\Ma\\1'], |
| 60 | ['/^mes(sieur|seigneur)s$/', 'MailPoetVendor\\mon\\1'], |
| 61 | ['/^Mes(sieur|seigneur)s$/', 'MailPoetVendor\\Mon\\1'], |
| 62 | // Default rule |
| 63 | ['/s$/i', ''], |
| 64 | ]; |
| 65 | 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'; |
| 66 | public function singularize(string $plural) : array |
| 67 | { |
| 68 | if ($this->isInflectedWord($plural)) { |
| 69 | return [$plural]; |
| 70 | } |
| 71 | foreach (self::SINGULARIZE_REGEXP as $rule) { |
| 72 | [$regexp, $replace] = $rule; |
| 73 | if (1 === \preg_match($regexp, $plural)) { |
| 74 | return [\preg_replace($regexp, $replace, $plural)]; |
| 75 | } |
| 76 | } |
| 77 | return [$plural]; |
| 78 | } |
| 79 | public function pluralize(string $singular) : array |
| 80 | { |
| 81 | if ($this->isInflectedWord($singular)) { |
| 82 | return [$singular]; |
| 83 | } |
| 84 | foreach (self::PLURALIZE_REGEXP as $rule) { |
| 85 | [$regexp, $replace] = $rule; |
| 86 | if (1 === \preg_match($regexp, $singular)) { |
| 87 | return [\preg_replace($regexp, $replace, $singular)]; |
| 88 | } |
| 89 | } |
| 90 | return [$singular . 's']; |
| 91 | } |
| 92 | private function isInflectedWord(string $word) : bool |
| 93 | { |
| 94 | return 1 === \preg_match(self::UNINFLECTED, $word); |
| 95 | } |
| 96 | } |
| 97 |