1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/lib/Doctrine/ORM/Mapping/UnderscoreNamingStrategy.php

139 lines
3.5 KiB
PHP
Raw Normal View History

2011-12-23 18:16:36 +04:00
<?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
2012-05-26 16:37:00 +04:00
* and is licensed under the MIT license. For more information, see
2011-12-23 18:16:36 +04:00
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Mapping;
2011-12-23 18:16:36 +04:00
/**
2011-12-27 15:53:09 +04:00
* Naming strategy implementing the underscore naming convention.
* Converts 'MyEntity' to 'my_entity' or 'MY_ENTITY'.
2011-12-23 18:16:36 +04:00
*
2012-05-26 16:37:00 +04:00
*
2011-12-23 18:16:36 +04:00
* @link www.doctrine-project.org
* @since 2.3
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/
class UnderscoreNamingStrategy implements NamingStrategy
{
/**
2011-12-27 15:53:09 +04:00
* @var integer
2011-12-23 18:16:36 +04:00
*/
private $case;
/**
2012-12-03 13:36:08 +04:00
* Underscore naming strategy construct.
2011-12-24 17:45:51 +04:00
*
* @param integer $case CASE_LOWER | CASE_UPPER
2011-12-23 18:16:36 +04:00
*/
2011-12-24 17:45:51 +04:00
public function __construct($case = CASE_LOWER)
2011-12-23 18:16:36 +04:00
{
$this->case = $case;
}
/**
2012-12-03 13:36:08 +04:00
* @return integer CASE_LOWER | CASE_UPPER
2011-12-23 18:16:36 +04:00
*/
public function getCase()
{
return $this->case;
}
/**
2012-12-03 13:36:08 +04:00
* Sets string case CASE_LOWER | CASE_UPPER.
* Alphabetic characters converted to lowercase or uppercase.
2011-12-24 17:45:51 +04:00
*
* @param integer $case
2012-12-03 13:36:08 +04:00
*
* @return void
2011-12-23 18:16:36 +04:00
*/
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, $className = null)
2011-12-23 18:16:36 +04:00
{
return $this->underscore($propertyName);
}
/**
* {@inheritdoc}
*/
public function referenceColumnName()
{
2011-12-24 17:45:51 +04:00
return $this->case === CASE_UPPER ? 'ID' : 'id';
2011-12-23 18:16:36 +04:00
}
/**
* {@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());
}
/**
2011-12-24 17:45:51 +04:00
* @param string $string
2012-12-03 13:36:08 +04:00
*
2011-12-24 17:45:51 +04:00
* @return string
2011-12-23 18:16:36 +04:00
*/
private function underscore($string)
{
$string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string);
2011-12-23 18:16:36 +04:00
2011-12-24 17:45:51 +04:00
if ($this->case === CASE_UPPER) {
2011-12-23 18:16:36 +04:00
return strtoupper($string);
}
2011-12-24 17:45:51 +04:00
return strtolower($string);
2011-12-23 18:16:36 +04:00
}
2012-05-26 16:37:00 +04:00
}