Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Mapping / Builder / FieldBuilder.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
21 namespace Doctrine\ORM\Mapping\Builder;
22
23 /**
24  * Field Builder
25  *
26  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
27  * @link        www.doctrine-project.com
28  * @since       2.2
29  * @author      Benjamin Eberlei <kontakt@beberlei.de>
30  */
31 class FieldBuilder
32 {
33     /**
34      * @var ClassMetadataBuilder
35      */
36     private $builder;
37     /**
38      * @var array
39      */
40     private $mapping;
41     /**
42      * @var bool
43      */
44     private $version;
45
46     /**
47      * @var string
48      */
49     private $generatedValue;
50
51     /**
52      * @var array
53      */
54     private $sequenceDef;
55
56     /**
57      *
58      * @param ClassMetadataBuilder $builder
59      * @param array $mapping
60      */
61     public function __construct(ClassMetadataBuilder $builder, array $mapping)
62     {
63         $this->builder = $builder;
64         $this->mapping = $mapping;
65     }
66
67     /**
68      * Set length.
69      *
70      * @param int $length
71      * @return FieldBuilder
72      */
73     public function length($length)
74     {
75         $this->mapping['length'] = $length;
76         return $this;
77     }
78
79     /**
80      * Set nullable
81      *
82      * @param bool
83      * @return FieldBuilder
84      */
85     public function nullable($flag = true)
86     {
87         $this->mapping['nullable'] = (bool)$flag;
88         return $this;
89     }
90
91     /**
92      * Set Unique
93      *
94      * @param bool
95      * @return FieldBuilder
96      */
97     public function unique($flag = true)
98     {
99         $this->mapping['unique'] = (bool)$flag;
100         return $this;
101     }
102
103     /**
104      * Set column name
105      *
106      * @param string $name
107      * @return FieldBuilder
108      */
109     public function columnName($name)
110     {
111         $this->mapping['columnName'] = $name;
112         return $this;
113     }
114
115     /**
116      * Set Precision
117      *
118      * @param  int $p
119      * @return FieldBuilder
120      */
121     public function precision($p)
122     {
123         $this->mapping['precision'] = $p;
124         return $this;
125     }
126
127     /**
128      * Set scale.
129      *
130      * @param int $s
131      * @return FieldBuilder
132      */
133     public function scale($s)
134     {
135         $this->mapping['scale'] = $s;
136         return $this;
137     }
138
139     /**
140      * Set field as primary key.
141      *
142      * @return FieldBuilder
143      */
144     public function isPrimaryKey()
145     {
146         $this->mapping['id'] = true;
147         return $this;
148     }
149
150     /**
151      * @param  int $strategy
152      * @return FieldBuilder
153      */
154     public function generatedValue($strategy = 'AUTO')
155     {
156         $this->generatedValue = $strategy;
157         return $this;
158     }
159
160     /**
161      * Set field versioned
162      *
163      * @return FieldBuilder
164      */
165     public function isVersionField()
166     {
167         $this->version = true;
168         return $this;
169     }
170
171     /**
172      * Set Sequence Generator
173      *
174      * @param string $sequenceName
175      * @param int $allocationSize
176      * @param int $initialValue
177      * @return FieldBuilder
178      */
179     public function setSequenceGenerator($sequenceName, $allocationSize = 1, $initialValue = 1)
180     {
181         $this->sequenceDef = array(
182             'sequenceName' => $sequenceName,
183             'allocationSize' => $allocationSize,
184             'initialValue' => $initialValue,
185         );
186         return $this;
187     }
188
189     /**
190      * Set column definition.
191      *
192      * @param string $def
193      * @return FieldBuilder
194      */
195     public function columnDefinition($def)
196     {
197         $this->mapping['columnDefinition'] = $def;
198         return $this;
199     }
200
201     /**
202      * Finalize this field and attach it to the ClassMetadata.
203      *
204      * Without this call a FieldBuilder has no effect on the ClassMetadata.
205      *
206      * @return ClassMetadataBuilder
207      */
208     public function build()
209     {
210         $cm = $this->builder->getClassMetadata();
211         if ($this->generatedValue) {
212             $cm->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $this->generatedValue));
213         }
214         if ($this->version) {
215             $cm->setVersionMapping($this->mapping);
216         }
217         $cm->mapField($this->mapping);
218         if ($this->sequenceDef) {
219             $cm->setSequenceGeneratorDefinition($this->sequenceDef);
220         }
221         return $this->builder;
222     }
223 }