2005-07-04 01:07:14 +00:00
|
|
|
<?php
|
2005-08-21 06:49:02 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Pluralize and singularize English words.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* Used by Cake's naming conventions throughout the framework.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2005-12-27 03:33:44 +00:00
|
|
|
* Copyright (c) 2005, Cake Software Foundation, Inc.
|
2005-12-23 21:57:26 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* @filesource
|
2005-12-23 21:57:26 +00:00
|
|
|
* @copyright Copyright (c) 2005, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
2005-08-21 06:49:02 +00:00
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs
|
2005-08-21 06:49:02 +00:00
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Included libraries.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if(!class_exists('Object', FALSE))
|
|
|
|
{
|
|
|
|
uses('object');
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Pluralize and singularize English words.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* Inflector pluralizes and singularizes English nouns.
|
2005-10-18 22:27:39 +00:00
|
|
|
* Used by Cake's naming conventions throughout the framework.
|
2005-08-21 06:49:02 +00:00
|
|
|
* Test with $i = new Inflector(); $i->test();
|
|
|
|
*
|
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs
|
2005-08-21 06:49:02 +00:00
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
class Inflector extends Object
|
2005-06-30 02:09:47 +00:00
|
|
|
{
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function __construct ()
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
|
|
|
* Return $word in plural form.
|
|
|
|
*
|
|
|
|
* @param string $word Word in singular
|
|
|
|
* @return string Word in plural
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function pluralize ($word)
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
$plural_rules = array(
|
[1242]
Author: phpnut
Date: 6:19:00 PM, Thursday, October 27, 2005
Message:
removing session directory
[1241]
Author: phpnut
Date: 6:13:46 PM, Thursday, October 27, 2005
Message:
added docblocks and temp error messages when trying to use the unimplemeted database for sessions
[1239]
Author: phpnut
Date: 5:28:57 PM, Thursday, October 27, 2005
Message:
More work on session.
Added config settings to core.php
updated model to allow models without a database table.
[1237]
Author: phpnut
Date: 3:50:27 PM, Thursday, October 27, 2005
Message:
adding directories for sessions
[1235]
Author: phpnut
Date: 3:36:08 AM, Thursday, October 27, 2005
Message:
Typo
[1234]
Author: phpnut
Date: 3:34:07 AM, Thursday, October 27, 2005
Message:
More work on the Session classes and adding the first methods the to Security class
[1233]
Author: phpnut
Date: 3:05:46 AM, Thursday, October 27, 2005
Message:
Start to core security class added.
Moved paths.php to cake/config/
Refactoring Session classes
[1232]
Author: phpnut
Date: 2:20:25 AM, Thursday, October 27, 2005
Message:
More work on the core session class.
Adding session component class.
Added fix for errors messages not working with validation.
Added possible fix for inflector now working on the word status.
git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1243 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-27 23:29:56 +00:00
|
|
|
'/(s)tatus$/' => '\1\2tatuses',
|
2005-12-27 03:33:44 +00:00
|
|
|
'/^(ox)$/' => '\1\2en', # ox
|
|
|
|
'/([m|l])ouse$/' => '\1ice', # mouse, louse
|
|
|
|
'/(matr|vert|ind)ix|ex$/' => '\1ices', # matrix, vertex, index
|
2005-08-21 06:49:02 +00:00
|
|
|
'/(x|ch|ss|sh)$/' => '\1es', # search, switch, fix, box, process, address
|
|
|
|
'/([^aeiouy]|qu)y$/' => '\1ies', # query, ability, agency
|
|
|
|
'/(hive)$/' => '\1s', # archive, hive
|
|
|
|
'/(?:([^f])fe|([lr])f)$/' => '\1\2ves', # half, safe, wife
|
|
|
|
'/sis$/' => 'ses', # basis, diagnosis
|
|
|
|
'/([ti])um$/' => '\1a', # datum, medium
|
|
|
|
'/(p)erson$/' => '\1eople', # person, salesperson
|
|
|
|
'/(m)an$/' => '\1en', # man, woman, spokesman
|
|
|
|
'/(c)hild$/' => '\1hildren', # child
|
2005-12-27 03:33:44 +00:00
|
|
|
'/(buffal|tomat)o$/' => '\1\2oes', # buffalo, tomato
|
|
|
|
'/(bu)s$/' => '\1\2ses', # bus
|
2005-08-21 06:49:02 +00:00
|
|
|
'/(alias)/' => '\1es', # alias
|
2005-12-27 03:33:44 +00:00
|
|
|
'/(octop|vir)us$/' => '\1i', # octopus, virus - virus has no defined plural (according to Latin/dictionary.com), but viri is better than viruses/viruss
|
|
|
|
'/(ax|cri|test)is$/' => '\1es', # axis, crisis
|
2005-08-21 06:49:02 +00:00
|
|
|
'/s$/' => 's', # no change (compatibility)
|
|
|
|
'/$/' => 's'
|
2005-07-10 05:08:19 +00:00
|
|
|
);
|
2005-12-27 03:33:44 +00:00
|
|
|
|
|
|
|
foreach ($plural_rules as $rule => $replacement)
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-12-27 03:33:44 +00:00
|
|
|
if (preg_match($rule, $word))
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
return preg_replace($rule, $replacement, $word);
|
|
|
|
}
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
return $word;//false;
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return $word in singular form.
|
|
|
|
*
|
|
|
|
* @param string $word Word in plural
|
|
|
|
* @return string Word in singular
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function singularize ($word)
|
|
|
|
{
|
|
|
|
$singular_rules = array(
|
[1242]
Author: phpnut
Date: 6:19:00 PM, Thursday, October 27, 2005
Message:
removing session directory
[1241]
Author: phpnut
Date: 6:13:46 PM, Thursday, October 27, 2005
Message:
added docblocks and temp error messages when trying to use the unimplemeted database for sessions
[1239]
Author: phpnut
Date: 5:28:57 PM, Thursday, October 27, 2005
Message:
More work on session.
Added config settings to core.php
updated model to allow models without a database table.
[1237]
Author: phpnut
Date: 3:50:27 PM, Thursday, October 27, 2005
Message:
adding directories for sessions
[1235]
Author: phpnut
Date: 3:36:08 AM, Thursday, October 27, 2005
Message:
Typo
[1234]
Author: phpnut
Date: 3:34:07 AM, Thursday, October 27, 2005
Message:
More work on the Session classes and adding the first methods the to Security class
[1233]
Author: phpnut
Date: 3:05:46 AM, Thursday, October 27, 2005
Message:
Start to core security class added.
Moved paths.php to cake/config/
Refactoring Session classes
[1232]
Author: phpnut
Date: 2:20:25 AM, Thursday, October 27, 2005
Message:
More work on the core session class.
Adding session component class.
Added fix for errors messages not working with validation.
Added possible fix for inflector now working on the word status.
git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1243 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-10-27 23:29:56 +00:00
|
|
|
'/(s)tatuses$/' => '\1\2tatus',
|
2005-08-21 06:49:02 +00:00
|
|
|
'/(matr)ices$/' =>'\1ix',
|
2005-12-27 03:33:44 +00:00
|
|
|
'/(vert|ind)ices$/' => '\1ex',
|
|
|
|
'/^(ox)en/' => '\1',
|
|
|
|
'/(alias)es$/' => '\1',
|
|
|
|
'/([octop|vir])i$/' => '\1us',
|
|
|
|
'/(cris|ax|test)es$/' => '\1is',
|
|
|
|
'/(shoe)s$/' => '\1',
|
|
|
|
'/(o)es$/' => '\1',
|
|
|
|
'/(bus)es$/' => '\1',
|
|
|
|
'/([m|l])ice$/' => '\1ouse',
|
2005-08-21 06:49:02 +00:00
|
|
|
'/(x|ch|ss|sh)es$/' => '\1',
|
|
|
|
'/(m)ovies$/' => '\1\2ovie',
|
|
|
|
'/(s)eries$/' => '\1\2eries',
|
|
|
|
'/([^aeiouy]|qu)ies$/' => '\1y',
|
|
|
|
'/([lr])ves$/' => '\1f',
|
|
|
|
'/(tive)s$/' => '\1',
|
|
|
|
'/(hive)s$/' => '\1',
|
|
|
|
'/([^f])ves$/' => '\1fe',
|
|
|
|
'/(^analy)ses$/' => '\1sis',
|
|
|
|
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
|
|
|
|
'/([ti])a$/' => '\1um',
|
|
|
|
'/(p)eople$/' => '\1\2erson',
|
|
|
|
'/(m)en$/' => '\1an',
|
|
|
|
'/(c)hildren$/' => '\1\2hild',
|
|
|
|
'/(n)ews$/' => '\1\2ews',
|
|
|
|
'/s$/' => ''
|
2005-07-10 05:08:19 +00:00
|
|
|
);
|
2005-06-30 02:09:47 +00:00
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
foreach ($singular_rules as $rule => $replacement)
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-12-27 03:33:44 +00:00
|
|
|
if (preg_match($rule, $word))
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
return preg_replace($rule, $replacement, $word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// should not return false is not matched
|
|
|
|
return $word;//false;
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-06-30 02:09:47 +00:00
|
|
|
/**
|
|
|
|
* Returns given $lower_case_and_underscored_word as a camelCased word.
|
|
|
|
*
|
|
|
|
* @param string $lower_case_and_underscored_word Word to camelize
|
|
|
|
* @return string Camelized word. likeThis.
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function camelize($lower_case_and_underscored_word)
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
return str_replace(" ","",ucwords(str_replace("_"," ",$lower_case_and_underscored_word)));
|
2005-12-27 03:33:44 +00:00
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word.
|
|
|
|
*
|
|
|
|
* @param string $camel_cased_word Camel-cased word to be "underscorized"
|
|
|
|
* @return string Underscore-syntaxed version of the $camel_cased_word
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function underscore($camel_cased_word)
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-10-18 22:27:39 +00:00
|
|
|
$camel_cased_word = preg_replace('/([A-Z]+)([A-Z])/','\1_\2', $camel_cased_word);
|
|
|
|
return strtolower(preg_replace('/([a-z])([A-Z])/','\1_\2', $camel_cased_word));
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a human-readable string from $lower_case_and_underscored_word,
|
|
|
|
* by replacing underscores with a space, and by upper-casing the initial characters.
|
|
|
|
*
|
|
|
|
* @param string $lower_case_and_underscored_word String to be made more readable
|
|
|
|
* @return string Human-readable string
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function humanize($lower_case_and_underscored_word)
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
return ucwords(str_replace("_"," ",$lower_case_and_underscored_word));
|
2005-12-27 03:33:44 +00:00
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns corresponding table name for given $class_name. ("posts" for the model class "Post").
|
2005-06-30 02:09:47 +00:00
|
|
|
*
|
|
|
|
* @param string $class_name Name of class to get database table name for
|
|
|
|
* @return string Name of the database table for given class
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function tableize($class_name)
|
2005-07-16 06:10:56 +00:00
|
|
|
{
|
2005-07-10 05:08:19 +00:00
|
|
|
return Inflector::pluralize(Inflector::underscore($class_name));
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns Cake model class name ("Post" for the database table "posts".) for given database table.
|
2005-06-30 02:09:47 +00:00
|
|
|
*
|
2005-09-18 13:25:20 +00:00
|
|
|
* @param string $tableName Name of database table to get class name for
|
2005-06-30 02:09:47 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2005-09-18 13:25:20 +00:00
|
|
|
function classify($tableName)
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
2005-09-18 13:25:20 +00:00
|
|
|
return Inflector::camelize(Inflector::singularize($tableName));
|
2005-07-10 05:08:19 +00:00
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-27 03:33:44 +00:00
|
|
|
* Returns $class_name in underscored form, with "_id" tacked on at the end.
|
2005-10-18 22:27:39 +00:00
|
|
|
* This is for use in dealing with foreign keys in the database.
|
2005-06-30 02:09:47 +00:00
|
|
|
*
|
|
|
|
* @param string $class_name
|
|
|
|
* @return string
|
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function foreignKey($class_name)
|
|
|
|
{
|
|
|
|
return Inflector::underscore($class_name) . "_id";
|
2005-12-27 03:33:44 +00:00
|
|
|
}
|
|
|
|
|
2005-07-21 04:02:32 +00:00
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns filename for given Cake controller name.
|
2005-07-21 04:02:32 +00:00
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* @param string $name
|
|
|
|
* @return string
|
2005-07-21 04:02:32 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function toControllerFilename($name)
|
|
|
|
{
|
|
|
|
return CONTROLLERS.Inflector::underscore($name).'.php';
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-07-21 04:02:32 +00:00
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns filename for given Cake helper name.
|
2005-07-21 04:02:32 +00:00
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* @param string $name
|
|
|
|
* @return string
|
2005-07-21 04:02:32 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function toHelperFilename($name)
|
|
|
|
{
|
|
|
|
return HELPERS.Inflector::underscore($name).'.php';
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-07-21 04:02:32 +00:00
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns given name as camelized.
|
2005-07-21 04:02:32 +00:00
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* @param string $name
|
2005-12-27 03:33:44 +00:00
|
|
|
* @param string $correct
|
2005-10-18 22:27:39 +00:00
|
|
|
* @return string
|
|
|
|
* @todo Explain this method
|
2005-07-21 04:02:32 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function toFullName($name, $correct)
|
|
|
|
{
|
|
|
|
if (strstr($name, '_') && (strtolower($name) == $name))
|
|
|
|
{
|
|
|
|
return Inflector::camelize($name);
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-07-10 05:08:19 +00:00
|
|
|
if (preg_match("/^(.*)({$correct})$/i", $name, $reg))
|
|
|
|
{
|
|
|
|
if ($reg[2] == $correct)
|
|
|
|
{
|
|
|
|
return $name;
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
else
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
|
|
|
return ucfirst($reg[1].$correct);
|
|
|
|
}
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
else
|
2005-07-10 05:08:19 +00:00
|
|
|
{
|
|
|
|
return ucfirst($name.$correct);
|
|
|
|
}
|
|
|
|
}
|
2005-12-27 03:33:44 +00:00
|
|
|
|
2005-07-21 04:02:32 +00:00
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns filename for given Cake library name.
|
2005-07-21 04:02:32 +00:00
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* @param string $name
|
|
|
|
* @return string
|
2005-07-21 04:02:32 +00:00
|
|
|
*/
|
2005-07-10 05:08:19 +00:00
|
|
|
function toLibraryFilename ($name)
|
|
|
|
{
|
|
|
|
return LIBS.Inflector::underscore($name).'.php';
|
|
|
|
}
|
2005-06-30 02:09:47 +00:00
|
|
|
}
|
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
?>
|