1
0
mirror of synced 2024-12-14 07:06:04 +03:00

add UnderscoreNamingStrategy

This commit is contained in:
Fabio B. Silva 2011-12-23 12:16:36 -02:00
parent 537821418e
commit 8b1f60c9f8
2 changed files with 227 additions and 12 deletions

View File

@ -0,0 +1,133 @@
<?php
/*
* 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.doctrine-project.org>.
*/
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 <fabio.bat.silva@gmail.com>
*/
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;
}
}

View File

@ -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,
),
);
}