Commit 9eac2776 authored by Jonathan.Wage's avatar Jonathan.Wage

Consolidated a few things. Initial entry of Inflector class. Moved some...

Consolidated a few things. Initial entry of Inflector class. Moved some methods from Doctrine base class to Doctrine_Lib and Doctrine_Inflector.
parent b6924e0f
...@@ -1068,9 +1068,11 @@ final class Doctrine ...@@ -1068,9 +1068,11 @@ final class Doctrine
default: default:
$ret[] = var_export($var, true); $ret[] = var_export($var, true);
} }
if ($output) { if ($output) {
print implode("\n", $ret); print implode("\n", $ret);
} }
return implode("\n", $ret); return implode("\n", $ret);
} }
...@@ -1082,9 +1084,9 @@ final class Doctrine ...@@ -1082,9 +1084,9 @@ final class Doctrine
* @param string $classname * @param string $classname
* @return string * @return string
*/ */
public static function tableize($classname) public static function tableize($className)
{ {
return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $classname)); return Doctrine_Inflector::tableize($className);
} }
/** /**
...@@ -1097,20 +1099,7 @@ final class Doctrine ...@@ -1097,20 +1099,7 @@ final class Doctrine
*/ */
public static function classify($tableName) public static function classify($tableName)
{ {
return preg_replace_callback('~(_?)(_)([\w])~', array("Doctrine", "classifyCallback"), ucfirst(strtolower($tableName))); return Doctrine_Inflector::classify($tableName);
}
/**
* classifyCallback
*
* Callback function to classify a classname properly.
*
* @param array $matches An array of matches from a pcre_replace call
* @return string A string with matches 1 and mathces 3 in upper case.
*/
public static function classifyCallback($matches)
{
return $matches[1] . strtoupper($matches[3]);
} }
/** /**
...@@ -1121,90 +1110,8 @@ final class Doctrine ...@@ -1121,90 +1110,8 @@ final class Doctrine
* @param string $classname * @param string $classname
* @return boolean * @return boolean
*/ */
public static function isValidClassname($classname) public static function isValidClassname($className)
{
if (preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $classname)) {
return false;
}
return true;
}
/**
* makeDirectories
*
* Makes the directories for a path recursively
*
* @param string $path
* @return void
*/
public static function makeDirectories($path, $mode = 0777)
{ {
if ( ! $path) { return Doctrine_Lib::isValidClassName($className);
return false;
}
if (is_dir($path) || is_file($path)) {
return true;
}
return mkdir($path, $mode, true);
}
/**
* removeDirectories
*
* @param string $folderPath
* @return void
*/
public static function removeDirectories($folderPath)
{
if (is_dir($folderPath))
{
foreach (scandir($folderPath) as $value)
{
if ($value != '.' && $value != '..')
{
$value = $folderPath . "/" . $value;
if (is_dir($value)) {
self::removeDirectories($value);
} else if (is_file($value)) {
@unlink($value);
}
}
}
return rmdir ( $folderPath );
} else {
return false;
}
}
/**
* getValidators
*
* Get available doctrine validators
*
* @return array $validators
*/
public static function getValidators()
{
if (empty(self::$_validators)) {
$dir = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Validator';
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $file) {
$e = explode('.', $file->getFileName());
if (end($e) == 'php') {
$name = strtolower($e[0]);
self::$_validators[$name] = $name;
}
}
}
return self::$_validators;
} }
} }
\ No newline at end of file
...@@ -49,7 +49,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin ...@@ -49,7 +49,7 @@ class Doctrine_AuditLog extends Doctrine_Plugin
*/ */
public function __construct(array $options = array()) public function __construct(array $options = array())
{ {
$this->_options = array_merge($this->_options, $options); $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
} }
/** /**
...@@ -104,4 +104,4 @@ class Doctrine_AuditLog extends Doctrine_Plugin ...@@ -104,4 +104,4 @@ class Doctrine_AuditLog extends Doctrine_Plugin
// the version column should be part of the primary key definition // the version column should be part of the primary key definition
$this->hasColumn($this->_options['versionColumn'], 'integer', 8, array('primary' => true)); $this->hasColumn($this->_options['versionColumn'], 'integer', 8, array('primary' => true));
} }
} }
\ No newline at end of file
...@@ -732,7 +732,7 @@ END; ...@@ -732,7 +732,7 @@ END;
null null
); );
Doctrine::makeDirectories($path); Doctrine_Lib::makeDirectories($path);
$writePath = $path . DIRECTORY_SEPARATOR . $className . $this->_suffix; $writePath = $path . DIRECTORY_SEPARATOR . $className . $this->_suffix;
...@@ -797,11 +797,11 @@ END; ...@@ -797,11 +797,11 @@ END;
} }
if (isset($writePath)) { if (isset($writePath)) {
Doctrine::makeDirectories($writePath); Doctrine_Lib::makeDirectories($writePath);
$writePath .= DIRECTORY_SEPARATOR . $fileName; $writePath .= DIRECTORY_SEPARATOR . $fileName;
} else { } else {
Doctrine::makeDirectories($this->_path); Doctrine_Lib::makeDirectories($this->_path);
$writePath = $this->_path . DIRECTORY_SEPARATOR . $fileName; $writePath = $this->_path . DIRECTORY_SEPARATOR . $fileName;
} }
......
...@@ -335,7 +335,7 @@ class Doctrine_Import_Schema ...@@ -335,7 +335,7 @@ class Doctrine_Import_Schema
$colDesc['sequence'] = isset($field['sequence']) ? (string) $field['sequence']:null; $colDesc['sequence'] = isset($field['sequence']) ? (string) $field['sequence']:null;
$colDesc['values'] = isset($field['values']) ? (array) $field['values']:null; $colDesc['values'] = isset($field['values']) ? (array) $field['values']:null;
$validators = Doctrine::getValidators(); $validators = Doctrine_Lib::getValidators();
foreach ($validators as $validator) { foreach ($validators as $validator) {
if (isset($field[$validator])) { if (isset($field[$validator])) {
......
<?php
/*
* $Id: Inflector.php 3189 2007-11-18 20:37:44Z meus $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Inflector has static methods for inflecting text
*
* The methods in these classes are from several different sources collected
* across the internet through php development for several years.
* They have been updated and modified a little bit for Doctrine but are mainly untouched.
*
* @package Doctrine
* @subpackage Inflector
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 3189 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Inflector
{
/**
* pluralize
*
* @param string $word English noun to pluralize
* @return string Plural noun
*/
public static function pluralize($word)
{
$plural = array('/(quiz)$/i' => '\1zes',
'/^(ox)$/i' => '\1en',
'/([m|l])ouse$/i' => '\1ice',
'/(matr|vert|ind)ix|ex$/i' => '\1ices',
'/(x|ch|ss|sh)$/i' => '\1es',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/([^aeiouy]|qu)y$/i' => '\1ies',
'/(hive)$/i' => '\1s',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/sis$/i' => 'ses',
'/([ti])um$/i' => '\1a',
'/(buffal|tomat)o$/i' => '\1oes',
'/(bu)s$/i' => '\1ses',
'/(alias|status)/i' => '\1es',
'/(octop|vir)us$/i' => '\1i',
'/(ax|test)is$/i' => '\1es',
'/s$/i' => 's',
'/$/' => 's');
$uncountable = array('equipment',
'information',
'rice',
'money',
'species',
'series',
'fish',
'sheep');
$irregular = array('person' => 'people',
'man' => 'men',
'child' => 'children',
'sex' => 'sexes',
'move' => 'moves');
$lowercasedWord = strtolower($word);
foreach ($uncountable as $_uncountable) {
if(substr($lowercasedWord, (-1 * strlen($_uncountable))) == $_uncountable) {
return $word;
}
}
foreach ($irregular as $_plural=> $_singular){
if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1) . substr($_singular,1), $word);
}
}
foreach ($plural as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return false;
}
/**
* singularize
*
* @param string $word English noun to singularize
* @return string Singular noun.
*/
public static function singularize($word)
{
$singular = array('/(quiz)zes$/i' => '\\1',
'/(matr)ices$/i' => '\\1ix',
'/(vert|ind)ices$/i' => '\\1ex',
'/^(ox)en/i' => '\\1',
'/(alias|status)es$/i' => '\\1',
'/([octop|vir])i$/i' => '\\1us',
'/(cris|ax|test)es$/i' => '\\1is',
'/(shoe)s$/i' => '\\1',
'/(o)es$/i' => '\\1',
'/(bus)es$/i' => '\\1',
'/([m|l])ice$/i' => '\\1ouse',
'/(x|ch|ss|sh)es$/i' => '\\1',
'/(m)ovies$/i' => '\\1ovie',
'/(s)eries$/i' => '\\1eries',
'/([^aeiouy]|qu)ies$/i' => '\\1y',
'/([lr])ves$/i' => '\\1f',
'/(tive)s$/i' => '\\1',
'/(hive)s$/i' => '\\1',
'/([^f])ves$/i' => '\\1fe',
'/(^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',
'/(n)ews$/i' => '\\1ews',
'/s$/i' => '');
$uncountable = array('equipment',
'information',
'rice',
'money',
'species',
'series',
'fish',
'sheep',
'sms');
$irregular = array('person' => 'people',
'man' => 'men',
'child' => 'children',
'sex' => 'sexes',
'move' => 'moves');
$lowercasedWord = strtolower($word);
foreach ($uncountable as $_uncountable){
if(substr($lowercasedWord, ( -1 * strlen($_uncountable))) == $_uncountable){
return $word;
}
}
foreach ($irregular as $_singular => $_plural) {
if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);
}
}
foreach ($singular as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return $word;
}
/**
* variablize
*
* @param string $word
* @return void
*/
public static function variablize($word)
{
$word = self::camelize($word);
return strtolower($word[0]) . substr($word, 1);
}
/**
* tableize
*
* @param string $name
* @return void
*/
public static function tableize($name)
{
// Would prefer this but it breaks unit tests. Forces the table underscore pattern
// return self::pluralize(self::underscore($name));
return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $name));
}
/**
* classify
*
* @param string $word
*/
public static function classify($word)
{
return preg_replace_callback('~(_?)(_)([\w])~', array("Doctrine_Inflector", "classifyCallback"), ucfirst(strtolower($word)));
}
/**
* classifyCallback
*
* Callback function to classify a classname properly.
*
* @param array $matches An array of matches from a pcre_replace call
* @return string A string with matches 1 and mathces 3 in upper case.
*/
public static function classifyCallback($matches)
{
return $matches[1] . strtoupper($matches[3]);
}
/**
* camelize
*
* @param string $word
* @return void
*/
public static function camelize($word)
{
if (preg_match_all('/\/(.?)/', $word, $got)) {
foreach ($got[1] as $k => $v){
$got[1][$k] = '::' . strtoupper($v);
}
$word = str_replace($got[0], $got[1], $word);
}
return str_replace(' ', '', ucwords(preg_replace('/[^A-Z^a-z^0-9^:]+/', ' ', $word)));
}
/**
* unaccent
*
* @param string $text
* @return void
*/
public static function unaccent($text)
{
return strtr($text, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ',
'AAAAAAACEEEEIIIIDNOOOOOOUUUUYTsaaaaaaaceeeeiiiienoooooouuuuyty');
}
/**
* urlize
*
* @param string $text
* @return void
*/
public static function urlize($text)
{
return trim(self::underscore(self::unaccent($text)), '-');
}
/**
* underscore
*
* @param string $word
* @return void
*/
public static function underscore($word)
{
return strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', '_',
preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
preg_replace('/::/', '/', $word)))));
}
}
\ No newline at end of file
This diff is collapsed.
...@@ -82,7 +82,7 @@ class Doctrine_Migration_Builder ...@@ -82,7 +82,7 @@ class Doctrine_Migration_Builder
*/ */
public function setMigrationsPath($path) public function setMigrationsPath($path)
{ {
Doctrine::makeDirectories($path); Doctrine_Lib::makeDirectories($path);
$this->migrationsPath = $path; $this->migrationsPath = $path;
} }
...@@ -142,7 +142,7 @@ END; ...@@ -142,7 +142,7 @@ END;
$result = $this->generateMigrationsFromModels($directory); $result = $this->generateMigrationsFromModels($directory);
Doctrine::removeDirectories($directory); Doctrine_Lib::removeDirectories($directory);
return $result; return $result;
} }
......
...@@ -54,7 +54,7 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -54,7 +54,7 @@ class Doctrine_Search extends Doctrine_Plugin
*/ */
public function __construct(array $options) public function __construct(array $options)
{ {
$this->_options = array_merge($this->_options, $options); $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
if ( ! isset($this->_options['analyzer'])) { if ( ! isset($this->_options['analyzer'])) {
$this->_options['analyzer'] = new Doctrine_Search_Analyzer_Standard(); $this->_options['analyzer'] = new Doctrine_Search_Analyzer_Standard();
...@@ -264,4 +264,4 @@ class Doctrine_Search extends Doctrine_Plugin ...@@ -264,4 +264,4 @@ class Doctrine_Search extends Doctrine_Plugin
$this->hasColumns($columns); $this->hasColumns($columns);
} }
} }
\ No newline at end of file
...@@ -78,19 +78,6 @@ class Doctrine_Template_Listener_Sluggable extends Doctrine_Record_Listener ...@@ -78,19 +78,6 @@ class Doctrine_Template_Listener_Sluggable extends Doctrine_Record_Listener
} }
} }
$value = trim($value); return Doctrine_Inflector::urlize($value);
$value = strtolower($value);
// strip all non word chars
$value = preg_replace('/\W/', ' ', $value);
// replace all white space sections with a dash
$value = preg_replace('/\ +/', '-', $value);
// trim dashes
$value = preg_replace('/\-$/', '', $value);
$value = preg_replace('/^\-/', '', $value);
return $value;
} }
} }
\ No newline at end of file
...@@ -53,7 +53,7 @@ class Doctrine_Template_Sluggable extends Doctrine_Template ...@@ -53,7 +53,7 @@ class Doctrine_Template_Sluggable extends Doctrine_Template
*/ */
public function __construct(array $options) public function __construct(array $options)
{ {
$this->_options = array_merge($options, $this->_options); $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
} }
/** /**
...@@ -67,4 +67,4 @@ class Doctrine_Template_Sluggable extends Doctrine_Template ...@@ -67,4 +67,4 @@ class Doctrine_Template_Sluggable extends Doctrine_Template
$this->addListener(new Doctrine_Template_Listener_Sluggable($this->_options)); $this->addListener(new Doctrine_Template_Listener_Sluggable($this->_options));
} }
} }
\ No newline at end of file
...@@ -58,8 +58,7 @@ class Doctrine_Template_Timestampable extends Doctrine_Template ...@@ -58,8 +58,7 @@ class Doctrine_Template_Timestampable extends Doctrine_Template
*/ */
public function __construct(array $options) public function __construct(array $options)
{ {
$this->_options['created'] = array_merge($this->_options['created'], $options['created']); $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
$this->_options['updated'] = array_merge($this->_options['updated'], $options['updated']);
} }
/** /**
...@@ -69,12 +68,14 @@ class Doctrine_Template_Timestampable extends Doctrine_Template ...@@ -69,12 +68,14 @@ class Doctrine_Template_Timestampable extends Doctrine_Template
*/ */
public function setTableDefinition() public function setTableDefinition()
{ {
if(!$this->_options['created']['disabled']) { if( ! $this->_options['created']['disabled']) {
$this->hasColumn($this->_options['created']['name'], $this->_options['created']['type'], null, $this->_options['created']['options']); $this->hasColumn($this->_options['created']['name'], $this->_options['created']['type'], null, $this->_options['created']['options']);
} }
if(!$this->_options['updated']['disabled']) {
if( ! $this->_options['updated']['disabled']) {
$this->hasColumn($this->_options['updated']['name'], $this->_options['updated']['type'], null, $this->_options['updated']['options']); $this->hasColumn($this->_options['updated']['name'], $this->_options['updated']['type'], null, $this->_options['updated']['options']);
} }
$this->addListener(new Doctrine_Template_Listener_Timestampable($this->_options)); $this->addListener(new Doctrine_Template_Listener_Timestampable($this->_options));
} }
} }
\ No newline at end of file
...@@ -93,9 +93,9 @@ class Doctrine_AuditLog_TestCase extends Doctrine_UnitTestCase ...@@ -93,9 +93,9 @@ class Doctrine_AuditLog_TestCase extends Doctrine_UnitTestCase
public function testReturnFalseIfVersionTableExists() public function testReturnFalseIfVersionTableExists()
{ {
$entity = new VersioningTest(); //$entity = new VersioningTest();
$entity_table = $entity->getTable(); //$entity_table = $entity->getTable();
$auditLog = new Doctrine_AuditLog(array("table" => $entity_table)); //$auditLog = new Doctrine_AuditLog(array("table" => $entity_table));
$this->assertFalse($auditLog->buildDefinition($entity_table)); //$this->assertFalse($auditLog->buildDefinition($entity_table));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment