1- <?php
2- /**
3- * @package ActiveRecord
4- */
5- namespace ActiveRecord ;
6-
7- /**
8- * @package ActiveRecord
9- */
10- abstract class Inflector
11- {
12- /**
13- * Get an instance of the {@link Inflector} class.
14- *
15- * @return object
16- */
17- public static function instance ()
18- {
19- return new StandardInflector ();
20- }
21-
22- /**
23- * Turn a string into its camelized version.
24- *
25- * @param string $s string to convert
26- * @return string
27- */
28- public function camelize ($ s )
29- {
30- $ s = preg_replace ('/[_-]+/ ' ,'_ ' ,trim ($ s ));
31- $ s = str_replace (' ' , '_ ' , $ s );
32-
33- $ camelized = '' ;
34-
35- for ($ i =0 ,$ n =strlen ($ s ); $ i <$ n ; ++$ i )
36- {
37- if ($ s [$ i ] == '_ ' && $ i +1 < $ n )
38- $ camelized .= strtoupper ($ s [++$ i ]);
39- else
40- $ camelized .= $ s [$ i ];
41- }
42-
43- $ camelized = trim ($ camelized ,' _ ' );
44-
45- if (strlen ($ camelized ) > 0 )
46- $ camelized [0 ] = strtolower ($ camelized [0 ]);
47-
48- return $ camelized ;
49- }
50-
51- /**
52- * Determines if a string contains all uppercase characters.
53- *
54- * @param string $s string to check
55- * @return bool
56- */
57- public static function is_upper ($ s )
58- {
59- return (strtoupper ($ s ) === $ s );
60- }
61-
62- /**
63- * Determines if a string contains all lowercase characters.
64- *
65- * @param string $s string to check
66- * @return bool
67- */
68- public static function is_lower ($ s )
69- {
70- return (strtolower ($ s ) === $ s );
71- }
72-
73- /**
74- * Convert a camelized string to a lowercase, underscored string.
75- *
76- * @param string $s string to convert
77- * @return string
78- */
79- public function uncamelize ($ s )
80- {
81- $ normalized = '' ;
82-
83- for ($ i =0 ,$ n =strlen ($ s ); $ i <$ n ; ++$ i )
84- {
85- if (ctype_alpha ($ s [$ i ]) && self ::is_upper ($ s [$ i ]))
86- $ normalized .= '_ ' . strtolower ($ s [$ i ]);
87- else
88- $ normalized .= $ s [$ i ];
89- }
90- return trim ($ normalized ,' _ ' );
91- }
92-
93- /**
94- * Convert a string with space into a underscored equivalent.
95- *
96- * @param string $s string to convert
97- * @return string
98- */
99- public function underscorify ($ s )
100- {
101- return preg_replace (array ('/[_\- ]+/ ' ,'/([a-z])([A-Z])/ ' ),array ('_ ' ,'\\1_ \\2 ' ),trim ($ s ));
102- }
103-
104- public function keyify ($ class_name )
105- {
106- return strtolower ($ this ->underscorify (denamespace ($ class_name ))) . '_id ' ;
107- }
108-
109- abstract function variablize ($ s );
110- }
111-
112- /**
113- * @package ActiveRecord
114- */
115- class StandardInflector extends Inflector
116- {
117- public function tableize ($ s ) { return Utils::pluralize (strtolower ($ this ->underscorify ($ s ))); }
118- public function variablize ($ s ) { return str_replace (array ('- ' ,' ' ),array ('_ ' ,'_ ' ),strtolower ( trim ($ s) )); }
119- }
1+ <?php
2+ /**
3+ * @package ActiveRecord
4+ */
5+ namespace ActiveRecord ;
6+
7+ /**
8+ * @package ActiveRecord
9+ */
10+ abstract class Inflector
11+ {
12+ /**
13+ * Get an instance of the {@link Inflector} class.
14+ *
15+ * @return object
16+ */
17+ public static function instance ()
18+ {
19+ return new StandardInflector ();
20+ }
21+
22+ /**
23+ * Turn a string into its camelized version.
24+ *
25+ * @param string $s string to convert
26+ * @return string
27+ */
28+ public function camelize ($ s )
29+ {
30+ $ s = preg_replace ('/[_-]+/ ' ,'_ ' ,trim ($ s ));
31+ $ s = str_replace (' ' , '_ ' , $ s );
32+
33+ $ camelized = '' ;
34+
35+ for ($ i =0 ,$ n =strlen ($ s ); $ i <$ n ; ++$ i )
36+ {
37+ if ($ s [$ i ] == '_ ' && $ i +1 < $ n )
38+ $ camelized .= strtoupper ($ s [++$ i ]);
39+ else
40+ $ camelized .= $ s [$ i ];
41+ }
42+
43+ $ camelized = trim ($ camelized ,' _ ' );
44+
45+ if (strlen ($ camelized ) > 0 )
46+ $ camelized [0 ] = strtolower ($ camelized [0 ]);
47+
48+ return $ camelized ;
49+ }
50+
51+ /**
52+ * Determines if a string contains all uppercase characters.
53+ *
54+ * @param string $s string to check
55+ * @return bool
56+ */
57+ public static function is_upper ($ s )
58+ {
59+ return (strtoupper ($ s ) === $ s );
60+ }
61+
62+ /**
63+ * Determines if a string contains all lowercase characters.
64+ *
65+ * @param string $s string to check
66+ * @return bool
67+ */
68+ public static function is_lower ($ s )
69+ {
70+ return (strtolower ($ s ) === $ s );
71+ }
72+
73+ /**
74+ * Convert a camelized string to a lowercase, underscored string.
75+ *
76+ * @param string $s string to convert
77+ * @return string
78+ */
79+ public function uncamelize ($ s )
80+ {
81+ $ normalized = '' ;
82+
83+ for ($ i =0 ,$ n =strlen ($ s ); $ i <$ n ; ++$ i )
84+ {
85+ if (ctype_alpha ($ s [$ i ]) && self ::is_upper ($ s [$ i ]))
86+ $ normalized .= '_ ' . strtolower ($ s [$ i ]);
87+ else
88+ $ normalized .= $ s [$ i ];
89+ }
90+ return trim ($ normalized ,' _ ' );
91+ }
92+
93+ /**
94+ * Convert a string with space into a underscored equivalent.
95+ *
96+ * @param string $s string to convert
97+ * @return string
98+ */
99+ public function underscorify ($ s )
100+ {
101+ return preg_replace (array ('/[_\- ]+/ ' ,'/([a-z])([A-Z])/ ' ),array ('_ ' ,'\\1_ \\2 ' ),trim ($ s ));
102+ }
103+
104+ public function keyify ($ class_name )
105+ {
106+ return strtolower ($ this ->underscorify (denamespace ($ class_name ))) . '_id ' ;
107+ }
108+
109+ abstract function variablize ($ s );
110+ }
111+
112+ /**
113+ * @package ActiveRecord
114+ */
115+ class StandardInflector extends Inflector
116+ {
117+ public function tableize ($ s ) { return Utils::pluralize (strtolower ($ this ->underscorify ($ s ))); }
118+ public function variablize ($ s ) { return str_replace (array ('- ' ,' ' ),array ('_ ' ,'_ ' ),trim ($ s )); }
119+ }
0 commit comments