pluralizer.php 3.4 KB

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