Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema / ForeignKeyConstraint.php
1 <?php
2 /*
3  *  $Id$
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the MIT license. For more information, see
19  * <http://www.doctrine-project.org>.
20  */
21
22 namespace Doctrine\DBAL\Schema;
23
24 use Doctrine\DBAL\Schema\Visitor\Visitor;
25 use Doctrine\DBAL\Platforms\AbstractPlatform;
26
27 class ForeignKeyConstraint extends AbstractAsset implements Constraint
28 {
29     /**
30      * @var Table
31      */
32     protected $_localTable;
33
34     /**
35      * @var array
36      */
37     protected $_localColumnNames;
38
39     /**
40      * @var string
41      */
42     protected $_foreignTableName;
43
44     /**
45      * @var array
46      */
47     protected $_foreignColumnNames;
48
49     /**
50      * @var string
51      */
52     protected $_cascade = '';
53
54     /**
55      * @var array
56      */
57     protected $_options;
58
59     /**
60      *
61      * @param array $localColumnNames
62      * @param string $foreignTableName
63      * @param array $foreignColumnNames
64      * @param string $cascade
65      * @param string|null $name
66      */
67     public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name=null, array $options=array())
68     {
69         $this->_setName($name);
70         $this->_localColumnNames = $localColumnNames;
71         $this->_foreignTableName = $foreignTableName;
72         $this->_foreignColumnNames = $foreignColumnNames;
73         $this->_options = $options;
74     }
75
76     /**
77      * @return string
78      */
79     public function getLocalTableName()
80     {
81         return $this->_localTable->getName();
82     }
83
84     /**
85      * @param Table $table
86      */
87     public function setLocalTable(Table $table)
88     {
89         $this->_localTable = $table;
90     }
91
92     /**
93      * @return array
94      */
95     public function getLocalColumns()
96     {
97         return $this->_localColumnNames;
98     }
99
100     public function getColumns()
101     {
102         return $this->_localColumnNames;
103     }
104
105     /**
106      * @return string
107      */
108     public function getForeignTableName()
109     {
110         return $this->_foreignTableName;
111     }
112
113     /**
114      * Get the quoted representation of this asset but only if it was defined with one. Otherwise
115      * return the plain unquoted value as inserted.
116      *
117      * @param AbstractPlatform $platform
118      * @return string
119      */
120     public function getQuotedForeignTableName(AbstractPlatform $platform)
121     {
122         $keywords = $platform->getReservedKeywordsList();
123         $parts = explode(".", $this->getForeignTableName());
124         foreach ($parts AS $k => $v) {
125             $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
126         }
127
128         return implode(".", $parts);
129     }
130
131     /**
132      * @return array
133      */
134     public function getForeignColumns()
135     {
136         return $this->_foreignColumnNames;
137     }
138
139     public function hasOption($name)
140     {
141         return isset($this->_options[$name]);
142     }
143
144     public function getOption($name)
145     {
146         return $this->_options[$name];
147     }
148
149     /**
150      * Gets the options associated with this constraint
151      *
152      * @return array
153      */
154     public function getOptions()
155     {
156         return $this->_options;
157     }
158
159     /**
160      * Foreign Key onUpdate status
161      *
162      * @return string|null
163      */
164     public function onUpdate()
165     {
166         return $this->_onEvent('onUpdate');
167     }
168
169     /**
170      * Foreign Key onDelete status
171      *
172      * @return string|null
173      */
174     public function onDelete()
175     {
176         return $this->_onEvent('onDelete');
177     }
178
179     /**
180      * @param  string $event
181      * @return string|null
182      */
183     private function _onEvent($event)
184     {
185         if (isset($this->_options[$event])) {
186             $onEvent = strtoupper($this->_options[$event]);
187             if (!in_array($onEvent, array('NO ACTION', 'RESTRICT'))) {
188                 return $onEvent;
189             }
190         }
191         return false;
192     }
193 }