SpanishInflector.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\String\Inflector;
  11. final class SpanishInflector implements InflectorInterface
  12. {
  13. /**
  14. * A list of all rules for pluralise.
  15. *
  16. * @see https://www.spanishdict.com/guide/spanish-plural-noun-forms
  17. * @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
  18. */
  19. // First entry: regex
  20. // Second entry: replacement
  21. private const PLURALIZE_REGEXP = [
  22. // Specials sí, no
  23. ['/(sí|no)$/i', '\1es'],
  24. // Words ending with vowel must use -s (RAE 3.2a, 3.2c)
  25. ['/(a|e|i|o|u|á|é|í|ó|ú)$/i', '\1s'],
  26. // Word ending in s or x and the previous letter is accented (RAE 3.2n)
  27. ['/ás$/i', 'ases'],
  28. ['/és$/i', 'eses'],
  29. ['/ís$/i', 'ises'],
  30. ['/ós$/i', 'oses'],
  31. ['/ús$/i', 'uses'],
  32. // Words ending in -ión must changed to -iones
  33. ['/ión$/i', '\1iones'],
  34. // Words ending in some consonants must use -es (RAE 3.2k)
  35. ['/(l|r|n|d|j|s|x|ch|y)$/i', '\1es'],
  36. // Word ending in z, must changed to ces
  37. ['/(z)$/i', 'ces'],
  38. ];
  39. /**
  40. * A list of all rules for singularize.
  41. */
  42. private const SINGULARIZE_REGEXP = [
  43. // Specials sí, no
  44. ['/(sí|no)es$/i', '\1'],
  45. // Words ending in -ión must changed to -iones
  46. ['/iones$/i', '\1ión'],
  47. // Word ending in z, must changed to ces
  48. ['/ces$/i', 'z'],
  49. // Word ending in s or x and the previous letter is accented (RAE 3.2n)
  50. ['/(\w)ases$/i', '\1ás'],
  51. ['/eses$/i', 'és'],
  52. ['/ises$/i', 'ís'],
  53. ['/(\w{2,})oses$/i', '\1ós'],
  54. ['/(\w)uses$/i', '\1ús'],
  55. // Words ending in some consonants and -es, must be the consonants
  56. ['/(l|r|n|d|j|s|x|ch|y)e?s$/i', '\1'],
  57. // Words ended with vowel and s, must be vowel
  58. ['/(a|e|i|o|u|á|é|ó|í|ú)s$/i', '\1'],
  59. ];
  60. private const UNINFLECTED_RULES = [
  61. // Words ending with pies (RAE 3.2n)
  62. '/.*(piés)$/i',
  63. ];
  64. private const UNINFLECTED = '/^(lunes|martes|miércoles|jueves|viernes|análisis|torax|yo|pies)$/i';
  65. public function singularize(string $plural): array
  66. {
  67. if ($this->isInflectedWord($plural)) {
  68. return [$plural];
  69. }
  70. foreach (self::SINGULARIZE_REGEXP as $rule) {
  71. [$regexp, $replace] = $rule;
  72. if (1 === preg_match($regexp, $plural)) {
  73. return [preg_replace($regexp, $replace, $plural)];
  74. }
  75. }
  76. return [$plural];
  77. }
  78. public function pluralize(string $singular): array
  79. {
  80. if ($this->isInflectedWord($singular)) {
  81. return [$singular];
  82. }
  83. foreach (self::PLURALIZE_REGEXP as $rule) {
  84. [$regexp, $replace] = $rule;
  85. if (1 === preg_match($regexp, $singular)) {
  86. return [preg_replace($regexp, $replace, $singular)];
  87. }
  88. }
  89. return [$singular.'s'];
  90. }
  91. private function isInflectedWord(string $word): bool
  92. {
  93. foreach (self::UNINFLECTED_RULES as $rule) {
  94. if (1 === preg_match($rule, $word)) {
  95. return true;
  96. }
  97. }
  98. return 1 === preg_match(self::UNINFLECTED, $word);
  99. }
  100. }