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