Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Mapping / Builder / ClassMetadataBuilder.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\ORM\Mapping\Builder;
21
22 use Doctrine\ORM\Mapping\ClassMetadata,
23     Doctrine\ORM\Mapping\ClassMetadataInfo;
24
25 /**
26  * Builder Object for ClassMetadata
27  *
28  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29  * @link        www.doctrine-project.com
30  * @since       2.2
31  * @author      Benjamin Eberlei <kontakt@beberlei.de>
32  * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
33  */
34 class ClassMetadataBuilder
35 {
36     /**
37      * @var \Doctrine\ORM\Mapping\ClassMetadataInfo
38      */
39     private $cm;
40
41     /**
42      * @param \Doctrine\ORM\Mapping\ClassMetadataInfo $cm
43      */
44     public function __construct(ClassMetadataInfo $cm)
45     {
46         $this->cm = $cm;
47     }
48
49     /**
50      * @return ClassMetadata
51      */
52     public function getClassMetadata()
53     {
54         return $this->cm;
55     }
56
57     /**
58      * Mark the class as mapped superclass.
59      *
60      * @return ClassMetadataBuilder
61      */
62     public function setMappedSuperClass()
63     {
64         $this->cm->isMappedSuperclass = true;
65
66         return $this;
67     }
68
69     /**
70      * Set custom Repository class name
71      *
72      * @param string $repositoryClassName
73      * @return ClassMetadataBuilder
74      */
75     public function setCustomRepositoryClass($repositoryClassName)
76     {
77         $this->cm->setCustomRepositoryClass($repositoryClassName);
78
79         return $this;
80     }
81
82     /**
83      * Mark class read only
84      *
85      * @return ClassMetadataBuilder
86      */
87     public function setReadOnly()
88     {
89         $this->cm->markReadOnly();
90
91         return $this;
92     }
93
94     /**
95      * Set the table name
96      *
97      * @param string $name
98      * @return ClassMetadataBuilder
99      */
100     public function setTable($name)
101     {
102         $this->cm->setPrimaryTable(array('name' => $name));
103
104         return $this;
105     }
106
107     /**
108      * Add Index
109      *
110      * @param array $columns
111      * @param string $name
112      * @return ClassMetadataBuilder
113      */
114     public function addIndex(array $columns, $name)
115     {
116         if (!isset($this->cm->table['indexes'])) {
117             $this->cm->table['indexes'] = array();
118         }
119
120         $this->cm->table['indexes'][$name] = array('columns' => $columns);
121
122         return $this;
123     }
124
125     /**
126      * Add Unique Constraint
127      *
128      * @param array $columns
129      * @param string $name
130      * @return ClassMetadataBuilder
131      */
132     public function addUniqueConstraint(array $columns, $name)
133     {
134         if ( ! isset($this->cm->table['uniqueConstraints'])) {
135             $this->cm->table['uniqueConstraints'] = array();
136         }
137
138         $this->cm->table['uniqueConstraints'][$name] = array('columns' => $columns);
139
140         return $this;
141     }
142
143     /**
144      * Add named query
145      *
146      * @param string $name
147      * @param string $dqlQuery
148      * @return ClassMetadataBuilder
149      */
150     public function addNamedQuery($name, $dqlQuery)
151     {
152         $this->cm->addNamedQuery(array(
153             'name' => $name,
154             'query' => $dqlQuery,
155         ));
156
157         return $this;
158     }
159
160     /**
161      * Set class as root of a joined table inheritance hierachy.
162      *
163      * @return ClassMetadataBuilder
164      */
165     public function setJoinedTableInheritance()
166     {
167         $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_JOINED);
168
169         return $this;
170     }
171
172     /**
173      * Set class as root of a single table inheritance hierachy.
174      *
175      * @return ClassMetadataBuilder
176      */
177     public function setSingleTableInheritance()
178     {
179         $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
180
181         return $this;
182     }
183
184     /**
185      * Set the discriminator column details.
186      *
187      * @param string $name
188      * @param string $type
189      */
190     public function setDiscriminatorColumn($name, $type = 'string', $length = 255)
191     {
192         $this->cm->setDiscriminatorColumn(array(
193             'name' => $name,
194             'type' => $type,
195             'length' => $length,
196         ));
197
198         return $this;
199     }
200
201     /**
202      * Add a subclass to this inheritance hierachy.
203      *
204      * @param string $name
205      * @param string $class
206      * @return ClassMetadataBuilder
207      */
208     public function addDiscriminatorMapClass($name, $class)
209     {
210         $this->cm->addDiscriminatorMapClass($name, $class);
211
212         return $this;
213     }
214
215     /**
216      * Set deferred explicit change tracking policy.
217      *
218      * @return ClassMetadataBuilder
219      */
220     public function setChangeTrackingPolicyDeferredExplicit()
221     {
222         $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT);
223
224         return $this;
225     }
226
227     /**
228      * Set notify change tracking policy.
229      *
230      * @return ClassMetadataBuilder
231      */
232     public function setChangeTrackingPolicyNotify()
233     {
234         $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_NOTIFY);
235
236         return $this;
237     }
238
239     /**
240      * Add lifecycle event
241      *
242      * @param string $methodName
243      * @param string $event
244      * @return ClassMetadataBuilder
245      */
246     public function addLifecycleEvent($methodName, $event)
247     {
248         $this->cm->addLifecycleCallback($methodName, $event);
249
250         return $this;
251     }
252
253     /**
254      * Add Field
255      *
256      * @param string $name
257      * @param string $type
258      * @param array $mapping
259      */
260     public function addField($name, $type, array $mapping = array())
261     {
262         $mapping['fieldName'] = $name;
263         $mapping['type'] = $type;
264
265         $this->cm->mapField($mapping);
266
267         return $this;
268     }
269
270     /**
271      * Create a field builder.
272      *
273      * @param string $name
274      * @param string $type
275      * @return FieldBuilder
276      */
277     public function createField($name, $type)
278     {
279         return new FieldBuilder(
280             $this,
281             array(
282                 'fieldName' => $name,
283                 'type'      => $type
284             )
285         );
286     }
287
288     /**
289      * Add a simple many to one association, optionally with the inversed by field.
290      *
291      * @param string $name
292      * @param string $targetEntity
293      * @param string|null $inversedBy
294      * @return ClassMetadataBuilder
295      */
296     public function addManyToOne($name, $targetEntity, $inversedBy = null)
297     {
298         $builder = $this->createManyToOne($name, $targetEntity);
299
300         if ($inversedBy) {
301             $builder->inversedBy($inversedBy);
302         }
303
304         return $builder->build();
305     }
306
307     /**
308      * Create a ManyToOne Assocation Builder.
309      *
310      * Note: This method does not add the association, you have to call build() on the AssociationBuilder.
311      *
312      * @param string $name
313      * @param string $targetEntity
314      * @return AssociationBuilder
315      */
316     public function createManyToOne($name, $targetEntity)
317     {
318         return new AssociationBuilder(
319             $this,
320             array(
321                 'fieldName'    => $name,
322                 'targetEntity' => $targetEntity
323             ),
324             ClassMetadata::MANY_TO_ONE
325         );
326     }
327
328     /**
329      * Create OneToOne Assocation Builder
330      *
331      * @param string $name
332      * @param string $targetEntity
333      * @return AssociationBuilder
334      */
335     public function createOneToOne($name, $targetEntity)
336     {
337         return new AssociationBuilder(
338             $this,
339             array(
340                 'fieldName'    => $name,
341                 'targetEntity' => $targetEntity
342             ),
343             ClassMetadata::ONE_TO_ONE
344         );
345     }
346
347     /**
348      * Add simple inverse one-to-one assocation.
349      *
350      * @param string $name
351      * @param string $targetEntity
352      * @param string $mappedBy
353      * @return ClassMetadataBuilder
354      */
355     public function addInverseOneToOne($name, $targetEntity, $mappedBy)
356     {
357         $builder = $this->createOneToOne($name, $targetEntity);
358         $builder->mappedBy($mappedBy);
359
360         return $builder->build();
361     }
362
363     /**
364      * Add simple owning one-to-one assocation.
365      *
366      * @param string $name
367      * @param string $targetEntity
368      * @param string $inversedBy
369      * @return ClassMetadataBuilder
370      */
371     public function addOwningOneToOne($name, $targetEntity, $inversedBy = null)
372     {
373         $builder = $this->createOneToOne($name, $targetEntity);
374
375         if ($inversedBy) {
376             $builder->inversedBy($inversedBy);
377         }
378
379         return $builder->build();
380     }
381
382     /**
383      * Create ManyToMany Assocation Builder
384      *
385      * @param string $name
386      * @param string $targetEntity
387      * @return ManyToManyAssociationBuilder
388      */
389     public function createManyToMany($name, $targetEntity)
390     {
391         return new ManyToManyAssociationBuilder(
392             $this,
393             array(
394                 'fieldName'    => $name,
395                 'targetEntity' => $targetEntity
396             ),
397             ClassMetadata::MANY_TO_MANY
398         );
399     }
400
401     /**
402      * Add a simple owning many to many assocation.
403      *
404      * @param string $name
405      * @param string $targetEntity
406      * @param string|null $inversedBy
407      * @return ClassMetadataBuilder
408      */
409     public function addOwningManyToMany($name, $targetEntity, $inversedBy = null)
410     {
411         $builder = $this->createManyToMany($name, $targetEntity);
412
413         if ($inversedBy) {
414             $builder->inversedBy($inversedBy);
415         }
416
417         return $builder->build();
418     }
419
420     /**
421      * Add a simple inverse many to many assocation.
422      *
423      * @param string $name
424      * @param string $targetEntity
425      * @param string $mappedBy
426      * @return ClassMetadataBuilder
427      */
428     public function addInverseManyToMany($name, $targetEntity, $mappedBy)
429     {
430         $builder = $this->createManyToMany($name, $targetEntity);
431         $builder->mappedBy($mappedBy);
432
433         return $builder->build();
434     }
435
436     /**
437      * Create a one to many assocation builder
438      *
439      * @param string $name
440      * @param string $targetEntity
441      * @return OneToManyAssociationBuilder
442      */
443     public function createOneToMany($name, $targetEntity)
444     {
445         return new OneToManyAssociationBuilder(
446             $this,
447             array(
448                 'fieldName'    => $name,
449                 'targetEntity' => $targetEntity
450             ),
451             ClassMetadata::ONE_TO_MANY
452         );
453     }
454
455     /**
456      * Add simple OneToMany assocation.
457      *
458      * @param string $name
459      * @param string $targetEntity
460      * @param string $mappedBy
461      * @return ClassMetadataBuilder
462      */
463     public function addOneToMany($name, $targetEntity, $mappedBy)
464     {
465         $builder = $this->createOneToMany($name, $targetEntity);
466         $builder->mappedBy($mappedBy);
467
468         return $builder->build();
469     }
470 }