2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pluralize and singularize English words.
|
|
|
|
*
|
|
|
|
* Used by Cake's naming conventions throughout the framework.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-05-01 17:03:30 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2009-05-01 17:03:30 +00:00
|
|
|
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2009-05-01 17:03:30 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Included libraries.
|
|
|
|
*
|
|
|
|
*/
|
2008-06-11 15:46:31 +00:00
|
|
|
if (!class_exists('Object')) {
|
2009-05-01 17:03:30 +00:00
|
|
|
require_once(LIBS . 'object.php');
|
2008-06-11 15:46:31 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Pluralize and singularize English words.
|
|
|
|
*
|
|
|
|
* Inflector pluralizes and singularizes English nouns.
|
|
|
|
* Used by Cake's naming conventions throughout the framework.
|
|
|
|
* Test with $i = new Inflector(); $i->test();
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/491/Inflector
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Inflector extends Object {
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2009-03-22 01:38:32 +00:00
|
|
|
/**
|
|
|
|
* Plural inflector rules
|
|
|
|
*
|
|
|
|
* @var array
|
2009-04-10 17:41:32 +00:00
|
|
|
* @access protected
|
2009-03-22 01:38:32 +00:00
|
|
|
**/
|
2009-04-10 17:41:32 +00:00
|
|
|
var $_plural = array(
|
2009-03-18 00:22:36 +00:00
|
|
|
'rules' => array(
|
2008-06-23 03:08:35 +00:00
|
|
|
'/(s)tatus$/i' => '\1\2tatuses',
|
|
|
|
'/(quiz)$/i' => '\1zes',
|
|
|
|
'/^(ox)$/i' => '\1\2en',
|
|
|
|
'/([m|l])ouse$/i' => '\1ice',
|
|
|
|
'/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
|
|
|
|
'/(x|ch|ss|sh)$/i' => '\1es',
|
|
|
|
'/([^aeiouy]|qu)y$/i' => '\1ies',
|
|
|
|
'/(hive)$/i' => '\1s',
|
|
|
|
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
|
|
|
|
'/sis$/i' => 'ses',
|
|
|
|
'/([ti])um$/i' => '\1a',
|
|
|
|
'/(p)erson$/i' => '\1eople',
|
|
|
|
'/(m)an$/i' => '\1en',
|
|
|
|
'/(c)hild$/i' => '\1hildren',
|
|
|
|
'/(buffal|tomat)o$/i' => '\1\2oes',
|
|
|
|
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
|
|
|
|
'/us$/' => 'uses',
|
|
|
|
'/(alias)$/i' => '\1es',
|
2009-02-22 04:09:39 +00:00
|
|
|
'/(ax|cris|test)is$/i' => '\1es',
|
2008-06-23 03:08:35 +00:00
|
|
|
'/s$/' => 's',
|
2008-08-26 18:30:54 +00:00
|
|
|
'/^$/' => '',
|
2009-03-18 00:22:36 +00:00
|
|
|
'/$/' => 's',
|
|
|
|
),
|
|
|
|
'uninflected' => array(
|
|
|
|
'.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people'
|
|
|
|
),
|
|
|
|
'irregular' => array(
|
2008-06-23 03:08:35 +00:00
|
|
|
'atlas' => 'atlases',
|
|
|
|
'beef' => 'beefs',
|
|
|
|
'brother' => 'brothers',
|
|
|
|
'child' => 'children',
|
|
|
|
'corpus' => 'corpuses',
|
|
|
|
'cow' => 'cows',
|
|
|
|
'ganglion' => 'ganglions',
|
|
|
|
'genie' => 'genies',
|
|
|
|
'genus' => 'genera',
|
|
|
|
'graffito' => 'graffiti',
|
|
|
|
'hoof' => 'hoofs',
|
|
|
|
'loaf' => 'loaves',
|
|
|
|
'man' => 'men',
|
|
|
|
'money' => 'monies',
|
|
|
|
'mongoose' => 'mongooses',
|
|
|
|
'move' => 'moves',
|
|
|
|
'mythos' => 'mythoi',
|
2009-04-10 18:47:17 +00:00
|
|
|
'niche' => 'niches',
|
2008-06-23 03:08:35 +00:00
|
|
|
'numen' => 'numina',
|
|
|
|
'occiput' => 'occiputs',
|
|
|
|
'octopus' => 'octopuses',
|
|
|
|
'opus' => 'opuses',
|
|
|
|
'ox' => 'oxen',
|
|
|
|
'penis' => 'penises',
|
|
|
|
'person' => 'people',
|
|
|
|
'sex' => 'sexes',
|
|
|
|
'soliloquy' => 'soliloquies',
|
|
|
|
'testis' => 'testes',
|
|
|
|
'trilby' => 'trilbys',
|
2009-03-18 00:22:36 +00:00
|
|
|
'turf' => 'turfs'
|
|
|
|
)
|
|
|
|
);
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2009-03-22 01:38:32 +00:00
|
|
|
/**
|
|
|
|
* Singular inflector rules
|
|
|
|
*
|
|
|
|
* @var array
|
2009-04-10 17:41:32 +00:00
|
|
|
* @access protected
|
2009-03-22 01:38:32 +00:00
|
|
|
**/
|
2009-04-10 17:41:32 +00:00
|
|
|
var $_singular = array(
|
2009-03-18 00:22:36 +00:00
|
|
|
'rules' => array(
|
2008-06-23 03:08:35 +00:00
|
|
|
'/(s)tatuses$/i' => '\1\2tatus',
|
|
|
|
'/^(.*)(menu)s$/i' => '\1\2',
|
|
|
|
'/(quiz)zes$/i' => '\\1',
|
|
|
|
'/(matr)ices$/i' => '\1ix',
|
|
|
|
'/(vert|ind)ices$/i' => '\1ex',
|
|
|
|
'/^(ox)en/i' => '\1',
|
|
|
|
'/(alias)(es)*$/i' => '\1',
|
|
|
|
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
|
2009-02-22 04:09:39 +00:00
|
|
|
'/([ftw]ax)es/' => '\1',
|
2008-06-23 03:08:35 +00:00
|
|
|
'/(cris|ax|test)es$/i' => '\1is',
|
|
|
|
'/(shoe)s$/i' => '\1',
|
|
|
|
'/(o)es$/i' => '\1',
|
|
|
|
'/ouses$/' => 'ouse',
|
|
|
|
'/uses$/' => 'us',
|
|
|
|
'/([m|l])ice$/i' => '\1ouse',
|
|
|
|
'/(x|ch|ss|sh)es$/i' => '\1',
|
|
|
|
'/(m)ovies$/i' => '\1\2ovie',
|
|
|
|
'/(s)eries$/i' => '\1\2eries',
|
|
|
|
'/([^aeiouy]|qu)ies$/i' => '\1y',
|
|
|
|
'/([lr])ves$/i' => '\1f',
|
|
|
|
'/(tive)s$/i' => '\1',
|
|
|
|
'/(hive)s$/i' => '\1',
|
|
|
|
'/(drive)s$/i' => '\1',
|
2008-07-12 00:22:09 +00:00
|
|
|
'/([^fo])ves$/i' => '\1fe',
|
2008-06-23 03:08:35 +00:00
|
|
|
'/(^analy)ses$/i' => '\1sis',
|
|
|
|
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
|
|
|
|
'/([ti])a$/i' => '\1um',
|
|
|
|
'/(p)eople$/i' => '\1\2erson',
|
|
|
|
'/(m)en$/i' => '\1an',
|
|
|
|
'/(c)hildren$/i' => '\1\2hild',
|
|
|
|
'/(n)ews$/i' => '\1\2ews',
|
|
|
|
'/^(.*us)$/' => '\\1',
|
2009-03-18 00:22:36 +00:00
|
|
|
'/s$/i' => ''
|
|
|
|
),
|
|
|
|
'uninflected' => array(
|
2009-04-10 19:28:34 +00:00
|
|
|
'.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
|
2009-03-18 00:22:36 +00:00
|
|
|
),
|
|
|
|
'irregular' => array()
|
|
|
|
);
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2009-03-22 01:38:32 +00:00
|
|
|
/**
|
|
|
|
* Words that should not be inflected
|
|
|
|
*
|
|
|
|
* @var array
|
2009-04-10 17:41:32 +00:00
|
|
|
* @access protected
|
2009-03-22 01:38:32 +00:00
|
|
|
**/
|
2009-04-10 17:41:32 +00:00
|
|
|
var $_uninflected = array(
|
2009-04-10 19:28:34 +00:00
|
|
|
'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
|
|
|
|
'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
|
|
|
|
'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
|
|
|
|
'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
|
|
|
|
'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
|
|
|
|
'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', 'media',
|
|
|
|
'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
|
|
|
|
'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
|
|
|
|
'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
|
|
|
|
'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
|
|
|
|
'trousers', 'trout','tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
|
|
|
|
'Yengeese'
|
2009-03-18 00:22:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached array identity map of pluralized words.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-04-10 17:41:32 +00:00
|
|
|
* @access protected
|
2009-03-18 00:22:36 +00:00
|
|
|
**/
|
2009-04-10 17:41:32 +00:00
|
|
|
var $_pluralized = array();
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
/**
|
|
|
|
* Cached array identity map of singularized words.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-04-10 17:41:32 +00:00
|
|
|
* @access protected
|
2009-03-18 00:22:36 +00:00
|
|
|
**/
|
2009-04-10 17:41:32 +00:00
|
|
|
var $_singularized = array();
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to the Inflector object instance
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function &getInstance() {
|
|
|
|
static $instance = array();
|
|
|
|
|
|
|
|
if (!$instance) {
|
|
|
|
$instance[0] =& new Inflector();
|
|
|
|
}
|
|
|
|
return $instance[0];
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
/**
|
2009-03-22 01:38:32 +00:00
|
|
|
* Adds custom inflection $rules, of either 'plural' or 'singular' $type.
|
2009-03-18 00:22:36 +00:00
|
|
|
*
|
2009-03-22 01:38:32 +00:00
|
|
|
* @param string $type The type of inflection, either 'singular' or 'plural'
|
|
|
|
* @param array $rules Array of rules to be added. Example usage:
|
|
|
|
* Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
|
|
|
|
* Inflector::rules('plural', array(
|
|
|
|
* 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
|
|
|
|
* 'uninflected' => array('dontinflectme'),
|
|
|
|
* 'irregular' => array('red' => 'redlings')
|
|
|
|
* ));
|
|
|
|
* @access public
|
2009-03-18 00:22:36 +00:00
|
|
|
* @return void
|
2009-03-22 01:38:32 +00:00
|
|
|
* @static
|
2009-03-18 00:22:36 +00:00
|
|
|
*/
|
|
|
|
function rules($type, $rules = array()) {
|
|
|
|
$_this =& Inflector::getInstance();
|
2009-04-10 17:41:32 +00:00
|
|
|
$type = '_'.$type;
|
2009-03-18 00:22:36 +00:00
|
|
|
|
2009-04-10 17:03:38 +00:00
|
|
|
foreach ($rules as $rule => $pattern) {
|
|
|
|
if (is_array($pattern)) {
|
|
|
|
$_this->{$type}[$rule] = array_merge($pattern, $_this->{$type}[$rule]);
|
|
|
|
unset($rules[$rule], $_this->{$type}['cache' . ucfirst($rule)], $_this->{$type}['merged'][$rule]);
|
|
|
|
}
|
2009-03-18 00:22:36 +00:00
|
|
|
}
|
2009-04-10 17:03:38 +00:00
|
|
|
$_this->{$type}['rules'] = array_merge($rules, $_this->{$type}['rules']);
|
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
/**
|
|
|
|
* Return $word in plural form.
|
|
|
|
*
|
|
|
|
* @param string $word Word in singular
|
|
|
|
* @return string Word in plural
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
|
|
|
*/
|
|
|
|
function pluralize($word) {
|
|
|
|
$_this =& Inflector::getInstance();
|
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (isset($_this->_pluralized[$word])) {
|
|
|
|
return $_this->_pluralized[$word];
|
2009-03-18 00:22:36 +00:00
|
|
|
}
|
2009-03-18 05:42:22 +00:00
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (!isset($_this->_plural['merged']['irregular'])) {
|
|
|
|
$_this->_plural['merged']['irregular'] = $_this->_plural['irregular'];
|
2009-04-10 17:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_this->plural['merged']['uninflected'])) {
|
2009-04-10 17:41:32 +00:00
|
|
|
$_this->_plural['merged']['uninflected'] = array_merge($_this->_plural['uninflected'], $_this->_uninflected);
|
2009-04-10 17:03:38 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (!isset($_this->_plural['cacheUninflected']) || !isset($_this->_plural['cacheIrregular'])) {
|
|
|
|
$_this->_plural['cacheUninflected'] = '(?:' . join( '|', $_this->_plural['merged']['uninflected']) . ')';
|
|
|
|
$_this->_plural['cacheIrregular'] = '(?:' . join( '|', array_keys($_this->_plural['merged']['irregular'])) . ')';
|
2009-03-18 00:22:36 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (preg_match('/(.*)\\b(' . $_this->_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
|
|
|
|
$_this->_pluralized[$word] = $regs[1] . substr($word, 0, 1) . substr($_this->_plural['merged']['irregular'][strtolower($regs[2])], 1);
|
|
|
|
return $_this->_pluralized[$word];
|
2009-03-18 00:22:36 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (preg_match('/^(' . $_this->_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
|
|
|
|
$_this->_pluralized[$word] = $word;
|
2009-03-18 00:22:36 +00:00
|
|
|
return $word;
|
|
|
|
}
|
2008-08-27 03:42:17 +00:00
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
foreach ($_this->_plural['rules'] as $rule => $replacement) {
|
2009-03-18 00:22:36 +00:00
|
|
|
if (preg_match($rule, $word)) {
|
2009-04-10 17:41:32 +00:00
|
|
|
$_this->_pluralized[$word] = preg_replace($rule, $replacement, $word);
|
|
|
|
return $_this->_pluralized[$word];
|
2009-03-18 00:22:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-18 00:22:36 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Return $word in singular form.
|
|
|
|
*
|
|
|
|
* @param string $word Word in plural
|
|
|
|
* @return string Word in singular
|
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function singularize($word) {
|
|
|
|
$_this =& Inflector::getInstance();
|
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (isset($_this->_singularized[$word])) {
|
|
|
|
return $_this->_singularized[$word];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-12-18 01:35:27 +00:00
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (!isset($_this->_singular['merged']['uninflected'])) {
|
|
|
|
$_this->_singular['merged']['uninflected'] = array_merge($_this->_singular['uninflected'], $_this->_uninflected);
|
2009-04-10 17:03:38 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (!isset($_this->_singular['merged']['irregular'])) {
|
|
|
|
$_this->_singular['merged']['irregular'] = array_merge($_this->_singular['irregular'], array_flip($_this->_plural['irregular']));
|
2009-04-10 17:03:38 +00:00
|
|
|
}
|
2009-03-18 00:22:36 +00:00
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (!isset($_this->_singular['cacheUninflected']) || !isset($_this->_singular['cacheIrregular'])) {
|
|
|
|
$_this->_singular['cacheUninflected'] = '(?:' . join( '|', $_this->_singular['merged']['uninflected']) . ')';
|
|
|
|
$_this->_singular['cacheIrregular'] = '(?:' . join( '|', array_keys($_this->_singular['merged']['irregular'])) . ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (preg_match('/(.*)\\b(' . $_this->_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
|
|
|
|
$_this->_singularized[$word] = $regs[1] . substr($word, 0, 1) . substr($_this->_singular['merged']['irregular'][strtolower($regs[2])], 1);
|
|
|
|
return $_this->_singularized[$word];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
if (preg_match('/^(' . $_this->_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
|
|
|
|
$_this->_singularized[$word] = $word;
|
2008-05-30 11:40:08 +00:00
|
|
|
return $word;
|
|
|
|
}
|
|
|
|
|
2009-04-10 17:41:32 +00:00
|
|
|
foreach ($_this->_singular['rules'] as $rule => $replacement) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (preg_match($rule, $word)) {
|
2009-04-10 17:41:32 +00:00
|
|
|
$_this->_singularized[$word] = preg_replace($rule, $replacement, $word);
|
|
|
|
return $_this->_singularized[$word];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-10 17:41:32 +00:00
|
|
|
$_this->_singularized[$word] = $word;
|
2008-05-30 11:40:08 +00:00
|
|
|
return $word;
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
* Returns the given lower_case_and_underscored_word as a CamelCased word.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $lower_case_and_underscored_word Word to camelize
|
2008-06-23 03:08:35 +00:00
|
|
|
* @return string Camelized word. LikeThis.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function camelize($lowerCaseAndUnderscoredWord) {
|
2008-12-18 01:35:27 +00:00
|
|
|
return str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
* Returns the given camelCasedWord as an underscored_word.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-23 00:10:44 +00:00
|
|
|
* @param string $camelCasedWord Camel-cased word to be "underscorized"
|
|
|
|
* @return string Underscore-syntaxed version of the $camelCasedWord
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function underscore($camelCasedWord) {
|
2008-12-18 01:35:27 +00:00
|
|
|
return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
* Returns the given underscored_word_group as a Human Readable Word Group.
|
|
|
|
* (Underscores are replaced by spaces and capitalized following words.)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $lower_case_and_underscored_word String to be made more readable
|
|
|
|
* @return string Human-readable string
|
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function humanize($lowerCaseAndUnderscoredWord) {
|
2008-12-18 01:35:27 +00:00
|
|
|
return ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
* Returns corresponding table name for given model $className. ("people" for the model class "Person").
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-23 00:10:44 +00:00
|
|
|
* @param string $className Name of class to get database table name for
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Name of the database table for given class
|
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function tableize($className) {
|
2008-12-18 01:35:27 +00:00
|
|
|
return Inflector::pluralize(Inflector::underscore($className));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
* Returns Cake model class name ("Person" for the database table "people".) for given database table.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $tableName Name of database table to get class name for
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string Class name
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function classify($tableName) {
|
2008-12-18 01:35:27 +00:00
|
|
|
return Inflector::camelize(Inflector::singularize($tableName));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
* Returns camelBacked version of an underscored string.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $string
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return string in variable form
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function variable($string) {
|
|
|
|
$string = Inflector::camelize(Inflector::underscore($string));
|
|
|
|
$replace = strtolower(substr($string, 0, 1));
|
2008-12-18 01:35:27 +00:00
|
|
|
return preg_replace('/\\w/', $replace, $string, 1);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
* Returns a string with all spaces converted to underscores (by default), accented
|
|
|
|
* characters converted to non-accented characters, and non word characters removed.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-05-01 17:03:30 +00:00
|
|
|
* @param string $string the string you want to slug
|
|
|
|
* @param string $replacement will replace keys in map
|
|
|
|
* @param array $map extra elements to map to the replacement
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
* @static
|
2008-10-31 20:49:23 +00:00
|
|
|
* @link http://book.cakephp.org/view/572/Class-methods
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-05-01 17:03:30 +00:00
|
|
|
function slug($string, $replacement = '_', $map = array()) {
|
2009-04-10 18:15:27 +00:00
|
|
|
$quotedReplacement = preg_quote($replacement, '/');
|
|
|
|
|
2009-05-01 17:03:30 +00:00
|
|
|
$default = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'/à|á|å|â/' => 'a',
|
|
|
|
'/è|é|ê|ẽ|ë/' => 'e',
|
|
|
|
'/ì|í|î/' => 'i',
|
|
|
|
'/ò|ó|ô|ø/' => 'o',
|
|
|
|
'/ù|ú|ů|û/' => 'u',
|
|
|
|
'/ç/' => 'c',
|
|
|
|
'/ñ/' => 'n',
|
|
|
|
'/ä|æ/' => 'ae',
|
|
|
|
'/ö/' => 'oe',
|
|
|
|
'/ü/' => 'ue',
|
|
|
|
'/Ä/' => 'Ae',
|
|
|
|
'/Ü/' => 'Ue',
|
|
|
|
'/Ö/' => 'Oe',
|
|
|
|
'/ß/' => 'ss',
|
|
|
|
'/[^\w\s]/' => ' ',
|
|
|
|
'/\\s+/' => $replacement,
|
2009-04-10 18:15:27 +00:00
|
|
|
sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2009-05-01 17:03:30 +00:00
|
|
|
$map = array_merge($default, $map);
|
2008-12-18 01:35:27 +00:00
|
|
|
return preg_replace(array_keys($map), array_values($map), $string);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-10 19:28:34 +00:00
|
|
|
?>
|