3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
// CodeIgniter Inflector Helpers
16
if (! function_exists('singular')) {
20
* Takes a plural word and makes it singular
22
* @param string $string Input string
24
function singular(string $string): string
28
if (! is_pluralizable($result)) {
34
'/(matr)ices$/' => '\1ix',
35
'/(vert|ind)ices$/' => '\1ex',
37
'/(alias)es$/' => '\1',
38
'/([octop|vir])i$/' => '\1us',
39
'/(cris|ax|test)es$/' => '\1is',
42
'/(bus|campus)es$/' => '\1',
43
'/([m|l])ice$/' => '\1ouse',
44
'/(x|ch|ss|sh)es$/' => '\1',
45
'/(m)ovies$/' => '\1\2ovie',
46
'/(s)eries$/' => '\1\2eries',
47
'/([^aeiouy]|qu)ies$/' => '\1y',
48
'/([lr])ves$/' => '\1f',
51
'/([^f])ves$/' => '\1fe',
52
'/(^analy)ses$/' => '\1sis',
53
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
54
'/([ti])a$/' => '\1um',
55
'/(p)eople$/' => '\1\2erson',
57
'/(s)tatuses$/' => '\1\2tatus',
58
'/(c)hildren$/' => '\1\2hild',
59
'/(n)ews$/' => '\1\2ews',
60
'/(quiz)zes$/' => '\1',
61
'/([^us])s$/' => '\1',
64
foreach ($singularRules as $rule => $replacement) {
65
if (preg_match($rule, $result)) {
66
$result = preg_replace($rule, $replacement, $result);
75
if (! function_exists('plural')) {
79
* Takes a singular word and makes it plural
81
* @param string $string Input string
83
function plural(string $string): string
87
if (! is_pluralizable($result)) {
92
'/(quiz)$/' => '\1zes', // quizzes
93
'/^(ox)$/' => '\1\2en', // ox
94
'/([m|l])ouse$/' => '\1ice', // mouse, louse
95
'/(matr|vert|ind)ix|ex$/' => '\1ices', // matrix, vertex, index
96
'/(x|ch|ss|sh)$/' => '\1es', // search, switch, fix, box, process, address
97
'/([^aeiouy]|qu)y$/' => '\1ies', // query, ability, agency
98
'/(hive)$/' => '\1s', // archive, hive
99
'/(?:([^f])fe|([lr])f)$/' => '\1\2ves', // half, safe, wife
100
'/sis$/' => 'ses', // basis, diagnosis
101
'/([ti])um$/' => '\1a', // datum, medium
102
'/(p)erson$/' => '\1eople', // person, salesperson
103
'/(m)an$/' => '\1en', // man, woman, spokesman
104
'/(c)hild$/' => '\1hildren', // child
105
'/(buffal|tomat)o$/' => '\1\2oes', // buffalo, tomato
106
'/(bu|campu)s$/' => '\1\2ses', // bus, campus
107
'/(alias|status|virus)$/' => '\1es', // alias
108
'/(octop)us$/' => '\1i', // octopus
109
'/(ax|cris|test)is$/' => '\1es', // axis, crisis
110
'/s$/' => 's', // no change (compatibility)
114
foreach ($pluralRules as $rule => $replacement) {
115
if (preg_match($rule, $result)) {
116
$result = preg_replace($rule, $replacement, $result);
125
if (! function_exists('counted')) {
129
* Takes a number and a word to return the plural or not
130
* E.g. 0 cats, 1 cat, 2 cats, ...
132
* @param int $count Number of items
133
* @param string $string Input string
135
function counted(int $count, string $string): string
137
$result = "{$count} ";
139
return $result . ($count === 1 ? singular($string) : plural($string));
143
if (! function_exists('camelize')) {
147
* Takes multiple words separated by spaces or
148
* underscores and converts them to camel case.
150
* @param string $string Input string
152
function camelize(string $string): string
154
return lcfirst(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $string))));
158
if (! function_exists('pascalize')) {
162
* Takes multiple words separated by spaces or
163
* underscores and converts them to Pascal case,
164
* which is camel case with an uppercase first letter.
166
* @param string $string Input string
168
function pascalize(string $string): string
170
return ucfirst(camelize($string));
174
if (! function_exists('underscore')) {
178
* Takes multiple words separated by spaces and underscores them
180
* @param string $string Input string
182
function underscore(string $string): string
184
$replacement = trim($string);
186
return preg_replace('/[\s]+/', '_', $replacement);
190
if (! function_exists('decamelize')) {
194
* Takes multiple words separated by camel case and
197
* @param string $string Input string
199
function decamelize(string $string): string
201
return strtolower(preg_replace(['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', trim($string)));
205
if (! function_exists('humanize')) {
209
* Takes multiple words separated by the separator,
210
* camelizes and changes them to spaces
212
* @param string $string Input string
213
* @param string $separator Input separator
215
function humanize(string $string, string $separator = '_'): string
217
$replacement = trim($string);
219
return ucwords(preg_replace('/[' . $separator . ']+/', ' ', $replacement));
223
if (! function_exists('is_pluralizable')) {
225
* Checks if the given word has a plural version.
227
* @param string $word Word to check
229
function is_pluralizable(string $word): bool
231
$uncountables = in_array(
284
return ! $uncountables;
288
if (! function_exists('dasherize')) {
290
* Replaces underscores with dashes in the string.
292
* @param string $string Input string
294
function dasherize(string $string): string
296
return str_replace('_', '-', $string);
300
if (! function_exists('ordinal')) {
302
* Returns the suffix that should be added to a
303
* number to denote the position in an ordered
304
* sequence such as 1st, 2nd, 3rd, 4th.
306
* @param int $integer The integer to determine the suffix
308
function ordinal(int $integer): string
323
return $integer % 100 >= 11 && $integer % 100 <= 13 ? 'th' : $suffixes[$integer % 10];
327
if (! function_exists('ordinalize')) {
329
* Turns a number into an ordinal string used
330
* to denote the position in an ordered sequence
331
* such as 1st, 2nd, 3rd, 4th.
333
* @param int $integer The integer to ordinalize
335
function ordinalize(int $integer): string
337
return $integer . ordinal($integer);