Просмотр файла vendor/illuminate/support/Pluralizer.php

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