Pluralizer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Stringable;
  12. use Countable;
  13. use Doctrine\Inflector\Inflector;
  14. use Doctrine\Inflector\InflectorFactory;
  15. use Doctrine\Inflector\Language;
  16. class Pluralizer
  17. {
  18. /**
  19. * Uncountable word forms.
  20. */
  21. public static array $uncountable
  22. = [
  23. 'recommended',
  24. 'related',
  25. ];
  26. /**
  27. * The language that should be used by the inflector.
  28. */
  29. protected static string $language = Language::ENGLISH;
  30. protected static ?Inflector $inflector = null;
  31. /**
  32. * Get the plural form of an English word.
  33. */
  34. public static function plural(string $value, array|Countable|int $count = 2): string
  35. {
  36. if (is_countable($count)) {
  37. $count = count($count);
  38. }
  39. if ((int) abs($count) === 1 || static::uncountable($value) || preg_match('/^(.*)[A-Za-z0-9\x{0080}-\x{FFFF}]$/u', $value) == 0) {
  40. return $value;
  41. }
  42. $plural = static::getInflector()->pluralize($value);
  43. return static::matchCase($plural, $value);
  44. }
  45. /**
  46. * Get the singular form of an English word.
  47. */
  48. public static function singular(string $value): string
  49. {
  50. $singular = static::getInflector()->singularize($value);
  51. return static::matchCase($singular, $value);
  52. }
  53. public static function setInflector(?Inflector $inflector): void
  54. {
  55. static::$inflector = $inflector;
  56. }
  57. /**
  58. * Get the inflector instance.
  59. */
  60. public static function getInflector(): Inflector
  61. {
  62. if (is_null(static::$inflector)) {
  63. static::$inflector = InflectorFactory::createForLanguage(static::$language)->build();
  64. }
  65. return static::$inflector;
  66. }
  67. public static function useLanguage(string $language): void
  68. {
  69. static::$language = $language;
  70. static::$inflector = null;
  71. }
  72. /**
  73. * Determine if the given value is uncountable.
  74. *
  75. * @param string $value
  76. * @return bool
  77. */
  78. protected static function uncountable($value)
  79. {
  80. return in_array(strtolower($value), static::$uncountable);
  81. }
  82. /**
  83. * Attempt to match the case on two strings.
  84. */
  85. protected static function matchCase(string $value, string $comparison): string
  86. {
  87. $functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords'];
  88. foreach ($functions as $function) {
  89. if ($function($comparison) === $comparison) {
  90. return $function($value);
  91. }
  92. }
  93. return $value;
  94. }
  95. }