Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Pluralizer.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Doctrine\Inflector\InflectorFactory;
6
7 class Pluralizer
8 {
9 /**
10 * The cached inflector instance.
11 *
12 * @var static
13 */
14 protected static $inflector;
15
16 /**
17 * The language that should be used by the inflector.
18 *
19 * @var string
20 */
21 protected static $language = 'english';
22
23 /**
24 * Uncountable non-nouns word forms.
25 *
26 * Contains words supported by Doctrine/Inflector/Rules/English/Uninflected.php
27 *
28 * @var string[]
29 */
30 public static $uncountable = [
31 'recommended',
32 'related',
33 ];
34
35 /**
36 * Get the plural form of an English word.
37 *
38 * @param string $value
39 * @param int|array|\Countable $count
40 * @return string
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 * Get the singular form of an English word.
59 *
60 * @param string $value
61 * @return string
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 * Determine if the given value is uncountable.
72 *
73 * @param string $value
74 * @return bool
75 */
76 protected static function uncountable($value)
77 {
78 return in_array(strtolower($value), static::$uncountable);
79 }
80
81 /**
82 * Attempt to match the case on two strings.
83 *
84 * @param string $value
85 * @param string $comparison
86 * @return string
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 * Get the inflector instance.
103 *
104 * @return \Doctrine\Inflector\Inflector
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 * Specify the language that should be used by the inflector.
117 *
118 * @param string $language
119 * @return void
120 */
121 public static function useLanguage(string $language)
122 {
123 static::$language = $language;
124
125 static::$inflector = null;
126 }
127 }