Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Mapping / UnderscoreNamingStrategy.php
1 <?php
2
3 /*
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.
15  *
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>.
19  */
20
21 namespace Doctrine\ORM\Mapping;
22
23 /**
24  * Naming strategy implementing the underscore naming convention.
25  * Converts 'MyEntity' to 'my_entity' or 'MY_ENTITY'.
26  *
27  * 
28  * @link    www.doctrine-project.org
29  * @since   2.3
30  * @author  Fabio B. Silva <fabio.bat.silva@gmail.com>
31  */
32 class UnderscoreNamingStrategy implements NamingStrategy
33 {
34     /**
35      * @var integer
36      */
37     private $case;
38
39     /**
40      * Underscore naming strategy construct
41      *
42      * @param integer $case CASE_LOWER | CASE_UPPER
43      */
44     public function __construct($case = CASE_LOWER)
45     {
46         $this->case = $case;
47     }
48
49     /**
50      * @return integer
51      */
52     public function getCase()
53     {
54         return $this->case;
55     }
56
57     /**
58      * Sets string case CASE_LOWER | CASE_UPPER
59      * Alphabetic characters converted to lowercase or uppercase
60      * 
61      * @param integer $case
62      */
63     public function setCase($case)
64     {
65         $this->case = $case;
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function classToTableName($className)
72     {
73         if (strpos($className, '\\') !== false) {
74             $className = substr($className, strrpos($className, '\\') + 1);
75         }
76
77         return $this->underscore($className);
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     public function propertyToColumnName($propertyName)
84     {
85         return $this->underscore($propertyName);
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     public function referenceColumnName()
92     {
93         return $this->case === CASE_UPPER ?  'ID' : 'id';
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     public function joinColumnName($propertyName)
100     {
101         return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
108     {
109         return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity);
110     }
111     
112     /**
113      * {@inheritdoc}
114      */
115     public function joinKeyColumnName($entityName, $referencedColumnName = null)
116     {
117         return $this->classToTableName($entityName) . '_' .
118                 ($referencedColumnName ?: $this->referenceColumnName());
119     }
120     
121     /**
122      * @param string $string
123      * @return string
124      */
125     private function underscore($string)
126     {
127         $string = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $string);
128
129         if ($this->case === CASE_UPPER) {
130             return strtoupper($string);
131         }
132
133         return strtolower($string);
134     }
135 }