Coverage for Doctrine_Inflector

Back to coverage report

1 <?php
2 /*
3  *  $Id: Inflector.php 3189 2007-11-18 20:37:44Z meus $
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the LGPL. For more information, see
19  * <http://www.phpdoctrine.org>.
20  */
21
22 /**
23  * Doctrine_Inflector has static methods for inflecting text
24  * 
25  * The methods in these classes are from several different sources collected
26  * across the internet through php development for several years.
27  * They have been updated and modified a little bit for Doctrine but are mainly untouched.
28  *
29  * @package     Doctrine
30  * @subpackage  Inflector
31  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
32  * @link        www.phpdoctrine.org
33  * @since       1.0
34  * @version     $Revision: 3189 $
35  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
36  */
37 class Doctrine_Inflector
38 {
39     /**
40     * pluralize
41     *
42     * @param    string $word English noun to pluralize
43     * @return   string Plural noun
44     */
45     public static function pluralize($word)
46     {
47         $plural = array('/(quiz)$/i' => '\1zes',
48                         '/^(ox)$/i' => '\1en',
49                         '/([m|l])ouse$/i' => '\1ice',
50                         '/(matr|vert|ind)ix|ex$/i' => '\1ices',
51                         '/(x|ch|ss|sh)$/i' => '\1es',
52                         '/([^aeiouy]|qu)ies$/i' => '\1y',
53                         '/([^aeiouy]|qu)y$/i' => '\1ies',
54                         '/(hive)$/i' => '\1s',
55                         '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
56                         '/sis$/i' => 'ses',
57                         '/([ti])um$/i' => '\1a',
58                         '/(buffal|tomat)o$/i' => '\1oes',
59                         '/(bu)s$/i' => '\1ses',
60                         '/(alias|status)/i' => '\1es',
61                         '/(octop|vir)us$/i' => '\1i',
62                         '/(ax|test)is$/i' => '\1es',
63                         '/s$/i' => 's',
64                         '/$/' => 's');
65
66         $uncountable = array('equipment',
67                              'information',
68                              'rice',
69                              'money',
70                              'species',
71                              'series',
72                              'fish',
73                              'sheep');
74
75         $irregular = array('person' => 'people',
76                            'man'    => 'men',
77                            'child'  => 'children',
78                            'sex'    => 'sexes',
79                            'move'   => 'moves');
80
81         $lowercasedWord = strtolower($word);
82
83         foreach ($uncountable as $_uncountable) {
84             if(substr($lowercasedWord, (-1 * strlen($_uncountable))) == $_uncountable) {
85                 return $word;
86             }
87         }
88
89         foreach ($irregular 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 ($plural as $rule => $replacement) {
96             if (preg_match($rule, $word)) {
97                 return preg_replace($rule, $replacement, $word);
98             }
99         }
100
101         return false;
102     }
103
104     /**
105     * singularize
106     *
107     * @param    string    $word    English noun to singularize
108     * @return   string Singular noun.
109     */
110     public static function singularize($word)
111     {
112         $singular = array('/(quiz)zes$/i' => '\\1',
113                           '/(matr)ices$/i' => '\\1ix',
114                           '/(vert|ind)ices$/i' => '\\1ex',
115                           '/^(ox)en/i' => '\\1',
116                           '/(alias|status)es$/i' => '\\1',
117                           '/([octop|vir])i$/i' => '\\1us',
118                           '/(cris|ax|test)es$/i' => '\\1is',
119                           '/(shoe)s$/i' => '\\1',
120                           '/(o)es$/i' => '\\1',
121                           '/(bus)es$/i' => '\\1',
122                           '/([m|l])ice$/i' => '\\1ouse',
123                           '/(x|ch|ss|sh)es$/i' => '\\1',
124                           '/(m)ovies$/i' => '\\1ovie',
125                           '/(s)eries$/i' => '\\1eries',
126                           '/([^aeiouy]|qu)ies$/i' => '\\1y',
127                           '/([lr])ves$/i' => '\\1f',
128                           '/(tive)s$/i' => '\\1',
129                           '/(hive)s$/i' => '\\1',
130                           '/([^f])ves$/i' => '\\1fe',
131                           '/(^analy)ses$/i' => '\\1sis',
132                           '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\\1\\2sis',
133                           '/([ti])a$/i' => '\\1um',
134                           '/(n)ews$/i' => '\\1ews',
135                           '/s$/i' => '');
136
137         $uncountable = array('equipment',
138                              'information',
139                              'rice',
140                              'money',
141                              'species',
142                              'series',
143                              'fish',
144                              'sheep',
145                              'sms');
146
147         $irregular = array('person' => 'people',
148                            'man'    => 'men',
149                            'child'  => 'children',
150                            'sex'    => 'sexes',
151                            'move'   => 'moves');
152
153         $lowercasedWord = strtolower($word);
154         foreach ($uncountable as $_uncountable){
155             if(substr($lowercasedWord, ( -1 * strlen($_uncountable))) == $_uncountable){
156                 return $word;
157             }
158         }
159
160         foreach ($irregular as $_singular => $_plural) {
161             if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
162                 return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);
163             }
164         }
165
166         foreach ($singular as $rule => $replacement) {
167             if (preg_match($rule, $word)) {
168                 return preg_replace($rule, $replacement, $word);
169             }
170         }
171
172         return $word;
173     }
174
175     /**
176      * variablize
177      * 
178      * @param string $word 
179      * @return void
180      */
181     public static function variablize($word)
182     {
183         $word = self::camelize($word);
184
185         return strtolower($word[0]) . substr($word, 1);
186     }
187
188     /**
189      * tableize
190      *
191      * @param string $name
192      * @return void
193      */
194     public static function tableize($name)
195     {
196         // Would prefer this but it breaks unit tests. Forces the table underscore pattern
197         // return self::pluralize(self::underscore($name));
198         return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $name));
199     }
200
201     /**
202      * classify
203      *
204      * @param string $word
205      */
206     public static function classify($word)
207     {
208         return preg_replace_callback('~(_?)(_)([\w])~', array("Doctrine_Inflector", "classifyCallback"), ucfirst(strtolower($word)));
209     }
210
211     /**
212      * classifyCallback
213      *
214      * Callback function to classify a classname properly.
215      *
216      * @param array $matches An array of matches from a pcre_replace call
217      * @return string A string with matches 1 and mathces 3 in upper case.
218      */
219     public static function classifyCallback($matches)
220     {
221         return $matches[1] . strtoupper($matches[3]);
222     }
223
224     /**
225      * camelize
226      *
227      * @param string $word 
228      * @return void
229      */
230     public static function camelize($word)
231     {
232         if (preg_match_all('/\/(.?)/', $word, $got)) {
233             foreach ($got[1] as $k => $v){
234                 $got[1][$k] = '::' . strtoupper($v);
235             }
236
237             $word = str_replace($got[0], $got[1], $word);
238         }
239
240         return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9^:]+/', ' ', $word)));
241     }
242
243     /**
244      * seemsUtf8
245      *
246      * By bmorel at ssi dot fr
247      *
248      * @param string $str 
249      * @return void
250      */
251     public static function seemsUtf8($string)
252     {
253      for ($i = 0; $i < strlen($string); $i++) {
254      if (ord($string[$i]) < 0x80) continue; # 0bbbbbbb
255      elseif ((ord($string[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
256      elseif ((ord($string[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
257      elseif ((ord($string[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
258      elseif ((ord($string[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
259      elseif ((ord($string[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
260      else return false; # Does not match any model
261      for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
262      if ((++$i == strlen($string)) || ((ord($string[$i]) & 0xC0) != 0x80))
263      return false;
264      }
265      }
266      return true;
267     }
268     
269     /**
270      * unaccent
271      *
272      * @param string $string 
273      * @return void
274      */
275     public static function unaccent($string)
276     {
277        if ( ! preg_match('/[\x80-\xff]/', $string) ) {
278        return $string;
279    }
280
281        if (self::seemsUtf8($string)) {
282        $chars = array(
283        // Decompositions for Latin-1 Supplement
284        chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
285        chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
286        chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
287        chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
288        chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
289        chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
290        chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
291        chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
292        chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
293        chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
294        chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
295        chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
296        chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
297        chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
298        chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
299        chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
300        chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
301        chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
302        chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
303        chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
304        chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
305        chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
306        chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
307        chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
308        chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
309        chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
310        chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
311        chr(195).chr(191) => 'y',
312        // Decompositions for Latin Extended-A
313        chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
314        chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
315        chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
316        chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
317        chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
318        chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
319        chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
320        chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
321        chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
322        chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
323        chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
324        chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
325        chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
326        chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
327        chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
328        chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
329        chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
330        chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
331        chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
332        chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
333        chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
334        chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
335        chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
336        chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
337        chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
338        chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
339        chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
340        chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
341        chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
342        chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
343        chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
344        chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
345        chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
346        chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
347        chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
348        chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
349        chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
350        chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
351        chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
352        chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
353        chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
354        chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
355        chr(197).chr(148) => 'R', chr(197).chr(149) => 'r',
356        chr(197).chr(150) => 'R', chr(197).chr(151) => 'r',
357        chr(197).chr(152) => 'R', chr(197).chr(153) => 'r',
358        chr(197).chr(154) => 'S', chr(197).chr(155) => 's',
359        chr(197).chr(156) => 'S', chr(197).chr(157) => 's',
360        chr(197).chr(158) => 'S', chr(197).chr(159) => 's',
361        chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
362        chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
363        chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
364        chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
365        chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
366        chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
367        chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
368        chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
369        chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
370        chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
371        chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
372        chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
373        chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
374        chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
375        chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
376        chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
377        // Euro Sign
378        chr(226).chr(130).chr(172) => 'E',
379        // GBP (Pound) Sign
380        chr(194).chr(163) => '');
381
382        $string = strtr($string, $chars);
383        } else {
384        // Assume ISO-8859-1 if not UTF-8
385        $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
386        .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
387        .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
388        .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
389        .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
390        .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
391        .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
392        .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
393        .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
394        .chr(252).chr(253).chr(255);
395
396        $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
397
398        $string = strtr($string, $chars['in'], $chars['out']);
399        $doubleChars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
400        $doubleChars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
401        $string = str_replace($doubleChars['in'], $doubleChars['out'], $string);
402        }
403
404        return $string;
405     }
406
407     /**
408      * urlize
409      *
410      * @param string $text 
411      * @return void
412      */
413     public static function urlize($text)
414     {
415         // Remove all non url friendly characters with the unaccent function
416         $text = self::unaccent($text);
417         
418         // Remove all none word characters
419         $text = preg_replace('/\W/', ' ', $text);
420         
421         // More stripping. Replace spaces with dashes
422         $text = strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', '-',
423                            preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
424                            preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
425                            preg_replace('/::/', '/', $text)))));
426         
427         return trim($text, '-');
428     }
429
430     /**
431      * underscore
432      *
433      * @param string $word 
434      * @return void
435      */
436     public static function underscore($word)
437     {
438         return strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', '_',
439                preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
440                preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
441                preg_replace('/::/', '/', $word)))));
442     }
443 }