Pluralizer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Illuminate\Support;
  3. use Doctrine\Inflector\Inflector;
  4. use Doctrine\Inflector\InflectorFactory;
  5. class Pluralizer
  6. {
  7. /**
  8. * Uncountable word forms.
  9. *
  10. * @var string[]
  11. */
  12. public static $uncountable = [
  13. 'audio',
  14. 'bison',
  15. 'cattle',
  16. 'chassis',
  17. 'compensation',
  18. 'coreopsis',
  19. 'data',
  20. 'deer',
  21. 'education',
  22. 'emoji',
  23. 'equipment',
  24. 'evidence',
  25. 'feedback',
  26. 'firmware',
  27. 'fish',
  28. 'furniture',
  29. 'gold',
  30. 'hardware',
  31. 'information',
  32. 'jedi',
  33. 'kin',
  34. 'knowledge',
  35. 'love',
  36. 'metadata',
  37. 'money',
  38. 'moose',
  39. 'news',
  40. 'nutrition',
  41. 'offspring',
  42. 'plankton',
  43. 'pokemon',
  44. 'police',
  45. 'rain',
  46. 'recommended',
  47. 'related',
  48. 'rice',
  49. 'series',
  50. 'sheep',
  51. 'software',
  52. 'species',
  53. 'swine',
  54. 'traffic',
  55. 'wheat',
  56. ];
  57. /**
  58. * Get the plural form of an English word.
  59. *
  60. * @param string $value
  61. * @param int|array|\Countable $count
  62. * @return string
  63. */
  64. public static function plural($value, $count = 2)
  65. {
  66. if (is_countable($count)) {
  67. $count = count($count);
  68. }
  69. if ((int) abs($count) === 1 || static::uncountable($value) || preg_match('/^(.*)[A-Za-z0-9\x{0080}-\x{FFFF}]$/u', $value) == 0) {
  70. return $value;
  71. }
  72. $plural = static::inflector()->pluralize($value);
  73. return static::matchCase($plural, $value);
  74. }
  75. /**
  76. * Get the singular form of an English word.
  77. *
  78. * @param string $value
  79. * @return string
  80. */
  81. public static function singular($value)
  82. {
  83. $singular = static::inflector()->singularize($value);
  84. return static::matchCase($singular, $value);
  85. }
  86. /**
  87. * Determine if the given value is uncountable.
  88. *
  89. * @param string $value
  90. * @return bool
  91. */
  92. protected static function uncountable($value)
  93. {
  94. return in_array(strtolower($value), static::$uncountable);
  95. }
  96. /**
  97. * Attempt to match the case on two strings.
  98. *
  99. * @param string $value
  100. * @param string $comparison
  101. * @return string
  102. */
  103. protected static function matchCase($value, $comparison)
  104. {
  105. $functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords'];
  106. foreach ($functions as $function) {
  107. if ($function($comparison) === $comparison) {
  108. return $function($value);
  109. }
  110. }
  111. return $value;
  112. }
  113. /**
  114. * Get the inflector instance.
  115. *
  116. * @return \Doctrine\Inflector\Inflector
  117. */
  118. public static function inflector()
  119. {
  120. static $inflector;
  121. if (is_null($inflector)) {
  122. $inflector = InflectorFactory::createForLanguage('english')->build();
  123. }
  124. return $inflector;
  125. }
  126. }