Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.doctrine-project.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 several different php projects and several different authors. The
27
 * original author names and emails are not known
28
 *
29
 * @package     Doctrine
30
 * @subpackage  Inflector
31
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
32
 * @link        www.doctrine-project.org
33
 * @since       1.0
34
 * @version     $Revision: 3189 $
35
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
36
 * @author      Jonathan H. Wage <jonwage@gmail.com>
37
 */
38
class Doctrine_Inflector
39
{
40
    /**
41
     * Convert word in to the format for a Doctrine table name. Converts 'ModelName' to 'model_name'
42
     *
43
     * @param  string $word  Word to tableize
44
     * @return string $word  Tableized word
45
     */
46
    public static function tableize($word)
47
    {
48
        return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word));
49
    }
50
 
51
    /**
52
     * Convert a word in to the format for a Doctrine class name. Converts 'table_name' to 'TableName'
53
     *
54
     * @param string  $word  Word to classify
55
     * @return string $word  Classified word
56
     */
57
    public static function classify($word)
58
    {
59
        static $cache = array();
60
 
61
        if (!isset($cache[$word])) {
62
            $word = preg_replace('/[$]/', '', $word);
63
            $classify = preg_replace_callback('~(_?)([-_])([\w])~', array("Doctrine_Inflector", "classifyCallback"), ucfirst(strtolower($word)));
64
            $cache[$word] = $classify;
65
        }
66
        return $cache[$word];
67
    }
68
 
69
    /**
70
     * Callback function to classify a classname properly.
71
     *
72
     * @param  array  $matches  An array of matches from a pcre_replace call
73
     * @return string $string   A string with matches 1 and mathces 3 in upper case.
74
     */
75
    public static function classifyCallback($matches)
76
    {
77
        return $matches[1] . strtoupper($matches[3]);
78
    }
79
 
80
    /**
81
     * Check if a string has utf7 characters in it
82
     *
83
     * By bmorel at ssi dot fr
84
     *
85
     * @param  string $string
86
     * @return boolean $bool
87
     */
88
    public static function seemsUtf8($string)
89
    {
90
      for ($i = 0; $i < strlen($string); $i++) {
91
        if (ord($string[$i]) < 0x80) continue; # 0bbbbbbb
92
        elseif ((ord($string[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
93
        elseif ((ord($string[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
94
        elseif ((ord($string[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
95
        elseif ((ord($string[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
96
        elseif ((ord($string[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
97
        else return false; # Does not match any model
98
        for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
99
          if ((++$i == strlen($string)) || ((ord($string[$i]) & 0xC0) != 0x80))
100
          return false;
101
        }
102
      }
103
      return true;
104
    }
105
 
106
    /**
107
     * Remove any illegal characters, accents, etc.
108
     *
109
     * @param  string $string  String to unaccent
110
     * @return string $string  Unaccented string
111
     */
112
    public static function unaccent($string)
113
    {
114
        if ( ! preg_match('/[\x80-\xff]/', $string) ) {
115
          return $string;
116
      }
117
 
118
        if (self::seemsUtf8($string)) {
119
          $chars = array(
120
          // Decompositions for Latin-1 Supplement
121
          chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
122
          chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
123
          chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
124
          chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
125
          chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
126
          chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
127
          chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
128
          chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
129
          chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
130
          chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
131
          chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
132
          chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
133
          chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
134
          chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
135
          chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
136
          chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
137
          chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
138
          chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
139
          chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
140
          chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
141
          chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
142
          chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
143
          chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
144
          chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
145
          chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
146
          chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
147
          chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
148
          chr(195).chr(191) => 'y',
149
          // Decompositions for Latin Extended-A
150
          chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
151
          chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
152
          chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
153
          chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
154
          chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
155
          chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
156
          chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
157
          chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
158
          chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
159
          chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
160
          chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
161
          chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
162
          chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
163
          chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
164
          chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
165
          chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
166
          chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
167
          chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
168
          chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
169
          chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
170
          chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
171
          chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
172
          chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
173
          chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
174
          chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
175
          chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
176
          chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
177
          chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
178
          chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
179
          chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
180
          chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
181
          chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
182
          chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
183
          chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
184
          chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
185
          chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
186
          chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
187
          chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
188
          chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
189
          chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
190
          chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
191
          chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
192
          chr(197).chr(148) => 'R', chr(197).chr(149) => 'r',
193
          chr(197).chr(150) => 'R', chr(197).chr(151) => 'r',
194
          chr(197).chr(152) => 'R', chr(197).chr(153) => 'r',
195
          chr(197).chr(154) => 'S', chr(197).chr(155) => 's',
196
          chr(197).chr(156) => 'S', chr(197).chr(157) => 's',
197
          chr(197).chr(158) => 'S', chr(197).chr(159) => 's',
198
          chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
199
          chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
200
          chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
201
          chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
202
          chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
203
          chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
204
          chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
205
          chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
206
          chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
207
          chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
208
          chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
209
          chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
210
          chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
211
          chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
212
          chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
213
          chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
214
          // Euro Sign
215
          chr(226).chr(130).chr(172) => 'E',
216
          // GBP (Pound) Sign
217
          chr(194).chr(163) => '',
218
          'Ä' => 'Ae', 'ä' => 'ae', 'Ü' => 'Ue', 'ü' => 'ue',
219
          'Ö' => 'Oe', 'ö' => 'oe', 'ß' => 'ss',
220
          // Norwegian characters
221
          'Å'=>'Aa','Æ'=>'Ae','Ø'=>'O','æ'=>'a','ø'=>'o','å'=>'aa'
222
          );
223
 
224
          $string = strtr($string, $chars);
225
        } else {
226
          // Assume ISO-8859-1 if not UTF-8
227
          $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
228
            .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
229
            .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
230
            .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
231
            .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
232
            .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
233
            .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
234
            .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
235
            .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
236
            .chr(252).chr(253).chr(255);
237
 
238
          $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
239
 
240
          $string = strtr($string, $chars['in'], $chars['out']);
241
          $doubleChars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
242
          $doubleChars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
243
          $string = str_replace($doubleChars['in'], $doubleChars['out'], $string);
244
        }
245
 
246
        return $string;
247
    }
248
 
249
    /**
250
     * Convert any passed string to a url friendly string. Converts 'My first blog post' to 'my-first-blog-post'
251
     *
252
     * @param  string $text  Text to urlize
253
     * @return string $text  Urlized text
254
     */
255
    public static function urlize($text)
256
    {
257
        // Remove all non url friendly characters with the unaccent function
258
        $text = self::unaccent($text);
259
 
260
        if (function_exists('mb_strtolower'))
261
        {
262
            $text = mb_strtolower($text);
263
        } else {
264
            $text = strtolower($text);
265
        }
266
 
267
        // Remove all none word characters
268
        $text = preg_replace('/\W/', ' ', $text);
269
 
270
        // More stripping. Replace spaces with dashes
271
        $text = strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', '-',
272
                           preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
273
                           preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
274
                           preg_replace('/::/', '/', $text)))));
275
 
276
        return trim($text, '-');
277
    }
278
}