Debian Codegraph @

vendor/symfony/string/Inflector/SpanishInflector.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 SpanishInflector implements InflectorInterface
15 {
16 /**
17 * A list of all rules for pluralise.
18 *
19 * @see https://www.spanishdict.com/guide/spanish-plural-noun-forms
20 * @see https://www.rae.es/gram%C3%A1tica/morfolog%C3%ADa/la-formaci%C3%B3n-del-plural-plurales-en-s-y-plurales-en-es-reglas-generales
21 */
22 // First entry: regex
23 // Second entry: replacement
24 private const PLURALIZE_REGEXP = [
25 // Specials sí, no
26 ['/(sí|no)$/i', '\1es'],
27
28 // Words ending with vowel must use -s (RAE 3.2a, 3.2c)
29 ['/(a|e|i|o|u|á|é|í|ó|ú)$/i', '\1s'],
30
31 // Word ending in s or x and the previous letter is accented (RAE 3.2n)
32 ['/ás$/i', 'ases'],
33 ['/és$/i', 'eses'],
34 ['/ís$/i', 'ises'],
35 ['/ós$/i', 'oses'],
36 ['/ús$/i', 'uses'],
37
38 // Words ending in -ión must changed to -iones
39 ['/ión$/i', '\1iones'],
40
41 // Words ending in some consonants must use -es (RAE 3.2k)
42 ['/(l|r|n|d|j|s|x|ch|y)$/i', '\1es'],
43
44 // Word ending in z, must changed to ces
45 ['/(z)$/i', 'ces'],
46 ];
47
48 /**
49 * A list of all rules for singularize.
50 */
51 private const SINGULARIZE_REGEXP = [
52 // Specials sí, no
53 ['/(sí|no)es$/i', '\1'],
54
55 // Words ending in -ión must changed to -iones
56 ['/iones$/i', '\1ión'],
57
58 // Word ending in z, must changed to ces
59 ['/ces$/i', 'z'],
60
61 // Word ending in s or x and the previous letter is accented (RAE 3.2n)
62 ['/(\w)ases$/i', '\1ás'],
63 ['/eses$/i', 'és'],
64 ['/ises$/i', 'ís'],
65 ['/(\w{2,})oses$/i', '\1ós'],
66 ['/(\w)uses$/i', '\1ús'],
67
68 // Words ending in some consonants and -es, must be the consonants
69 ['/(l|r|n|d|j|s|x|ch|y)e?s$/i', '\1'],
70
71 // Words ended with vowel and s, must be vowel
72 ['/(a|e|i|o|u|á|é|ó|í|ú)s$/i', '\1'],
73 ];
74
75 private const UNINFLECTED_RULES = [
76 // Words ending with pies (RAE 3.2n)
77 '/.*(piés)$/i',
78 ];
79
80 private const UNINFLECTED = '/^(lunes|martes|miércoles|jueves|viernes|análisis|torax|yo|pies)$/i';
81
82 public function singularize(string $plural): array
83 {
84 if ($this->isInflectedWord($plural)) {
85 return [$plural];
86 }
87
88 foreach (self::SINGULARIZE_REGEXP as $rule) {
89 [$regexp, $replace] = $rule;
90
91 if (1 === preg_match($regexp, $plural)) {
92 return [preg_replace($regexp, $replace, $plural)];
93 }
94 }
95
96 return [$plural];
97 }
98
99 public function pluralize(string $singular): array
100 {
101 if ($this->isInflectedWord($singular)) {
102 return [$singular];
103 }
104
105 foreach (self::PLURALIZE_REGEXP as $rule) {
106 [$regexp, $replace] = $rule;
107
108 if (1 === preg_match($regexp, $singular)) {
109 return [preg_replace($regexp, $replace, $singular)];
110 }
111 }
112
113 return [$singular.'s'];
114 }
115
116 private function isInflectedWord(string $word): bool
117 {
118 foreach (self::UNINFLECTED_RULES as $rule) {
119 if (1 === preg_match($rule, $word)) {
120 return true;
121 }
122 }
123
124 return 1 === preg_match(self::UNINFLECTED, $word);
125 }
126 }