cakephp2-php8/cake/libs/inflector.php
phpnut 9560f78884 Merging:
Revision: [1638]
removing php short tags

Revision: [1637]
Remove renderElememnts loading of helpers also, forgot it in the last commit

Revision: [1636]
Refactoring after profiling code.
Session was creating a new instance of Dispatcher removed the need for it.
Added a check to the Component class to pass the base to the SessionComponent class, will refactor that at a later time.

Changed View class so it would not load helpers when rending a layout, no need for that.
A great performance boost after the change.

Change the loadModels method call in app/webroot/index.php.
Will only attempt the loadModels call if the AppModel class is not in memory, and the Database class is in memory.
Removed all unnecessary calls to basics uses(). Again another big performance increase.
Added fix to the Html::guiListTree() after discussing the output that is expected.
A ticket was closed on this already.

Revision: [1635]
Removing calls to basic uses()

Revision: [1634]
Removing calls to basics uses() that are not needed.

Revision: [1633]
Removing calls to basics uses() that are not needed.
Moved Object class further up in the loading order

Revision: [1632]
adding fix for Ticket #132

Revision: [1631]
Added fix from Ticket #122

Revision: [1630]
Scaffold views can now be placed in a view directory.
These will override the core.
Example (Must have the scaffold dot name):
app/views/posts/scaffold.list.thtml
app/views/posts/scaffold.new.thtml
app/views/posts/scaffold.edit.thtml
app/views/posts/scaffold.show.thtml

Revision: [1629]
Think I fixed the issue with scaffold showing proper dates prior to January 1 1970 00:00:00.

Revision: [1628]
Added a few more change to allow saving dates prior to January 1 1970 00:00:00.
Still a few issues with this, but will get them figured out soon.
Changed scaffold to use only one form view.

Revision: [1627]
Added fix for Ticket #189

Revision: [1626]
Added fix for Ticket #120.

Revision: [1625]
left justified doc blocks

Revision: [1624]
remove files from uses() that are loaded by default in app/webroot/index.php no reason to attempt to load them again in the classes

Revision: [1623]
adding check to the loadModels and loadController that will only attempt to load files if the classes are not already in memory

Revision: [1622]
Adding fix to time helper that was lost in a previous merge
Removing all tabs from code

Revision: [1621]
Addtional model validation fixes

Revision: [1620]
fixed parse error

Revision: [1619]
Fixing ticket #102

Revision: [1618]
correcting mime types and keywords

Revision: [1617]
correcting mime types and keywords

Revision: [1616]
fixed link in footer

Revision: [1615]
Fixing ticket #207

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@1639 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-12-27 03:33:44 +00:00

288 lines
No EOL
8.4 KiB
PHP

<?php
/* SVN FILE: $Id$ */
/**
* Pluralize and singularize English words.
*
* Used by Cake's naming conventions throughout the framework.
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright (c) 2005, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2005, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP v 0.2.9
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Included libraries.
*
*/
if(!class_exists('Object', FALSE))
{
uses('object');
}
/**
* 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();
*
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP v 0.2.9
*/
class Inflector extends Object
{
/**
* Constructor.
*
*/
function __construct ()
{
parent::__construct();
}
/**
* Return $word in plural form.
*
* @param string $word Word in singular
* @return string Word in plural
*/
function pluralize ($word)
{
$plural_rules = array(
'/(s)tatus$/' => '\1\2tatuses',
'/^(ox)$/' => '\1\2en', # ox
'/([m|l])ouse$/' => '\1ice', # mouse, louse
'/(matr|vert|ind)ix|ex$/' => '\1ices', # matrix, vertex, index
'/(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
'/(buffal|tomat)o$/' => '\1\2oes', # buffalo, tomato
'/(bu)s$/' => '\1\2ses', # bus
'/(alias)/' => '\1es', # alias
'/(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
'/s$/' => 's', # no change (compatibility)
'/$/' => 's'
);
foreach ($plural_rules as $rule => $replacement)
{
if (preg_match($rule, $word))
{
return preg_replace($rule, $replacement, $word);
}
}
return $word;//false;
}
/**
* Return $word in singular form.
*
* @param string $word Word in plural
* @return string Word in singular
*/
function singularize ($word)
{
$singular_rules = array(
'/(s)tatuses$/' => '\1\2tatus',
'/(matr)ices$/' =>'\1ix',
'/(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',
'/(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$/' => ''
);
foreach ($singular_rules as $rule => $replacement)
{
if (preg_match($rule, $word))
{
return preg_replace($rule, $replacement, $word);
}
}
// should not return false is not matched
return $word;//false;
}
/**
* 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.
*/
function camelize($lower_case_and_underscored_word)
{
return str_replace(" ","",ucwords(str_replace("_"," ",$lower_case_and_underscored_word)));
}
/**
* 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
*/
function underscore($camel_cased_word)
{
$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));
}
/**
* 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
*/
function humanize($lower_case_and_underscored_word)
{
return ucwords(str_replace("_"," ",$lower_case_and_underscored_word));
}
/**
* Returns corresponding table name for given $class_name. ("posts" for the model class "Post").
*
* @param string $class_name Name of class to get database table name for
* @return string Name of the database table for given class
*/
function tableize($class_name)
{
return Inflector::pluralize(Inflector::underscore($class_name));
}
/**
* Returns Cake model class name ("Post" for the database table "posts".) for given database table.
*
* @param string $tableName Name of database table to get class name for
* @return string
*/
function classify($tableName)
{
return Inflector::camelize(Inflector::singularize($tableName));
}
/**
* Returns $class_name in underscored form, with "_id" tacked on at the end.
* This is for use in dealing with foreign keys in the database.
*
* @param string $class_name
* @return string
*/
function foreignKey($class_name)
{
return Inflector::underscore($class_name) . "_id";
}
/**
* Returns filename for given Cake controller name.
*
* @param string $name
* @return string
*/
function toControllerFilename($name)
{
return CONTROLLERS.Inflector::underscore($name).'.php';
}
/**
* Returns filename for given Cake helper name.
*
* @param string $name
* @return string
*/
function toHelperFilename($name)
{
return HELPERS.Inflector::underscore($name).'.php';
}
/**
* Returns given name as camelized.
*
* @param string $name
* @param string $correct
* @return string
* @todo Explain this method
*/
function toFullName($name, $correct)
{
if (strstr($name, '_') && (strtolower($name) == $name))
{
return Inflector::camelize($name);
}
if (preg_match("/^(.*)({$correct})$/i", $name, $reg))
{
if ($reg[2] == $correct)
{
return $name;
}
else
{
return ucfirst($reg[1].$correct);
}
}
else
{
return ucfirst($name.$correct);
}
}
/**
* Returns filename for given Cake library name.
*
* @param string $name
* @return string
*/
function toLibraryFilename ($name)
{
return LIBS.Inflector::underscore($name).'.php';
}
}
?>