CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.4 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.4
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeNumber
  • CakeTime
  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Hash
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  * @link          http://cakephp.org CakePHP(tm) Project
 12:  * @package       Cake.Utility
 13:  * @since         CakePHP(tm) v 0.2.9
 14:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 15:  */
 16: 
 17: /**
 18:  * Pluralize and singularize English words.
 19:  *
 20:  * Inflector pluralizes and singularizes English nouns.
 21:  * Used by CakePHP's naming conventions throughout the framework.
 22:  *
 23:  * @package       Cake.Utility
 24:  * @link          http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
 25:  */
 26: class Inflector {
 27: 
 28: /**
 29:  * Plural inflector rules
 30:  *
 31:  * @var array
 32:  */
 33:     protected static $_plural = array(
 34:         'rules' => array(
 35:             '/(s)tatus$/i' => '\1\2tatuses',
 36:             '/(quiz)$/i' => '\1zes',
 37:             '/^(ox)$/i' => '\1\2en',
 38:             '/([m|l])ouse$/i' => '\1ice',
 39:             '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
 40:             '/(x|ch|ss|sh)$/i' => '\1es',
 41:             '/([^aeiouy]|qu)y$/i' => '\1ies',
 42:             '/(hive)$/i' => '\1s',
 43:             '/(?:([^f])fe|([lre])f)$/i' => '\1\2ves',
 44:             '/sis$/i' => 'ses',
 45:             '/([ti])um$/i' => '\1a',
 46:             '/(p)erson$/i' => '\1eople',
 47:             '/(m)an$/i' => '\1en',
 48:             '/(c)hild$/i' => '\1hildren',
 49:             '/(buffal|tomat)o$/i' => '\1\2oes',
 50:             '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
 51:             '/us$/i' => 'uses',
 52:             '/(alias)$/i' => '\1es',
 53:             '/(ax|cris|test)is$/i' => '\1es',
 54:             '/s$/' => 's',
 55:             '/^$/' => '',
 56:             '/$/' => 's',
 57:         ),
 58:         'uninflected' => array(
 59:             '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people'
 60:         ),
 61:         'irregular' => array(
 62:             'atlas' => 'atlases',
 63:             'beef' => 'beefs',
 64:             'brief' => 'briefs',
 65:             'brother' => 'brothers',
 66:             'cafe' => 'cafes',
 67:             'child' => 'children',
 68:             'cookie' => 'cookies',
 69:             'corpus' => 'corpuses',
 70:             'cow' => 'cows',
 71:             'ganglion' => 'ganglions',
 72:             'genie' => 'genies',
 73:             'genus' => 'genera',
 74:             'graffito' => 'graffiti',
 75:             'hoof' => 'hoofs',
 76:             'loaf' => 'loaves',
 77:             'man' => 'men',
 78:             'money' => 'monies',
 79:             'mongoose' => 'mongooses',
 80:             'move' => 'moves',
 81:             'mythos' => 'mythoi',
 82:             'niche' => 'niches',
 83:             'numen' => 'numina',
 84:             'occiput' => 'occiputs',
 85:             'octopus' => 'octopuses',
 86:             'opus' => 'opuses',
 87:             'ox' => 'oxen',
 88:             'penis' => 'penises',
 89:             'person' => 'people',
 90:             'sex' => 'sexes',
 91:             'soliloquy' => 'soliloquies',
 92:             'testis' => 'testes',
 93:             'trilby' => 'trilbys',
 94:             'turf' => 'turfs',
 95:             'potato' => 'potatoes',
 96:             'hero' => 'heroes',
 97:             'tooth' => 'teeth',
 98:             'goose' => 'geese',
 99:             'foot' => 'feet'
100:         )
101:     );
102: 
103: /**
104:  * Singular inflector rules
105:  *
106:  * @var array
107:  */
108:     protected static $_singular = array(
109:         'rules' => array(
110:             '/(s)tatuses$/i' => '\1\2tatus',
111:             '/^(.*)(menu)s$/i' => '\1\2',
112:             '/(quiz)zes$/i' => '\\1',
113:             '/(matr)ices$/i' => '\1ix',
114:             '/(vert|ind)ices$/i' => '\1ex',
115:             '/^(ox)en/i' => '\1',
116:             '/(alias)(es)*$/i' => '\1',
117:             '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
118:             '/([ftw]ax)es/i' => '\1',
119:             '/(cris|ax|test)es$/i' => '\1is',
120:             '/(shoe)s$/i' => '\1',
121:             '/(o)es$/i' => '\1',
122:             '/ouses$/' => 'ouse',
123:             '/([^a])uses$/' => '\1us',
124:             '/([m|l])ice$/i' => '\1ouse',
125:             '/(x|ch|ss|sh)es$/i' => '\1',
126:             '/(m)ovies$/i' => '\1\2ovie',
127:             '/(s)eries$/i' => '\1\2eries',
128:             '/([^aeiouy]|qu)ies$/i' => '\1y',
129:             '/(tive)s$/i' => '\1',
130:             '/(hive)s$/i' => '\1',
131:             '/(drive)s$/i' => '\1',
132:             '/([le])ves$/i' => '\1f',
133:             '/([^rfoa])ves$/i' => '\1fe',
134:             '/(^analy)ses$/i' => '\1sis',
135:             '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
136:             '/([ti])a$/i' => '\1um',
137:             '/(p)eople$/i' => '\1\2erson',
138:             '/(m)en$/i' => '\1an',
139:             '/(c)hildren$/i' => '\1\2hild',
140:             '/(n)ews$/i' => '\1\2ews',
141:             '/eaus$/' => 'eau',
142:             '/^(.*us)$/' => '\\1',
143:             '/s$/i' => ''
144:         ),
145:         'uninflected' => array(
146:             '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
147:         ),
148:         'irregular' => array(
149:             'foes' => 'foe',
150:         )
151:     );
152: 
153: /**
154:  * Words that should not be inflected
155:  *
156:  * @var array
157:  */
158:     protected static $_uninflected = array(
159:         'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
160:         'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
161:         'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
162:         'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
163:         'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
164:         'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
165:         'metadata', 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
166:         'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
167:         'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
168:         'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
169:         'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
170:         'Yengeese'
171:     );
172: 
173: /**
174:  * Default map of accented and special characters to ASCII characters
175:  *
176:  * @var array
177:  */
178:     protected static $_transliteration = array(
179:         '/ä|æ|ǽ/' => 'ae',
180:         '/ö|œ/' => 'oe',
181:         '/ü/' => 'ue',
182:         '/Ä/' => 'Ae',
183:         '/Ü/' => 'Ue',
184:         '/Ö/' => 'Oe',
185:         '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
186:         '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
187:         '/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
188:         '/ç|ć|ĉ|ċ|č/' => 'c',
189:         '/Ð|Ď|Đ/' => 'D',
190:         '/ð|ď|đ/' => 'd',
191:         '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
192:         '/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
193:         '/Ĝ|Ğ|Ġ|Ģ/' => 'G',
194:         '/ĝ|ğ|ġ|ģ/' => 'g',
195:         '/Ĥ|Ħ/' => 'H',
196:         '/ĥ|ħ/' => 'h',
197:         '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I',
198:         '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i',
199:         '/Ĵ/' => 'J',
200:         '/ĵ/' => 'j',
201:         '/Ķ/' => 'K',
202:         '/ķ/' => 'k',
203:         '/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L',
204:         '/ĺ|ļ|ľ|ŀ|ł/' => 'l',
205:         '/Ñ|Ń|Ņ|Ň/' => 'N',
206:         '/ñ|ń|ņ|ň|ʼn/' => 'n',
207:         '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O',
208:         '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o',
209:         '/Ŕ|Ŗ|Ř/' => 'R',
210:         '/ŕ|ŗ|ř/' => 'r',
211:         '/Ś|Ŝ|Ş|Ș|Š/' => 'S',
212:         '/ś|ŝ|ş|ș|š|ſ/' => 's',
213:         '/Ţ|Ț|Ť|Ŧ/' => 'T',
214:         '/ţ|ț|ť|ŧ/' => 't',
215:         '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
216:         '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
217:         '/Ý|Ÿ|Ŷ/' => 'Y',
218:         '/ý|ÿ|ŷ/' => 'y',
219:         '/Ŵ/' => 'W',
220:         '/ŵ/' => 'w',
221:         '/Ź|Ż|Ž/' => 'Z',
222:         '/ź|ż|ž/' => 'z',
223:         '/Æ|Ǽ/' => 'AE',
224:         '/ß/' => 'ss',
225:         '/IJ/' => 'IJ',
226:         '/ij/' => 'ij',
227:         '/Œ/' => 'OE',
228:         '/ƒ/' => 'f'
229:     );
230: 
231: /**
232:  * Method cache array.
233:  *
234:  * @var array
235:  */
236:     protected static $_cache = array();
237: 
238: /**
239:  * The initial state of Inflector so reset() works.
240:  *
241:  * @var array
242:  */
243:     protected static $_initialState = array();
244: 
245: /**
246:  * Cache inflected values, and return if already available
247:  *
248:  * @param string $type Inflection type
249:  * @param string $key Original value
250:  * @param string $value Inflected value
251:  * @return string Inflected value, from cache
252:  */
253:     protected static function _cache($type, $key, $value = false) {
254:         $key = '_' . $key;
255:         $type = '_' . $type;
256:         if ($value !== false) {
257:             self::$_cache[$type][$key] = $value;
258:             return $value;
259:         }
260:         if (!isset(self::$_cache[$type][$key])) {
261:             return false;
262:         }
263:         return self::$_cache[$type][$key];
264:     }
265: 
266: /**
267:  * Clears Inflectors inflected value caches. And resets the inflection
268:  * rules to the initial values.
269:  *
270:  * @return void
271:  */
272:     public static function reset() {
273:         if (empty(self::$_initialState)) {
274:             self::$_initialState = get_class_vars('Inflector');
275:             return;
276:         }
277:         foreach (self::$_initialState as $key => $val) {
278:             if ($key !== '_initialState') {
279:                 self::${$key} = $val;
280:             }
281:         }
282:     }
283: 
284: /**
285:  * Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
286:  *
287:  * ### Usage:
288:  *
289:  * {{{
290:  * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
291:  * Inflector::rules('plural', array(
292:  *     'rules' => array('/^(inflect)ors$/i' => '\1ables'),
293:  *     'uninflected' => array('dontinflectme'),
294:  *     'irregular' => array('red' => 'redlings')
295:  * ));
296:  * Inflector::rules('transliteration', array('/å/' => 'aa'));
297:  * }}}
298:  *
299:  * @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
300:  * @param array $rules Array of rules to be added.
301:  * @param boolean $reset If true, will unset default inflections for all
302:  *        new rules that are being defined in $rules.
303:  * @return void
304:  */
305:     public static function rules($type, $rules, $reset = false) {
306:         $var = '_' . $type;
307: 
308:         switch ($type) {
309:             case 'transliteration':
310:                 if ($reset) {
311:                     self::$_transliteration = $rules;
312:                 } else {
313:                     self::$_transliteration = $rules + self::$_transliteration;
314:                 }
315:                 break;
316: 
317:             default:
318:                 foreach ($rules as $rule => $pattern) {
319:                     if (is_array($pattern)) {
320:                         if ($reset) {
321:                             self::${$var}[$rule] = $pattern;
322:                         } else {
323:                             if ($rule === 'uninflected') {
324:                                 self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
325:                             } else {
326:                                 self::${$var}[$rule] = $pattern + self::${$var}[$rule];
327:                             }
328:                         }
329:                         unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
330:                         if (isset(self::${$var}['merged'][$rule])) {
331:                             unset(self::${$var}['merged'][$rule]);
332:                         }
333:                         if ($type === 'plural') {
334:                             self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
335:                         } elseif ($type === 'singular') {
336:                             self::$_cache['singularize'] = array();
337:                         }
338:                     }
339:                 }
340:                 self::${$var}['rules'] = $rules + self::${$var}['rules'];
341:         }
342:     }
343: 
344: /**
345:  * Return $word in plural form.
346:  *
347:  * @param string $word Word in singular
348:  * @return string Word in plural
349:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
350:  */
351:     public static function pluralize($word) {
352:         if (isset(self::$_cache['pluralize'][$word])) {
353:             return self::$_cache['pluralize'][$word];
354:         }
355: 
356:         if (!isset(self::$_plural['merged']['irregular'])) {
357:             self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
358:         }
359: 
360:         if (!isset(self::$_plural['merged']['uninflected'])) {
361:             self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
362:         }
363: 
364:         if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
365:             self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
366:             self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
367:         }
368: 
369:         if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
370:             self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
371:             return self::$_cache['pluralize'][$word];
372:         }
373: 
374:         if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
375:             self::$_cache['pluralize'][$word] = $word;
376:             return $word;
377:         }
378: 
379:         foreach (self::$_plural['rules'] as $rule => $replacement) {
380:             if (preg_match($rule, $word)) {
381:                 self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
382:                 return self::$_cache['pluralize'][$word];
383:             }
384:         }
385:     }
386: 
387: /**
388:  * Return $word in singular form.
389:  *
390:  * @param string $word Word in plural
391:  * @return string Word in singular
392:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
393:  */
394:     public static function singularize($word) {
395:         if (isset(self::$_cache['singularize'][$word])) {
396:             return self::$_cache['singularize'][$word];
397:         }
398: 
399:         if (!isset(self::$_singular['merged']['uninflected'])) {
400:             self::$_singular['merged']['uninflected'] = array_merge(
401:                 self::$_singular['uninflected'],
402:                 self::$_uninflected
403:             );
404:         }
405: 
406:         if (!isset(self::$_singular['merged']['irregular'])) {
407:             self::$_singular['merged']['irregular'] = array_merge(
408:                 self::$_singular['irregular'],
409:                 array_flip(self::$_plural['irregular'])
410:             );
411:         }
412: 
413:         if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
414:             self::$_singular['cacheUninflected'] = '(?:' . implode('|', self::$_singular['merged']['uninflected']) . ')';
415:             self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
416:         }
417: 
418:         if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
419:             self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
420:             return self::$_cache['singularize'][$word];
421:         }
422: 
423:         if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
424:             self::$_cache['singularize'][$word] = $word;
425:             return $word;
426:         }
427: 
428:         foreach (self::$_singular['rules'] as $rule => $replacement) {
429:             if (preg_match($rule, $word)) {
430:                 self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
431:                 return self::$_cache['singularize'][$word];
432:             }
433:         }
434:         self::$_cache['singularize'][$word] = $word;
435:         return $word;
436:     }
437: 
438: /**
439:  * Returns the given lower_case_and_underscored_word as a CamelCased word.
440:  *
441:  * @param string $lowerCaseAndUnderscoredWord Word to camelize
442:  * @return string Camelized word. LikeThis.
443:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
444:  */
445:     public static function camelize($lowerCaseAndUnderscoredWord) {
446:         if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
447:             $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
448:             self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
449:         }
450:         return $result;
451:     }
452: 
453: /**
454:  * Returns the given camelCasedWord as an underscored_word.
455:  *
456:  * @param string $camelCasedWord Camel-cased word to be "underscorized"
457:  * @return string Underscore-syntaxed version of the $camelCasedWord
458:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
459:  */
460:     public static function underscore($camelCasedWord) {
461:         if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
462:             $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
463:             self::_cache(__FUNCTION__, $camelCasedWord, $result);
464:         }
465:         return $result;
466:     }
467: 
468: /**
469:  * Returns the given underscored_word_group as a Human Readable Word Group.
470:  * (Underscores are replaced by spaces and capitalized following words.)
471:  *
472:  * @param string $lowerCaseAndUnderscoredWord String to be made more readable
473:  * @return string Human-readable string
474:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
475:  */
476:     public static function humanize($lowerCaseAndUnderscoredWord) {
477:         if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
478:             $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
479:             self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
480:         }
481:         return $result;
482:     }
483: 
484: /**
485:  * Returns corresponding table name for given model $className. ("people" for the model class "Person").
486:  *
487:  * @param string $className Name of class to get database table name for
488:  * @return string Name of the database table for given class
489:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
490:  */
491:     public static function tableize($className) {
492:         if (!($result = self::_cache(__FUNCTION__, $className))) {
493:             $result = Inflector::pluralize(Inflector::underscore($className));
494:             self::_cache(__FUNCTION__, $className, $result);
495:         }
496:         return $result;
497:     }
498: 
499: /**
500:  * Returns Cake model class name ("Person" for the database table "people".) for given database table.
501:  *
502:  * @param string $tableName Name of database table to get class name for
503:  * @return string Class name
504:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
505:  */
506:     public static function classify($tableName) {
507:         if (!($result = self::_cache(__FUNCTION__, $tableName))) {
508:             $result = Inflector::camelize(Inflector::singularize($tableName));
509:             self::_cache(__FUNCTION__, $tableName, $result);
510:         }
511:         return $result;
512:     }
513: 
514: /**
515:  * Returns camelBacked version of an underscored string.
516:  *
517:  * @param string $string
518:  * @return string in variable form
519:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
520:  */
521:     public static function variable($string) {
522:         if (!($result = self::_cache(__FUNCTION__, $string))) {
523:             $camelized = Inflector::camelize(Inflector::underscore($string));
524:             $replace = strtolower(substr($camelized, 0, 1));
525:             $result = preg_replace('/\\w/', $replace, $camelized, 1);
526:             self::_cache(__FUNCTION__, $string, $result);
527:         }
528:         return $result;
529:     }
530: 
531: /**
532:  * Returns a string with all spaces converted to underscores (by default), accented
533:  * characters converted to non-accented characters, and non word characters removed.
534:  *
535:  * @param string $string the string you want to slug
536:  * @param string $replacement will replace keys in map
537:  * @return string
538:  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
539:  */
540:     public static function slug($string, $replacement = '_') {
541:         $quotedReplacement = preg_quote($replacement, '/');
542: 
543:         $merge = array(
544:             '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
545:             '/[\s\p{Zs}]+/mu' => $replacement,
546:             sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
547:         );
548: 
549:         $map = self::$_transliteration + $merge;
550:         return preg_replace(array_keys($map), array_values($map), $string);
551:     }
552: 
553: }
554: 
555: // Store the initial state
556: Inflector::reset();
557: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs