Pluralizer.php 2.4KB

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