Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema / AbstractAsset.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\DBAL\Schema;
21
22 use Doctrine\DBAL\Platforms\AbstractPlatform;
23
24 /**
25  * The abstract asset allows to reset the name of all assets without publishing this to the public userland.
26  *
27  * This encapsulation hack is necessary to keep a consistent state of the database schema. Say we have a list of tables
28  * array($tableName => Table($tableName)); if you want to rename the table, you have to make sure
29  *
30  *
31  * @link    www.doctrine-project.org
32  * @since   2.0
33  * @author  Benjamin Eberlei <kontakt@beberlei.de>
34  */
35 abstract class AbstractAsset
36 {
37     /**
38      * @var string
39      */
40     protected $_name;
41
42     /**
43      * Namespace of the asset. If none isset the default namespace is assumed.
44      *
45      * @var string
46      */
47     protected $_namespace;
48
49     /**
50      * @var bool
51      */
52     protected $_quoted = false;
53
54     /**
55      * Set name of this asset
56      *
57      * @param string $name
58      */
59     protected function _setName($name)
60     {
61         if ($this->isIdentifierQuoted($name)) {
62             $this->_quoted = true;
63             $name = $this->trimQuotes($name);
64         }
65         if (strpos($name, ".") !== false) {
66             $parts = explode(".", $name);
67             $this->_namespace = $parts[0];
68             $name = $parts[1];
69         }
70         $this->_name = $name;
71     }
72
73     /**
74      * Is this asset in the default namespace?
75      *
76      * @param string $defaultNamespaceName
77      * @return bool
78      */
79     public function isInDefaultNamespace($defaultNamespaceName)
80     {
81         return $this->_namespace == $defaultNamespaceName || $this->_namespace === null;
82     }
83
84     /**
85      * Get namespace name of this asset.
86      *
87      * If NULL is returned this means the default namespace is used.
88      *
89      * @return string
90      */
91     public function getNamespaceName()
92     {
93         return $this->_namespace;
94     }
95
96     /**
97      * The shortest name is stripped of the default namespace. All other
98      * namespaced elements are returned as full-qualified names.
99      *
100      * @param string
101      * @return string
102      */
103     public function getShortestName($defaultNamespaceName)
104     {
105         $shortestName = $this->getName();
106         if ($this->_namespace == $defaultNamespaceName) {
107             $shortestName = $this->_name;
108         }
109         return strtolower($shortestName);
110     }
111
112     /**
113      * The normalized name is full-qualified and lowerspaced. Lowerspacing is
114      * actually wrong, but we have to do it to keep our sanity. If you are
115      * using database objects that only differentiate in the casing (FOO vs
116      * Foo) then you will NOT be able to use Doctrine Schema abstraction.
117      *
118      * Every non-namespaced element is prefixed with the default namespace
119      * name which is passed as argument to this method.
120      *
121      * @return string
122      */
123     public function getFullQualifiedName($defaultNamespaceName)
124     {
125         $name = $this->getName();
126         if ( ! $this->_namespace) {
127             $name = $defaultNamespaceName . "." . $name;
128         }
129         return strtolower($name);
130     }
131
132     /**
133      * Check if this asset's name is quoted
134      *
135      * @return bool
136      */
137     public function isQuoted()
138     {
139         return $this->_quoted;
140     }
141
142     /**
143      * Check if this identifier is quoted.
144      *
145      * @param  string $identifier
146      * @return bool
147      */
148     protected function isIdentifierQuoted($identifier)
149     {
150         return (isset($identifier[0]) && ($identifier[0] == '`' || $identifier[0] == '"'));
151     }
152
153     /**
154      * Trim quotes from the identifier.
155      *
156      * @param  string $identifier
157      * @return string
158      */
159     protected function trimQuotes($identifier)
160     {
161         return str_replace(array('`', '"'), '', $identifier);
162     }
163
164     /**
165      * Return name of this schema asset.
166      *
167      * @return string
168      */
169     public function getName()
170     {
171         if ($this->_namespace) {
172             return $this->_namespace . "." . $this->_name;
173         }
174         return $this->_name;
175     }
176
177     /**
178      * Get the quoted representation of this asset but only if it was defined with one. Otherwise
179      * return the plain unquoted value as inserted.
180      *
181      * @param AbstractPlatform $platform
182      * @return string
183      */
184     public function getQuotedName(AbstractPlatform $platform)
185     {
186         $keywords = $platform->getReservedKeywordsList();
187         $parts = explode(".", $this->getName());
188         foreach ($parts as $k => $v) {
189             $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
190         }
191
192         return implode(".", $parts);
193     }
194
195     /**
196      * Generate an identifier from a list of column names obeying a certain string length.
197      *
198      * This is especially important for Oracle, since it does not allow identifiers larger than 30 chars,
199      * however building idents automatically for foreign keys, composite keys or such can easily create
200      * very long names.
201      *
202      * @param  array $columnNames
203      * @param  string $prefix
204      * @param  int $maxSize
205      * @return string
206      */
207     protected function _generateIdentifierName($columnNames, $prefix='', $maxSize=30)
208     {
209         $hash = implode("", array_map(function($column) {
210             return dechex(crc32($column));
211         }, $columnNames));
212         return substr(strtoupper($prefix . "_" . $hash), 0, $maxSize);
213     }
214 }