| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\String\Inflector; |
| 13 |
|
| 14 |
final class SpanishInflector implements InflectorInterface |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
private const PLURALIZE_REGEXP = [ |
| 25 |
o |
| 26 |
['/(sí|no)$/i', '\1es'], |
| 27 |
|
| 28 |
|
| 29 |
['/(a|e|i|o|u|á|é|í|ó|ú)$/i', '\1s'], |
| 30 |
|
| 31 |
|
| 32 |
['/ás$/i', 'ases'], |
| 33 |
['/és$/i', 'eses'], |
| 34 |
['/ís$/i', 'ises'], |
| 35 |
['/ós$/i', 'oses'], |
| 36 |
['/ús$/i', 'uses'], |
| 37 |
|
| 38 |
s |
| 39 |
['/ión$/i', '\1iones'], |
| 40 |
|
| 41 |
|
| 42 |
['/(l|r|n|d|j|s|x|ch|y)$/i', '\1es'], |
| 43 |
|
| 44 |
|
| 45 |
['/(z)$/i', 'ces'], |
| 46 |
]; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private const SINGULARIZE_REGEXP = [ |
| 52 |
o |
| 53 |
['/(sí|no)es$/i', '\1'], |
| 54 |
|
| 55 |
s |
| 56 |
['/iones$/i', '\1ión'], |
| 57 |
|
| 58 |
|
| 59 |
['/ces$/i', 'z'], |
| 60 |
|
| 61 |
|
| 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 |
|
| 69 |
['/(l|r|n|d|j|s|x|ch|y)e?s$/i', '\1'], |
| 70 |
|
| 71 |
|
| 72 |
['/(a|e|i|o|u|á|é|ó|í|ú)s$/i', '\1'], |
| 73 |
]; |
| 74 |
|
| 75 |
private const UNINFLECTED_RULES = [ |
| 76 |
|
| 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 |
} |