/
Inflector.class.php
127 lines (119 loc) · 3.17 KB
/
Inflector.class.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
<?php
/**
* The inflector provides basic functionality for transforming words
* between their singular and plural forms, as well as programmatic forms
* (i.e. camelCapsFormat, ProperCapsFormat, under_scores_format)
*
* @author Kris Jordan <krisjordan@gmail.com>
* @copyright 2008, 2009 Kris Jordan
* @package Recess PHP Framework
* @license MIT
* @link http://www.recessframework.org/
*/
class Inflector {
/**
* Return the plural form of an english word, in many cases.
* Currently this is naive and only appends an 's' to the end of a word.
* i.e. person => persons
* thing => things
* goose => gooses
*
* @param string $word
* @return string
*/
public static function toPlural($word) {
return $word .= 's';
}
/**
* Return the singular form of an english word, in many cases.
* Currently this is naive and only removes the last character
* of a string.
*
* i.e. persons => persons
* things => thing
* oxen => oxe
*
* @param string $word
* @return string
*/
public static function toSingular($word) {
return substr($word, 0, -1);
}
/**
* Return whether or not an english word is plural. Currently
* naive and only returns whether or not the last character
* is an 's' or not.
*
* @param string $word
* @return string
*/
public static function isPlural($word) {
return substr($word, -1, 1) === 's';
}
/**
* Go from underscores_form or camelCapsForm to ProperCapsForm.
*
* i.e. this_is_in_underscores => ThisIsInUnderscores
* helloWorld => HelloWorld
*
* @param string $word in camelCaps or underscores_form.
* @return string in ProperCapsForm
*/
public static function toProperCaps($word) {
return ucfirst(self::toCamelCaps($word));
}
/**
* Go from underscores_form or ProperCapsForm to camelCapsForm
*
* i.e. this_is_in_underscores => thisIsInUnderscores
* HelloWorld => helloWorld
*
* @param string $word in ProperCapsForm or underscores_form
* @return string camelCapsForm
*/
public static function toCamelCaps($word) {
$word = preg_replace_callback(
'/_([a-z])/',
create_function(
'$matches',
'return ucfirst($matches[1]);'
),
$word);
$word[0] = strtolower($word[0]);
return $word;
}
/**
* Go from ProperCapsForm or camelCapsForm to underscores_form
*
* @param string $word in camelCapsForm or ProperCapsForm
* @return string underscores_form
*/
public static function toUnderscores($word) {
$word[0] = strtolower($word[0]);
$word = preg_replace_callback(
'/([A-Z])/',
create_function(
'$matches',
'return \'_\' . strtolower($matches[0]);'
),
$word);
return $word;
}
/**
* Go from underscores_form or camelCapsForm or ProperCapsForm to English Form.
*
* @param string $word in underscores_form or camelCapsForm or ProperCapsForm
* @return string in English Form
*/
public static function toEnglish($word) {
$word = preg_replace_callback(
'/(?:(^.)|_([a-z])|([A-Z]))/',
create_function(
'$matches',
'return \' \' . strtoupper(array_pop($matches));'
),
$word);
return substr($word, 1);
}
}
?>