| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use Doctrine\Inflector\InflectorFactory; |
| 6 |
|
| 7 |
class Pluralizer |
| 8 |
{ |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
protected static $inflector; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
protected static $language = 'english'; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public static $uncountable = [ |
| 31 |
'recommended', |
| 32 |
'related', |
| 33 |
]; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public static function plural($value, $count = 2) |
| 43 |
{ |
| 44 |
if (is_countable($count)) { |
| 45 |
$count = count($count); |
| 46 |
} |
| 47 |
|
| 48 |
if ((int) abs($count) === 1 || static::uncountable($value) || preg_match('/^(.*)[A-Za-z0-9\x{0080}-\x{FFFF}]$/u', $value) == 0) { |
| 49 |
return $value; |
| 50 |
} |
| 51 |
|
| 52 |
$plural = static::inflector()->pluralize($value); |
| 53 |
|
| 54 |
return static::matchCase($plural, $value); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function singular($value) |
| 64 |
{ |
| 65 |
$singular = static::inflector()->singularize($value); |
| 66 |
|
| 67 |
return static::matchCase($singular, $value); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
protected static function uncountable($value) |
| 77 |
{ |
| 78 |
return in_array(strtolower($value), static::$uncountable); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected static function matchCase($value, $comparison) |
| 89 |
{ |
| 90 |
$functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords']; |
| 91 |
|
| 92 |
foreach ($functions as $function) { |
| 93 |
if ($function($comparison) === $comparison) { |
| 94 |
return $function($value); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $value; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static function inflector() |
| 107 |
{ |
| 108 |
if (is_null(static::$inflector)) { |
| 109 |
static::$inflector = InflectorFactory::createForLanguage(static::$language)->build(); |
| 110 |
} |
| 111 |
|
| 112 |
return static::$inflector; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static function useLanguage(string $language) |
| 122 |
{ |
| 123 |
static::$language = $language; |
| 124 |
|
| 125 |
static::$inflector = null; |
| 126 |
} |
| 127 |
} |