4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the MIT license. For more information, see
18 * <http://www.doctrine-project.org>.
21 namespace Doctrine\ORM\Mapping;
24 * Naming strategy implementing the underscore naming convention.
25 * Converts 'MyEntity' to 'my_entity' or 'MY_ENTITY'.
28 * @link www.doctrine-project.org
30 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
32 class UnderscoreNamingStrategy implements NamingStrategy
40 * Underscore naming strategy construct
42 * @param integer $case CASE_LOWER | CASE_UPPER
44 public function __construct($case = CASE_LOWER)
52 public function getCase()
58 * Sets string case CASE_LOWER | CASE_UPPER
59 * Alphabetic characters converted to lowercase or uppercase
61 * @param integer $case
63 public function setCase($case)
71 public function classToTableName($className)
73 if (strpos($className, '\\') !== false) {
74 $className = substr($className, strrpos($className, '\\') + 1);
77 return $this->underscore($className);
83 public function propertyToColumnName($propertyName)
85 return $this->underscore($propertyName);
91 public function referenceColumnName()
93 return $this->case === CASE_UPPER ? 'ID' : 'id';
99 public function joinColumnName($propertyName)
101 return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
107 public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
109 return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity);
115 public function joinKeyColumnName($entityName, $referencedColumnName = null)
117 return $this->classToTableName($entityName) . '_' .
118 ($referencedColumnName ?: $this->referenceColumnName());
122 * @param string $string
125 private function underscore($string)
127 $string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string);
129 if ($this->case === CASE_UPPER) {
130 return strtoupper($string);
133 return strtolower($string);