From 8b1f60c9f8b3efd2f36b357f21cdc085cb57bcc9 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Fri, 23 Dec 2011 12:16:36 -0200 Subject: [PATCH] add UnderscoreNamingStrategy --- lib/Doctrine/ORM/UnderscoreNamingStrategy.php | 133 ++++++++++++++++++ .../Doctrine/Tests/ORM/NamingStrategyTest.php | 106 ++++++++++++-- 2 files changed, 227 insertions(+), 12 deletions(-) create mode 100644 lib/Doctrine/ORM/UnderscoreNamingStrategy.php diff --git a/lib/Doctrine/ORM/UnderscoreNamingStrategy.php b/lib/Doctrine/ORM/UnderscoreNamingStrategy.php new file mode 100644 index 000000000..409b4f0ee --- /dev/null +++ b/lib/Doctrine/ORM/UnderscoreNamingStrategy.php @@ -0,0 +1,133 @@ +. + */ + +namespace Doctrine\ORM; +use Doctrine\Common\Util\Inflector; + +/** + * The default NamingStrategy + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.3 + * @author Fabio B. Silva + */ +class UnderscoreNamingStrategy implements NamingStrategy +{ + const CASE_LOWER = 'lower'; + const CASE_UPPER = 'upper'; + + /** + * @var string + */ + private $case; + + /** + * @param string $case + */ + public function __construct($case = self::CASE_LOWER) + { + $this->case = $case; + } + + /** + * @return string + */ + public function getCase() + { + return $this->case; + } + + /** + * @param string $case + */ + public function setCase($case) + { + $this->case = $case; + } + + /** + * {@inheritdoc} + */ + public function classToTableName($className) + { + if (strpos($className, '\\') !== false) { + $className = substr($className, strrpos($className, '\\') + 1); + } + + return $this->underscore($className); + } + + /** + * {@inheritdoc} + */ + public function propertyToColumnName($propertyName) + { + return $this->underscore($propertyName); + } + + /** + * {@inheritdoc} + */ + public function referenceColumnName() + { + return $this->case == self::CASE_UPPER ? 'ID' : 'id'; + } + + /** + * {@inheritdoc} + */ + public function joinColumnName($propertyName) + { + return $this->underscore($propertyName) . '_' . $this->referenceColumnName(); + } + + /** + * {@inheritdoc} + */ + public function joinTableName($sourceEntity, $targetEntity, $propertyName = null) + { + return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity); + } + + /** + * {@inheritdoc} + */ + public function joinKeyColumnName($entityName, $referencedColumnName = null) + { + return $this->classToTableName($entityName) . '_' . + ($referencedColumnName ?: $this->referenceColumnName()); + } + + /** + * @param string $string + * @return string + */ + private function underscore($string) + { + $string = Inflector::tableize($string); + + if ($this->case == self::CASE_UPPER) { + return strtoupper($string); + } + + return $string; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/NamingStrategyTest.php b/tests/Doctrine/Tests/ORM/NamingStrategyTest.php index f17459118..bf4ab7254 100644 --- a/tests/Doctrine/Tests/ORM/NamingStrategyTest.php +++ b/tests/Doctrine/Tests/ORM/NamingStrategyTest.php @@ -2,6 +2,7 @@ namespace Doctrine\Tests\ORM; +use Doctrine\ORM\UnderscoreNamingStrategy; use Doctrine\ORM\DefaultNamingStrategy; use Doctrine\ORM\NamingStrategy; @@ -12,20 +13,28 @@ require_once __DIR__ . '/../TestInit.php'; */ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase { - /** - * @var DefaultNamingStrategy - */ - private static $defaultNamingStrategy; - /** * @return DefaultNamingStrategy */ static private function defaultNaming() { - if (self::$defaultNamingStrategy == null) { - self::$defaultNamingStrategy = new DefaultNamingStrategy(); - } - return self::$defaultNamingStrategy; + return new DefaultNamingStrategy(); + } + + /** + * @return UnderscoreNamingStrategy + */ + static private function underscoreNamingLower() + { + return new UnderscoreNamingStrategy(UnderscoreNamingStrategy::CASE_LOWER); + } + + /** + * @return UnderscoreNamingStrategy + */ + static private function underscoreNamingUpper() + { + return new UnderscoreNamingStrategy(UnderscoreNamingStrategy::CASE_UPPER); } /** @@ -36,6 +45,7 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase static public function dataClassToTableName() { return array( + // DefaultNamingStrategy array(self::defaultNaming(), 'SomeClassName', 'SomeClassName' ), @@ -45,6 +55,20 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase array(self::defaultNaming(), 'Name', '\Some\Class\Name' ), + + // UnderscoreNamingStrategy + array(self::underscoreNamingLower(), 'some_class_name', + '\Name\Space\SomeClassName' + ), + array(self::underscoreNamingLower(), 'name', + '\Some\Class\Name' + ), + array(self::underscoreNamingUpper(), 'SOME_CLASS_NAME', + '\Name\Space\SomeClassName' + ), + array(self::underscoreNamingUpper(), 'NAME', + '\Some\Class\Name' + ), ); } @@ -64,6 +88,7 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase static public function dataPropertyToColumnName() { return array( + // DefaultNamingStrategy array(self::defaultNaming(), 'someProperty', 'someProperty' ), @@ -73,6 +98,14 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase array(self::defaultNaming(), 'some_property', 'some_property' ), + + // UnderscoreNamingStrategy + array(self::underscoreNamingLower(), 'some_property', + 'someProperty' + ), + array(self::underscoreNamingUpper(), 'SOME_PROPERTY', + 'someProperty' + ), ); } @@ -96,7 +129,12 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase static public function dataReferenceColumnName() { return array( + // DefaultNamingStrategy array(self::defaultNaming(), 'id'), + + // UnderscoreNamingStrategy + array(self::underscoreNamingLower(), 'id'), + array(self::underscoreNamingUpper(), 'ID'), ); } @@ -121,15 +159,21 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase static public function dataJoinColumnName() { return array( + // DefaultNamingStrategy array(self::defaultNaming(), 'someColumn_id', 'someColumn', null, ), - array(self::defaultNaming(), 'somecolumn_id', - 'somecolumn', null, - ), array(self::defaultNaming(), 'some_column_id', 'some_column', null, ), + + // UnderscoreNamingStrategy + array(self::underscoreNamingLower(), 'some_column_id', + 'someColumn', null, + ), + array(self::underscoreNamingUpper(), 'SOME_COLUMN_ID', + 'someColumn', null, + ), ); } @@ -153,6 +197,7 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase static public function dataJoinTableName() { return array( + // DefaultNamingStrategy array(self::defaultNaming(), 'someclassname_classname', 'SomeClassName', 'Some\ClassName', null, ), @@ -162,6 +207,27 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase array(self::defaultNaming(), 'name_classname', '\Some\Class\Name', 'ClassName', null, ), + + // UnderscoreNamingStrategy + array(self::underscoreNamingLower(), 'some_class_name_class_name', + 'SomeClassName', 'Some\ClassName', null, + ), + array(self::underscoreNamingLower(), 'some_class_name_class_name', + '\SomeClassName', 'ClassName', null, + ), + array(self::underscoreNamingLower(), 'name_class_name', + '\Some\Class\Name', 'ClassName', null, + ), + + array(self::underscoreNamingUpper(), 'SOME_CLASS_NAME_CLASS_NAME', + 'SomeClassName', 'Some\ClassName', null, + ), + array(self::underscoreNamingUpper(), 'SOME_CLASS_NAME_CLASS_NAME', + '\SomeClassName', 'ClassName', null, + ), + array(self::underscoreNamingUpper(), 'NAME_CLASS_NAME', + '\Some\Class\Name', 'ClassName', null, + ), ); } @@ -187,12 +253,28 @@ class NamingStrategyTest extends \Doctrine\Tests\OrmTestCase static public function dataJoinKeyColumnName() { return array( + // DefaultNamingStrategy array(self::defaultNaming(), 'someclassname_id', 'SomeClassName', null, null, ), array(self::defaultNaming(), 'name_identifier', '\Some\Class\Name', 'identifier', null, ), + + // UnderscoreNamingStrategy + array(self::underscoreNamingLower(), 'some_class_name_id', + 'SomeClassName', null, null, + ), + array(self::underscoreNamingLower(), 'class_name_identifier', + '\Some\Class\ClassName', 'identifier', null, + ), + + array(self::underscoreNamingUpper(), 'SOME_CLASS_NAME_ID', + 'SomeClassName', null, null, + ), + array(self::underscoreNamingUpper(), 'CLASS_NAME_IDENTIFIER', + '\Some\Class\ClassName', 'IDENTIFIER', null, + ), ); }