-
Notifications
You must be signed in to change notification settings - Fork 1
/
Inflect.php
161 lines (142 loc) · 5.01 KB
/
Inflect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Inflect;
/**
* Handle words in Portuguese
*
* @package Inflect
*/
class Inflect
{
/**
* Declare plural words in lowercase
*
* @var string
**/
static $plural = array(
'/(s)$/i' => '$1',
'/^(paí)s$/i' => '$1ses',
'/(z|r)$/i' => '$1es',
'/al$/i' => 'ais',
'/el$/i' => 'eis',
'/ol$/i' => 'ois',
'/ul$/i' => 'uis',
'/([^aeou])il$/i' => '$1is',
'/m$/i' => 'ns',
'/^(japon|escoc|ingl|dinamarqu|fregu|portugu)ês$/i' => '$1eses',
'/^(|g)ás$/i' => '$1ases',
'/ão$/i' => 'ões',
'/^(irm|m)ão$/i' => '$1ãos',
'/^(alem|c|p)ão$/i' => '$1ães',
'/ao$/i' => 'oes',
'/^(irm|m)ao$/i' => '\1aos',
'/^(alem|c|p)ao$/i' => '\1aes',
'/$/' => 's',
);
/**
* Declare singular words in lowercase
*
* @var string
**/
static $singular = array(
'/^(á|gá|paí)s$/i' => '$1s',
'/(r|z)es$/i' => '$1',
'/([^p])ais$/i' => '$1al',
'/eis$/i' => 'el',
'/ois$/i' => 'ol',
'/uis$/i' => 'ul',
'/(r|t|f|v)is$/i' => '$1il',
'/ns$/i' => 'm',
'/sses$/i' => 'sse',
'/^(.*[^s]s)es$/i' => '$1',
'/ães$/i' => 'ão',
'/aes$/i' => 'ao',
'/ãos$/i' => 'ão',
'/aos$/i' => 'ao',
'/ões$/i' => 'ão',
'/oes$/i' => 'ao',
'/(japon|escoc|ingl|dinamarqu|fregu|portugu)eses$/i' => '$1ês',
'/^(g|)ases$/i' => '$1ás',
'/([^ê])s$/i' => '$1'
);
/**
* Declare irregular word
*
* @var string
**/
static $irregular = array(
'país' => 'países'
);
/**
* Declare uncountable words
*
* @var string
**/
static $uncountable = array(
'tórax',
'tênis',
'ônibus',
'lápis',
'fênix'
);
/**
* Pluralize word
*
* @param string
* @return string
**/
public function pluralize($string)
{
if (in_array(strtolower($string), self::$uncountable)) {
return $string;
}
foreach (self::$irregular as $pattern => $result) {
$pattern = '/' . $pattern . '$/i';
if (preg_match($pattern, $string)) {
return preg_replace($pattern, $result, $string);
}
}
foreach (self::$plural as $pattern => $result) {
if (preg_match($pattern, $result)) {
return preg_replace($pattern, $result, $string);
}
}
return $string;
}
/**
* Singularize word
*
* @param string
* @return string
**/
public function singularize($string)
{
if (in_array(strtolower($string), self::$uncountable)) {
return $string;
}
foreach (self::$irregular as $result => $pattern) {
$pattern = '/' . $pattern . '$/i';
if (preg_match($pattern, $string)) {
return preg_replace($pattern, $result, $string);
}
}
foreach (self::$singular as $pattern => $result) {
if (preg_match($pattern, $string)) {
return preg_replace($pattern, $result, $string);
}
}
return $string;
}
/**
* Camelize word
*
* @param string
* @return string
**/
public function camelize($string, $uppercase_first_letter = false)
{
if ($uppercase_first_letter) {
return str_replace(' ', '', ucwords(strtr($string, '_-', ' ')));
}
return str_replace(' ', '', lcfirst(strtr($string, '_-', ' ')));
}
}