|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "RocketORM" package. |
|
5
|
|
|
* |
|
6
|
|
|
* https://github.com/RocketORM/ORM |
|
7
|
|
|
* |
|
8
|
|
|
* For the full license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Rocket\ORM\Generator\Utils; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Sylvain Lorinet <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class StringUtil |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $string The string |
|
21
|
|
|
* @param bool $upper True if the first letter is uppercase, false otherwise |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
8 |
|
public static function camelize($string, $upper = true) |
|
26
|
|
|
{ |
|
27
|
8 |
|
$camelize = strtr(ucwords(strtr($string, array('_' => ' ', '.' => ' ', '\\' => ' '))), array(' ' => '')); |
|
28
|
8 |
|
if ($upper) { |
|
29
|
8 |
|
return $camelize; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
8 |
|
return lcfirst($camelize); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Pluralizes English noun. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $word english noun to pluralize |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
* |
|
42
|
|
|
* @throws \LogicException |
|
43
|
|
|
* |
|
44
|
|
|
* @see https://github.com/whiteoctober/RestBundle/blob/master/Pluralization/Pluralization.php |
|
45
|
|
|
* @codeCoverageIgnore |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function pluralize($word) |
|
48
|
|
|
{ |
|
49
|
|
|
static $plurals = [ |
|
50
|
|
|
'/(quiz)$/i' => '\1zes', |
|
51
|
|
|
'/^(ox)$/i' => '\1en', |
|
52
|
|
|
'/([m|l])ouse$/i' => '\1ice', |
|
53
|
|
|
'/(matr|vert|ind)ix|ex$/i' => '\1ices', |
|
54
|
|
|
'/(x|ch|ss|sh)$/i' => '\1es', |
|
55
|
|
|
'/([^aeiouy]|qu)ies$/i' => '\1y', |
|
56
|
|
|
'/([^aeiouy]|qu)y$/i' => '\1ies', |
|
57
|
|
|
'/(hive)$/i' => '\1s', |
|
58
|
|
|
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', |
|
59
|
|
|
'/sis$/i' => 'ses', |
|
60
|
|
|
'/([ti])um$/i' => '\1a', |
|
61
|
|
|
'/(buffal|tomat)o$/i' => '\1oes', |
|
62
|
|
|
'/(bu)s$/i' => '\1ses', |
|
63
|
|
|
'/(alias|status)/i' => '\1es', |
|
64
|
|
|
'/(octop|vir)us$/i' => '\1i', |
|
65
|
|
|
'/(ax|test)is$/i' => '\1es', |
|
66
|
|
|
'/s$/i' => 's', |
|
67
|
|
|
'/$/' => 's' |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
static $uncountables = [ |
|
71
|
|
|
'equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep' |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
static $irregulars = [ |
|
75
|
|
|
'person' => 'people', |
|
76
|
|
|
'man' => 'men', |
|
77
|
|
|
'child' => 'children', |
|
78
|
|
|
'sex' => 'sexes', |
|
79
|
|
|
'move' => 'moves' |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
$lowerCasedWord = strtolower($word); |
|
83
|
|
|
foreach ($uncountables as $uncountable) { |
|
84
|
|
|
if ($uncountable == substr($lowerCasedWord, (-1 * strlen($uncountable)))) { |
|
85
|
|
|
return $word; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
foreach ($irregulars as $plural => $singular) { |
|
90
|
|
|
if (preg_match('/(' . $plural . ')$/i', $word, $arr)) { |
|
91
|
|
|
return preg_replace('/(' . $plural . ')$/i', substr($arr[0], 0, 1) . substr($singular, 1), $word); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
foreach ($plurals as $rule => $replacement) { |
|
96
|
|
|
if (preg_match($rule, $word)) { |
|
97
|
|
|
return preg_replace($rule, $replacement, $word); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
throw new \LogicException('Unknown plural for word "' . $word . '"'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|