pluralizer.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php namespace Laravel;
  2. class Pluralizer {
  3. /**
  4. * The "strings" configuration array.
  5. *
  6. * @var array
  7. */
  8. protected $config;
  9. /**
  10. * The cached copies of the plural inflections.
  11. */
  12. protected $plural = array();
  13. /**
  14. * The cached copies of the singular inflections.
  15. *
  16. * @var array
  17. */
  18. protected $singular = array();
  19. /**
  20. * Create a new pluralizer instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct($config)
  25. {
  26. $this->config = $config;
  27. }
  28. /**
  29. * Get the singular form of the given word.
  30. *
  31. * @param string $value
  32. * @return string
  33. */
  34. public function singular($value)
  35. {
  36. // First we'll check the cache of inflected values. We cache each word that
  37. // is inflected so we don't have to spin through the regular expressions
  38. // each time we need to inflect a given value for the developer.
  39. if (isset($this->singular[$value]))
  40. {
  41. return $this->singular[$value];
  42. }
  43. // English words may be automatically inflected using regular expressions.
  44. // If the word is english, we'll just pass off the word to the automatic
  45. // inflection method and return the result, which is cached.
  46. $irregular = $this->config['irregular'];
  47. $result = $this->auto($value, $this->config['singular'], $irregular);
  48. return $this->singular[$value] = $result ?: $value;
  49. }
  50. /**
  51. * Get the plural form of the given word.
  52. *
  53. * @param string $value
  54. * @param int $count
  55. * @return string
  56. */
  57. public function plural($value, $count = 2)
  58. {
  59. if ((int) $count == 1) return $value;
  60. // First we'll check the cache of inflected values. We cache each word that
  61. // is inflected so we don't have to spin through the regular expressions
  62. // each time we need to inflect a given value for the developer.
  63. if (isset($this->plural[$value]))
  64. {
  65. return $this->plural[$value];
  66. }
  67. // English words may be automatically inflected using regular expressions.
  68. // If the word is english, we'll just pass off the word to the automatic
  69. // inflection method and return the result, which is cached.
  70. $irregular = array_flip($this->config['irregular']);
  71. $result = $this->auto($value, $this->config['plural'], $irregular);
  72. return $this->plural[$value] = $result;
  73. }
  74. /**
  75. * Perform auto inflection on an English word.
  76. *
  77. * @param string $value
  78. * @param array $source
  79. * @param array $irregular
  80. * @return string
  81. */
  82. protected function auto($value, $source, $irregular)
  83. {
  84. // If the word hasn't been cached, we'll check the list of words that
  85. // that are "uncountable". This should be a quick look up since we
  86. // can just hit the array directly for the value.
  87. if (in_array(Str::lower($value), $this->config['uncountable']))
  88. {
  89. return $value;
  90. }
  91. // Next we will check the "irregular" patterns, which contains words
  92. // like "children" and "teeth" which can not be inflected using the
  93. // typically used regular expression matching approach.
  94. foreach ($irregular as $irregular => $pattern)
  95. {
  96. if (preg_match($pattern = '/'.$pattern.'$/i', $value))
  97. {
  98. return preg_replace($pattern, $irregular, $value);
  99. }
  100. }
  101. // Finally we'll spin through the array of regular expressions and
  102. // and look for matches for the word. If we find a match we will
  103. // cache and return the inflected value for quick look up.
  104. foreach ($source as $pattern => $inflected)
  105. {
  106. if (preg_match($pattern, $value))
  107. {
  108. return preg_replace($pattern, $inflected, $value);
  109. }
  110. }
  111. }
  112. }