Completed
Push — master ( 2a239b...205ee7 )
by Marco
22s
created

ORM/Mapping/Builder/ClassMetadataBuilder.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use Doctrine\ORM\Mapping\ClassMetadataInfo;
24
25
/**
26
 * Builder Object for ClassMetadata
27
 *
28
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
29
 * @link        www.doctrine-project.com
30
 * @since       2.2
31
 * @author      Benjamin Eberlei <[email protected]>
32
 * @author      Guilherme Blanco <[email protected]>
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 38
    public function __construct(ClassMetadataInfo $cm)
45
    {
46 38
        $this->cm = $cm;
47 38
    }
48
49
    /**
50
     * @return ClassMetadata
51
     */
52 20
    public function getClassMetadata()
53
    {
54 20
        return $this->cm;
55
    }
56
57
    /**
58
     * Marks the class as mapped superclass.
59
     *
60
     * @return ClassMetadataBuilder
61
     */
62 1
    public function setMappedSuperClass()
63
    {
64 1
        $this->cm->isMappedSuperclass = true;
65 1
        $this->cm->isEmbeddedClass = false;
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * Marks the class as embeddable.
72
     *
73
     * @return ClassMetadataBuilder
74
     */
75 1
    public function setEmbeddable()
76
    {
77 1
        $this->cm->isEmbeddedClass = true;
78 1
        $this->cm->isMappedSuperclass = false;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * Adds and embedded class
85
     *
86
     * @param string      $fieldName
87
     * @param string      $class
88
     * @param string|null $columnPrefix
89
     *
90
     * @return $this
91
     */
92 2
    public function addEmbedded($fieldName, $class, $columnPrefix = null)
93
    {
94 2
        $this->cm->mapEmbedded(
95
            [
96 2
                'fieldName'    => $fieldName,
97 2
                'class'        => $class,
98 2
                'columnPrefix' => $columnPrefix
99
            ]
100
        );
101
102 2
        return $this;
103
    }
104
105
    /**
106
     * Sets custom Repository class name.
107
     *
108
     * @param string $repositoryClassName
109
     *
110
     * @return ClassMetadataBuilder
111
     */
112 1
    public function setCustomRepositoryClass($repositoryClassName)
113
    {
114 1
        $this->cm->setCustomRepositoryClass($repositoryClassName);
115
116 1
        return $this;
117
    }
118
119
    /**
120
     * Marks class read only.
121
     *
122
     * @return ClassMetadataBuilder
123
     */
124 1
    public function setReadOnly()
125
    {
126 1
        $this->cm->markReadOnly();
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * Sets the table name.
133
     *
134
     * @param string $name
135
     *
136
     * @return ClassMetadataBuilder
137
     */
138 2
    public function setTable($name)
139
    {
140 2
        $this->cm->setPrimaryTable(['name' => $name]);
141
142 2
        return $this;
143
    }
144
145
    /**
146
     * Adds Index.
147
     *
148
     * @param array  $columns
149
     * @param string $name
150
     *
151
     * @return ClassMetadataBuilder
152
     */
153 2
    public function addIndex(array $columns, $name)
154
    {
155 2
        if (!isset($this->cm->table['indexes'])) {
156 2
            $this->cm->table['indexes'] = [];
157
        }
158
159 2
        $this->cm->table['indexes'][$name] = ['columns' => $columns];
160
161 2
        return $this;
162
    }
163
164
    /**
165
     * Adds Unique Constraint.
166
     *
167
     * @param array  $columns
168
     * @param string $name
169
     *
170
     * @return ClassMetadataBuilder
171
     */
172 2
    public function addUniqueConstraint(array $columns, $name)
173
    {
174 2
        if ( ! isset($this->cm->table['uniqueConstraints'])) {
175 2
            $this->cm->table['uniqueConstraints'] = [];
176
        }
177
178 2
        $this->cm->table['uniqueConstraints'][$name] = ['columns' => $columns];
179
180 2
        return $this;
181
    }
182
183
    /**
184
     * Adds named query.
185
     *
186
     * @param string $name
187
     * @param string $dqlQuery
188
     *
189
     * @return ClassMetadataBuilder
190
     */
191
    public function addNamedQuery($name, $dqlQuery)
192
    {
193
        $this->cm->addNamedQuery(
194
            [
195
                'name' => $name,
196
                'query' => $dqlQuery,
197
            ]
198
        );
199
200
        return $this;
201
    }
202
203
    /**
204
     * Sets class as root of a joined table inheritance hierarchy.
205
     *
206
     * @return ClassMetadataBuilder
207
     */
208 1
    public function setJoinedTableInheritance()
209
    {
210 1
        $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_JOINED);
211
212 1
        return $this;
213
    }
214
215
    /**
216
     * Sets class as root of a single table inheritance hierarchy.
217
     *
218
     * @return ClassMetadataBuilder
219
     */
220 1
    public function setSingleTableInheritance()
221
    {
222 1
        $this->cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
223
224 1
        return $this;
225
    }
226
227
    /**
228
     * Sets the discriminator column details.
229
     *
230
     * @param string $name
231
     * @param string $type
232
     * @param int    $length
233
     *
234
     * @return ClassMetadataBuilder
235
     */
236 1
    public function setDiscriminatorColumn($name, $type = 'string', $length = 255)
237
    {
238 1
        $this->cm->setDiscriminatorColumn(
239
            [
240 1
                'name' => $name,
241 1
                'type' => $type,
242 1
                'length' => $length,
243
            ]
244
        );
245
246 1
        return $this;
247
    }
248
249
    /**
250
     * Adds a subclass to this inheritance hierarchy.
251
     *
252
     * @param string $name
253
     * @param string $class
254
     *
255
     * @return ClassMetadataBuilder
256
     */
257 1
    public function addDiscriminatorMapClass($name, $class)
258
    {
259 1
        $this->cm->addDiscriminatorMapClass($name, $class);
260
261 1
        return $this;
262
    }
263
264
    /**
265
     * Sets deferred explicit change tracking policy.
266
     *
267
     * @return ClassMetadataBuilder
268
     */
269 1
    public function setChangeTrackingPolicyDeferredExplicit()
270
    {
271 1
        $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT);
272
273 1
        return $this;
274
    }
275
276
    /**
277
     * Sets notify change tracking policy.
278
     *
279
     * @return ClassMetadataBuilder
280
     */
281 1
    public function setChangeTrackingPolicyNotify()
282
    {
283 1
        $this->cm->setChangeTrackingPolicy(ClassMetadata::CHANGETRACKING_NOTIFY);
284
285 1
        return $this;
286
    }
287
288
    /**
289
     * Adds lifecycle event.
290
     *
291
     * @param string $methodName
292
     * @param string $event
293
     *
294
     * @return ClassMetadataBuilder
295
     */
296 1
    public function addLifecycleEvent($methodName, $event)
297
    {
298 1
        $this->cm->addLifecycleCallback($methodName, $event);
299
300 1
        return $this;
301
    }
302
303
    /**
304
     * Adds Field.
305
     *
306
     * @param string $name
307
     * @param string $type
308
     * @param array  $mapping
309
     *
310
     * @return ClassMetadataBuilder
311
     */
312 1
    public function addField($name, $type, array $mapping = [])
313
    {
314 1
        $mapping['fieldName'] = $name;
315 1
        $mapping['type'] = $type;
316
317 1
        $this->cm->mapField($mapping);
318
319 1
        return $this;
320
    }
321
322
    /**
323
     * Creates a field builder.
324
     *
325
     * @param string $name
326
     * @param string $type
327
     *
328
     * @return FieldBuilder
329
     */
330 5
    public function createField($name, $type)
331
    {
332 5
        return new FieldBuilder(
333
            $this,
334
            [
335 5
                'fieldName' => $name,
336 5
                'type'      => $type
337
            ]
338
        );
339
    }
340
341
    /**
342
     * Creates an embedded builder.
343
     *
344
     * @param string $fieldName
345
     * @param string $class
346
     *
347
     * @return EmbeddedBuilder
348
     */
349 2
    public function createEmbedded($fieldName, $class)
350
    {
351 2
        return new EmbeddedBuilder(
352
            $this,
353
            [
354 2
                'fieldName'    => $fieldName,
355 2
                'class'        => $class,
356
                'columnPrefix' => null
357
            ]
358
        );
359
    }
360
361
    /**
362
     * Adds a simple many to one association, optionally with the inversed by field.
363
     *
364
     * @param string      $name
365
     * @param string      $targetEntity
366
     * @param string|null $inversedBy
367
     *
368
     * @return ClassMetadataBuilder
369
     */
370
    public function addManyToOne($name, $targetEntity, $inversedBy = null)
371
    {
372
        $builder = $this->createManyToOne($name, $targetEntity);
373
374
        if ($inversedBy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inversedBy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
375
            $builder->inversedBy($inversedBy);
376
        }
377
378
        return $builder->build();
379
    }
380
381
    /**
382
     * Creates a ManyToOne Association Builder.
383
     *
384
     * Note: This method does not add the association, you have to call build() on the AssociationBuilder.
385
     *
386
     * @param string $name
387
     * @param string $targetEntity
388
     *
389
     * @return AssociationBuilder
390
     */
391 3
    public function createManyToOne($name, $targetEntity)
392
    {
393 3
        return new AssociationBuilder(
394
            $this,
395
            [
396 3
                'fieldName'    => $name,
397 3
                'targetEntity' => $targetEntity
398
            ],
399 3
            ClassMetadata::MANY_TO_ONE
400
        );
401
    }
402
403
    /**
404
     * Creates a OneToOne Association Builder.
405
     *
406
     * @param string $name
407
     * @param string $targetEntity
408
     *
409
     * @return AssociationBuilder
410
     */
411 4
    public function createOneToOne($name, $targetEntity)
412
    {
413 4
        return new AssociationBuilder(
414
            $this,
415
            [
416 4
                'fieldName'    => $name,
417 4
                'targetEntity' => $targetEntity
418
            ],
419 4
            ClassMetadata::ONE_TO_ONE
420
        );
421
    }
422
423
    /**
424
     * Adds simple inverse one-to-one association.
425
     *
426
     * @param string $name
427
     * @param string $targetEntity
428
     * @param string $mappedBy
429
     *
430
     * @return ClassMetadataBuilder
431
     */
432
    public function addInverseOneToOne($name, $targetEntity, $mappedBy)
433
    {
434
        $builder = $this->createOneToOne($name, $targetEntity);
435
        $builder->mappedBy($mappedBy);
436
437
        return $builder->build();
438
    }
439
440
    /**
441
     * Adds simple owning one-to-one association.
442
     *
443
     * @param string      $name
444
     * @param string      $targetEntity
445
     * @param string|null $inversedBy
446
     *
447
     * @return ClassMetadataBuilder
448
     */
449
    public function addOwningOneToOne($name, $targetEntity, $inversedBy = null)
450
    {
451
        $builder = $this->createOneToOne($name, $targetEntity);
452
453
        if ($inversedBy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inversedBy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
454
            $builder->inversedBy($inversedBy);
455
        }
456
457
        return $builder->build();
458
    }
459
460
    /**
461
     * Creates a ManyToMany Association Builder.
462
     *
463
     * @param string $name
464
     * @param string $targetEntity
465
     *
466
     * @return ManyToManyAssociationBuilder
467
     */
468 3
    public function createManyToMany($name, $targetEntity)
469
    {
470 3
        return new ManyToManyAssociationBuilder(
471
            $this,
472
            [
473 3
                'fieldName'    => $name,
474 3
                'targetEntity' => $targetEntity
475
            ],
476 3
            ClassMetadata::MANY_TO_MANY
477
        );
478
    }
479
480
    /**
481
     * Adds a simple owning many to many association.
482
     *
483
     * @param string      $name
484
     * @param string      $targetEntity
485
     * @param string|null $inversedBy
486
     *
487
     * @return ClassMetadataBuilder
488
     */
489
    public function addOwningManyToMany($name, $targetEntity, $inversedBy = null)
490
    {
491
        $builder = $this->createManyToMany($name, $targetEntity);
492
493
        if ($inversedBy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $inversedBy of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
494
            $builder->inversedBy($inversedBy);
495
        }
496
497
        return $builder->build();
498
    }
499
500
    /**
501
     * Adds a simple inverse many to many association.
502
     *
503
     * @param string $name
504
     * @param string $targetEntity
505
     * @param string $mappedBy
506
     *
507
     * @return ClassMetadataBuilder
508
     */
509
    public function addInverseManyToMany($name, $targetEntity, $mappedBy)
510
    {
511
        $builder = $this->createManyToMany($name, $targetEntity);
512
        $builder->mappedBy($mappedBy);
513
514
        return $builder->build();
515
    }
516
517
    /**
518
     * Creates a one to many association builder.
519
     *
520
     * @param string $name
521
     * @param string $targetEntity
522
     *
523
     * @return OneToManyAssociationBuilder
524
     */
525 3
    public function createOneToMany($name, $targetEntity)
526
    {
527 3
        return new OneToManyAssociationBuilder(
528
            $this,
529
            [
530 3
                'fieldName'    => $name,
531 3
                'targetEntity' => $targetEntity
532
            ],
533 3
            ClassMetadata::ONE_TO_MANY
534
        );
535
    }
536
537
    /**
538
     * Adds simple OneToMany association.
539
     *
540
     * @param string $name
541
     * @param string $targetEntity
542
     * @param string $mappedBy
543
     *
544
     * @return ClassMetadataBuilder
545
     */
546
    public function addOneToMany($name, $targetEntity, $mappedBy)
547
    {
548
        $builder = $this->createOneToMany($name, $targetEntity);
549
        $builder->mappedBy($mappedBy);
550
551
        return $builder->build();
552
    }
553
}
554