Completed
Pull Request — master (#1427)
by Andreas
06:15
created

ClassMetadataInfo::mapManyEmbedded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1.008
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\ODM\MongoDB\Mapping;
21
22
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
23
use Doctrine\ODM\MongoDB\LockException;
24
use Doctrine\ODM\MongoDB\Proxy\Proxy;
25
use Doctrine\ODM\MongoDB\Types\Type;
26
use InvalidArgumentException;
27
28
/**
29
 * A <tt>ClassMetadata</tt> instance holds all the object-document mapping metadata
30
 * of a document and it's references.
31
 *
32
 * Once populated, ClassMetadata instances are usually cached in a serialized form.
33
 *
34
 * <b>IMPORTANT NOTE:</b>
35
 *
36
 * The fields of this class are only public for 2 reasons:
37
 * 1) To allow fast READ access.
38
 * 2) To drastically reduce the size of a serialized instance (private/protected members
39
 *    get the whole class name, namespace inclusive, prepended to every property in
40
 *    the serialized representation).
41
 *
42
 * @since       1.0
43
 */
44
class ClassMetadataInfo implements \Doctrine\Common\Persistence\Mapping\ClassMetadata
45
{
46
    /* The Id generator types. */
47
    /**
48
     * AUTO means Doctrine will automatically create a new \MongoId instance for us.
49
     */
50
    const GENERATOR_TYPE_AUTO = 1;
51
52
    /**
53
     * INCREMENT means a separate collection is used for maintaining and incrementing id generation.
54
     * Offers full portability.
55
     */
56
    const GENERATOR_TYPE_INCREMENT = 2;
57
58
    /**
59
     * UUID means Doctrine will generate a uuid for us.
60
     */
61
    const GENERATOR_TYPE_UUID = 3;
62
63
    /**
64
     * ALNUM means Doctrine will generate Alpha-numeric string identifiers, using the INCREMENT
65
     * generator to ensure identifier uniqueness
66
     */
67
    const GENERATOR_TYPE_ALNUM = 4;
68
69
    /**
70
     * CUSTOM means Doctrine expect a class parameter. It will then try to initiate that class
71
     * and pass other options to the generator. It will throw an Exception if the class
72
     * does not exist or if an option was passed for that there is not setter in the new
73
     * generator class.
74
     *
75
     * The class  will have to be a subtype of AbstractIdGenerator.
76
     */
77
    const GENERATOR_TYPE_CUSTOM = 5;
78
79
    /**
80
     * NONE means Doctrine will not generate any id for us and you are responsible for manually
81
     * assigning an id.
82
     */
83
    const GENERATOR_TYPE_NONE = 6;
84
85
    /**
86
     * Default discriminator field name.
87
     *
88
     * This is used for associations value for associations where a that do not define a "targetDocument" or
89
     * "discriminatorField" option in their mapping.
90
     */
91
    const DEFAULT_DISCRIMINATOR_FIELD = '_doctrine_class_name';
92
93
    const REFERENCE_ONE = 1;
94
    const REFERENCE_MANY = 2;
95
    const EMBED_ONE = 3;
96
    const EMBED_MANY = 4;
97
    const MANY = 'many';
98
    const ONE = 'one';
99
100
    /**
101
     * The types of storeAs references
102
     */
103
    const REFERENCE_STORE_AS_ID = 'id';
104
    const REFERENCE_STORE_AS_DB_REF = 'dbRef';
105
    const REFERENCE_STORE_AS_DB_REF_WITH_DB = 'dbRefWithDb';
106
107
    /* The inheritance mapping types */
108
    /**
109
     * NONE means the class does not participate in an inheritance hierarchy
110
     * and therefore does not need an inheritance mapping type.
111
     */
112
    const INHERITANCE_TYPE_NONE = 1;
113
114
    /**
115
     * SINGLE_COLLECTION means the class will be persisted according to the rules of
116
     * <tt>Single Collection Inheritance</tt>.
117
     */
118
    const INHERITANCE_TYPE_SINGLE_COLLECTION = 2;
119
120
    /**
121
     * COLLECTION_PER_CLASS means the class will be persisted according to the rules
122
     * of <tt>Concrete Collection Inheritance</tt>.
123
     */
124
    const INHERITANCE_TYPE_COLLECTION_PER_CLASS = 3;
125
126
    /**
127
     * DEFERRED_IMPLICIT means that changes of entities are calculated at commit-time
128
     * by doing a property-by-property comparison with the original data. This will
129
     * be done for all entities that are in MANAGED state at commit-time.
130
     *
131
     * This is the default change tracking policy.
132
     */
133
    const CHANGETRACKING_DEFERRED_IMPLICIT = 1;
134
135
    /**
136
     * DEFERRED_EXPLICIT means that changes of entities are calculated at commit-time
137
     * by doing a property-by-property comparison with the original data. This will
138
     * be done only for entities that were explicitly saved (through persist() or a cascade).
139
     */
140
    const CHANGETRACKING_DEFERRED_EXPLICIT = 2;
141
142
    /**
143
     * NOTIFY means that Doctrine relies on the entities sending out notifications
144
     * when their properties change. Such entity classes must implement
145
     * the <tt>NotifyPropertyChanged</tt> interface.
146
     */
147
    const CHANGETRACKING_NOTIFY = 3;
148
149
    /**
150
     * SET means that fields will be written to the database using a $set operator
151
     */
152
    const STORAGE_STRATEGY_SET = 'set';
153
154
    /**
155
     * INCREMENT means that fields will be written to the database by calculating
156
     * the difference and using the $inc operator
157
     */
158
    const STORAGE_STRATEGY_INCREMENT = 'increment';
159
160
    const STORAGE_STRATEGY_PUSH_ALL = 'pushAll';
161
    const STORAGE_STRATEGY_ADD_TO_SET = 'addToSet';
162
    const STORAGE_STRATEGY_ATOMIC_SET = 'atomicSet';
163
    const STORAGE_STRATEGY_ATOMIC_SET_ARRAY = 'atomicSetArray';
164
    const STORAGE_STRATEGY_SET_ARRAY = 'setArray';
165
166
    /**
167
     * READ-ONLY: The name of the mongo database the document is mapped to.
168
     */
169
    public $db;
170
171
    /**
172
     * READ-ONLY: The name of the mongo collection the document is mapped to.
173
     */
174
    public $collection;
175
176
    /**
177
     * READ-ONLY: If the collection should be a fixed size.
178
     */
179
    public $collectionCapped;
180
181
    /**
182
     * READ-ONLY: If the collection is fixed size, its size in bytes.
183
     */
184
    public $collectionSize;
185
186
    /**
187
     * READ-ONLY: If the collection is fixed size, the maximum number of elements to store in the collection.
188
     */
189
    public $collectionMax;
190
191
    /**
192
     * READ-ONLY: Describes the level of acknowledgement requested from MongoDB for write operations.
193
     */
194
    public $writeConcern;
195
196
    /**
197
     * READ-ONLY: The field name of the document identifier.
198
     */
199
    public $identifier;
200
201
    /**
202
     * READ-ONLY: The field that stores a file reference and indicates the
203
     * document is a file and should be stored on the MongoGridFS.
204
     */
205
    public $file;
206
207
    /**
208
     * READ-ONLY: The field that stores the calculated distance when performing geo spatial
209
     * queries.
210
     */
211
    public $distance;
212
213
    /**
214
     * READ-ONLY: Whether or not reads for this class are okay to read from a slave.
215
     */
216
    public $slaveOkay;
217
218
    /**
219
     * READ-ONLY: The array of indexes for the document collection.
220
     */
221
    public $indexes = array();
222
223
    /**
224
     * READ-ONLY: Keys and options describing shard key. Only for sharded collections.
225
     */
226
    public $shardKey;
227
228
    /**
229
     * READ-ONLY: Whether or not queries on this document should require indexes.
230
     */
231
    public $requireIndexes = false;
232
233
    /**
234
     * READ-ONLY: The name of the document class.
235
     */
236
    public $name;
237
238
    /**
239
     * READ-ONLY: The namespace the document class is contained in.
240
     *
241
     * @var string
242
     * @todo Not really needed. Usage could be localized.
243
     */
244
    public $namespace;
245
246
    /**
247
     * READ-ONLY: The name of the document class that is at the root of the mapped document inheritance
248
     * hierarchy. If the document is not part of a mapped inheritance hierarchy this is the same
249
     * as {@link $documentName}.
250
     *
251
     * @var string
252
     */
253
    public $rootDocumentName;
254
255
    /**
256
     * The name of the custom repository class used for the document class.
257
     * (Optional).
258
     *
259
     * @var string
260
     */
261
    public $customRepositoryClassName;
262
263
    /**
264
     * READ-ONLY: The names of the parent classes (ancestors).
265
     *
266
     * @var array
267
     */
268
    public $parentClasses = array();
269
270
    /**
271
     * READ-ONLY: The names of all subclasses (descendants).
272
     *
273
     * @var array
274
     */
275
    public $subClasses = array();
276
277
    /**
278
     * The ReflectionProperty instances of the mapped class.
279
     *
280
     * @var \ReflectionProperty[]
281
     */
282
    public $reflFields = array();
283
284
    /**
285
     * READ-ONLY: The inheritance mapping type used by the class.
286
     *
287
     * @var integer
288
     */
289
    public $inheritanceType = self::INHERITANCE_TYPE_NONE;
290
291
    /**
292
     * READ-ONLY: The Id generator type used by the class.
293
     *
294
     * @var string
295
     */
296
    public $generatorType = self::GENERATOR_TYPE_AUTO;
297
298
    /**
299
     * READ-ONLY: The Id generator options.
300
     *
301
     * @var array
302
     */
303
    public $generatorOptions = array();
304
305
    /**
306
     * READ-ONLY: The ID generator used for generating IDs for this class.
307
     *
308
     * @var \Doctrine\ODM\MongoDB\Id\AbstractIdGenerator
309
     */
310
    public $idGenerator;
311
312
    /**
313
     * READ-ONLY: The field mappings of the class.
314
     * Keys are field names and values are mapping definitions.
315
     *
316
     * The mapping definition array has the following values:
317
     *
318
     * - <b>fieldName</b> (string)
319
     * The name of the field in the Document.
320
     *
321
     * - <b>id</b> (boolean, optional)
322
     * Marks the field as the primary key of the document. Multiple fields of an
323
     * document can have the id attribute, forming a composite key.
324
     *
325
     * @var array
326
     */
327
    public $fieldMappings = array();
328
329
    /**
330
     * READ-ONLY: The association mappings of the class.
331
     * Keys are field names and values are mapping definitions.
332
     *
333
     * @var array
334
     */
335
    public $associationMappings = array();
336
337
    /**
338
     * READ-ONLY: Array of fields to also load with a given method.
339
     *
340
     * @var array
341
     */
342
    public $alsoLoadMethods = array();
343
344
    /**
345
     * READ-ONLY: The registered lifecycle callbacks for documents of this class.
346
     *
347
     * @var array
348
     */
349
    public $lifecycleCallbacks = array();
350
351
    /**
352
     * READ-ONLY: The discriminator value of this class.
353
     *
354
     * <b>This does only apply to the JOINED and SINGLE_COLLECTION inheritance mapping strategies
355
     * where a discriminator field is used.</b>
356
     *
357
     * @var mixed
358
     * @see discriminatorField
359
     */
360
    public $discriminatorValue;
361
362
    /**
363
     * READ-ONLY: The discriminator map of all mapped classes in the hierarchy.
364
     *
365
     * <b>This does only apply to the SINGLE_COLLECTION inheritance mapping strategy
366
     * where a discriminator field is used.</b>
367
     *
368
     * @var mixed
369
     * @see discriminatorField
370
     */
371
    public $discriminatorMap = array();
372
373
    /**
374
     * READ-ONLY: The definition of the discriminator field used in SINGLE_COLLECTION
375
     * inheritance mapping.
376
     *
377
     * @var string
378
     */
379
    public $discriminatorField;
380
381
    /**
382
     * READ-ONLY: The default value for discriminatorField in case it's not set in the document
383
     *
384
     * @var string
385
     * @see discriminatorField
386
     */
387
    public $defaultDiscriminatorValue;
388
389
    /**
390
     * READ-ONLY: Whether this class describes the mapping of a mapped superclass.
391
     *
392
     * @var boolean
393
     */
394
    public $isMappedSuperclass = false;
395
396
    /**
397
     * READ-ONLY: Whether this class describes the mapping of a embedded document.
398
     *
399
     * @var boolean
400
     */
401
    public $isEmbeddedDocument = false;
402
403
    /**
404
     * READ-ONLY: The policy used for change-tracking on entities of this class.
405
     *
406
     * @var integer
407
     */
408
    public $changeTrackingPolicy = self::CHANGETRACKING_DEFERRED_IMPLICIT;
409
410
    /**
411
     * READ-ONLY: A flag for whether or not instances of this class are to be versioned
412
     * with optimistic locking.
413
     *
414
     * @var boolean $isVersioned
415
     */
416
    public $isVersioned;
417
418
    /**
419
     * READ-ONLY: The name of the field which is used for versioning in optimistic locking (if any).
420
     *
421
     * @var mixed $versionField
422
     */
423
    public $versionField;
424
425
    /**
426
     * READ-ONLY: A flag for whether or not instances of this class are to allow pessimistic
427
     * locking.
428
     *
429
     * @var boolean $isLockable
430
     */
431
    public $isLockable;
432
433
    /**
434
     * READ-ONLY: The name of the field which is used for locking a document.
435
     *
436
     * @var mixed $lockField
437
     */
438
    public $lockField;
439
440
    /**
441
     * The ReflectionClass instance of the mapped class.
442
     *
443
     * @var \ReflectionClass
444
     */
445
    public $reflClass;
446
447
    /**
448
     * Initializes a new ClassMetadata instance that will hold the object-document mapping
449
     * metadata of the class with the given name.
450
     *
451
     * @param string $documentName The name of the document class the new instance is used for.
452
     */
453 934
    public function __construct($documentName)
454
    {
455 934
        $this->name = $documentName;
456 934
        $this->rootDocumentName = $documentName;
457 934
    }
458
459
    /**
460
     * {@inheritDoc}
461
     */
462 867
    public function getReflectionClass()
463
    {
464 867
        if ( ! $this->reflClass) {
465
            $this->reflClass = new \ReflectionClass($this->name);
466
        }
467
468 867
        return $this->reflClass;
469
    }
470
471
    /**
472
     * {@inheritDoc}
473
     */
474
    public function isIdentifier($fieldName)
475
    {
476
        return $this->identifier === $fieldName;
477
    }
478
479
    /**
480
     * INTERNAL:
481
     * Sets the mapped identifier field of this class.
482
     *
483
     * @param string $identifier
484
     */
485 212
    public function setIdentifier($identifier)
486
    {
487 212
        $this->identifier = $identifier;
488 212
    }
489
490
    /**
491
     * {@inheritDoc}
492
     *
493
     * Since MongoDB only allows exactly one identifier field
494
     * this will always return an array with only one value
495
     */
496
    public function getIdentifier()
497
    {
498
        return array($this->identifier);
499
    }
500
501
    /**
502
     * {@inheritDoc}
503
     *
504
     * Since MongoDB only allows exactly one identifier field
505
     * this will always return an array with only one value
506
     */
507
    public function getIdentifierFieldNames()
508
    {
509
        return array($this->identifier);
510
    }
511
512
    /**
513
     * {@inheritDoc}
514
     */
515
    public function hasField($fieldName)
516
    {
517
        return isset($this->fieldMappings[$fieldName]);
518
    }
519
520
    /**
521
     * Sets the inheritance type used by the class and it's subclasses.
522
     *
523
     * @param integer $type
524
     */
525 244
    public function setInheritanceType($type)
526
    {
527 244
        $this->inheritanceType = $type;
528 244
    }
529
530
    /**
531
     * Checks whether a mapped field is inherited from an entity superclass.
532
     *
533
     * @param  string $fieldName
534
     *
535
     * @return boolean TRUE if the field is inherited, FALSE otherwise.
536
     */
537 863
    public function isInheritedField($fieldName)
538
    {
539 863
        return isset($this->fieldMappings[$fieldName]['inherited']);
540
    }
541
542
    /**
543
     * Registers a custom repository class for the document class.
544
     *
545
     * @param string $repositoryClassName The class name of the custom repository.
546
     */
547 217 View Code Duplication
    public function setCustomRepositoryClass($repositoryClassName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
548
    {
549 217
        if ($this->isEmbeddedDocument) {
550
            return;
551
        }
552
553 217
        if ($repositoryClassName && strpos($repositoryClassName, '\\') === false && strlen($this->namespace)) {
554 2
            $repositoryClassName = $this->namespace . '\\' . $repositoryClassName;
555
        }
556
557 217
        $this->customRepositoryClassName = $repositoryClassName;
558 217
    }
559
560
    /**
561
     * Dispatches the lifecycle event of the given document by invoking all
562
     * registered callbacks.
563
     *
564
     * @param string $event     Lifecycle event
565
     * @param object $document  Document on which the event occurred
566
     * @param array  $arguments Arguments to pass to all callbacks
567
     * @throws \InvalidArgumentException if document class is not this class or
568
     *                                   a Proxy of this class
569
     */
570
    public function invokeLifecycleCallbacks($event, $document, array $arguments = null)
571
    {
572
        if ( ! $document instanceof $this->name) {
573
            throw new \InvalidArgumentException(sprintf('Expected document class "%s"; found: "%s"', $this->name, get_class($document)));
574
        }
575
576
        if (empty($this->lifecycleCallbacks[$event])) {
577
            return;
578
        }
579
580
        foreach ($this->lifecycleCallbacks[$event] as $callback) {
581
            if ($arguments !== null) {
582
                call_user_func_array(array($document, $callback), $arguments);
583
            } else {
584
                $document->$callback();
585
            }
586
        }
587
    }
588
589
    /**
590
     * Checks whether the class has callbacks registered for a lifecycle event.
591
     *
592
     * @param string $event Lifecycle event
593
     *
594
     * @return boolean
595
     */
596
    public function hasLifecycleCallbacks($event)
597
    {
598
        return ! empty($this->lifecycleCallbacks[$event]);
599
    }
600
601
    /**
602
     * Gets the registered lifecycle callbacks for an event.
603
     *
604
     * @param string $event
605
     * @return array
606
     */
607
    public function getLifecycleCallbacks($event)
608
    {
609
        return isset($this->lifecycleCallbacks[$event]) ? $this->lifecycleCallbacks[$event] : array();
610
    }
611
612
    /**
613
     * Adds a lifecycle callback for documents of this class.
614
     *
615
     * If the callback is already registered, this is a NOOP.
616
     *
617
     * @param string $callback
618
     * @param string $event
619
     */
620 180
    public function addLifecycleCallback($callback, $event)
621
    {
622 180
        if (isset($this->lifecycleCallbacks[$event]) && in_array($callback, $this->lifecycleCallbacks[$event])) {
623
            return;
624
        }
625
626 180
        $this->lifecycleCallbacks[$event][] = $callback;
627 180
    }
628
629
    /**
630
     * Sets the lifecycle callbacks for documents of this class.
631
     *
632
     * Any previously registered callbacks are overwritten.
633
     *
634
     * @param array $callbacks
635
     */
636 211
    public function setLifecycleCallbacks(array $callbacks)
637
    {
638 211
        $this->lifecycleCallbacks = $callbacks;
639 211
    }
640
641
    /**
642
     * Registers a method for loading document data before field hydration.
643
     *
644
     * Note: A method may be registered multiple times for different fields.
645
     * it will be invoked only once for the first field found.
646
     *
647
     * @param string       $method Method name
648
     * @param array|string $fields Database field name(s)
649
     */
650
    public function registerAlsoLoadMethod($method, $fields)
651
    {
652
        $this->alsoLoadMethods[$method] = is_array($fields) ? $fields : array($fields);
653
    }
654
655
    /**
656
     * Sets the AlsoLoad methods for documents of this class.
657
     *
658
     * Any previously registered methods are overwritten.
659
     *
660
     * @param array $methods
661
     */
662 211
    public function setAlsoLoadMethods(array $methods)
663
    {
664 211
        $this->alsoLoadMethods = $methods;
665 211
    }
666
667
    /**
668
     * Sets the discriminator field.
669
     *
670
     * The field name is the the unmapped database field. Discriminator values
671
     * are only used to discern the hydration class and are not mapped to class
672
     * properties.
673
     *
674
     * @param string $discriminatorField
675
     *
676
     * @throws MappingException If the discriminator field conflicts with the
677
     *                          "name" attribute of a mapped field.
678
     */
679 244
    public function setDiscriminatorField($discriminatorField)
680
    {
681 244
        if ($discriminatorField === null) {
682 211
            $this->discriminatorField = null;
683
684 211
            return;
685
        }
686
687
        // Handle array argument with name/fieldName keys for BC
688 33
        if (is_array($discriminatorField)) {
689
            if (isset($discriminatorField['name'])) {
690
                $discriminatorField = $discriminatorField['name'];
691
            } elseif (isset($discriminatorField['fieldName'])) {
692
                $discriminatorField = $discriminatorField['fieldName'];
693
            }
694
        }
695
696 33
        foreach ($this->fieldMappings as $fieldMapping) {
697
            if ($discriminatorField == $fieldMapping['name']) {
698
                throw MappingException::discriminatorFieldConflict($this->name, $discriminatorField);
699
            }
700
        }
701
702 33
        $this->discriminatorField = $discriminatorField;
703 33
    }
704
705
    /**
706
     * Sets the discriminator values used by this class.
707
     * Used for JOINED and SINGLE_TABLE inheritance mapping strategies.
708
     *
709
     * @param array $map
710
     *
711
     * @throws MappingException
712
     */
713 243
    public function setDiscriminatorMap(array $map)
714
    {
715 243
        foreach ($map as $value => $className) {
716 32
            if (strpos($className, '\\') === false && strlen($this->namespace)) {
717 12
                $className = $this->namespace . '\\' . $className;
718
            }
719 32
            $this->discriminatorMap[$value] = $className;
720 32
            if ($this->name == $className) {
721 26
                $this->discriminatorValue = $value;
722
            } else {
723 27
                if ( ! class_exists($className)) {
724
                    throw MappingException::invalidClassInDiscriminatorMap($className, $this->name);
725
                }
726 27
                if (is_subclass_of($className, $this->name)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $this->name can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
727 32
                    $this->subClasses[] = $className;
728
                }
729
            }
730
        }
731 243
    }
732
733
    /**
734
     * Sets the default discriminator value to be used for this class
735
     * Used for JOINED and SINGLE_TABLE inheritance mapping strategies if the document has no discriminator value
736
     *
737
     * @param string $defaultDiscriminatorValue
738
     *
739
     * @throws MappingException
740
     */
741 217
    public function setDefaultDiscriminatorValue($defaultDiscriminatorValue)
742
    {
743 217
        if ($defaultDiscriminatorValue === null) {
744 211
            $this->defaultDiscriminatorValue = null;
745
746 211
            return;
747
        }
748
749 6
        if (!array_key_exists($defaultDiscriminatorValue, $this->discriminatorMap)) {
750
            throw MappingException::invalidDiscriminatorValue($defaultDiscriminatorValue, $this->name);
751
        }
752
753 6
        $this->defaultDiscriminatorValue = $defaultDiscriminatorValue;
754 6
    }
755
756
    /**
757
     * Sets the discriminator value for this class.
758
     * Used for JOINED/SINGLE_TABLE inheritance and multiple document types in a single
759
     * collection.
760
     *
761
     * @param string $value
762
     */
763
    public function setDiscriminatorValue($value)
764
    {
765
        $this->discriminatorMap[$value] = $this->name;
766
        $this->discriminatorValue = $value;
767
    }
768
769
    /**
770
     * Sets the slaveOkay option applied to collections for this class.
771
     *
772
     * @param boolean|null $slaveOkay
773
     */
774 1
    public function setSlaveOkay($slaveOkay)
775
    {
776 1
        $this->slaveOkay = $slaveOkay === null ? null : (boolean) $slaveOkay;
777 1
    }
778
779
    /**
780
     * Add a index for this Document.
781
     *
782
     * @param array $keys Array of keys for the index.
783
     * @param array $options Array of options for the index.
784
     */
785 24
    public function addIndex($keys, array $options = array())
786
    {
787 24
        $this->indexes[] = array(
788 View Code Duplication
            'keys' => array_map(function($value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
789 24
                if ($value == 1 || $value == -1) {
790
                    return (int) $value;
791
                }
792 24
                if (is_string($value)) {
793 24
                    $lower = strtolower($value);
794 24
                    if ($lower === 'asc') {
795 17
                        return 1;
796 9
                    } elseif ($lower === 'desc') {
797 2
                        return -1;
798
                    }
799
                }
800 7
                return $value;
801 24
            }, $keys),
802 24
            'options' => $options
803
        );
804 24
    }
805
806
    /**
807
     * Set whether or not queries on this document should require indexes.
808
     *
809
     * @param bool $requireIndexes
810
     */
811 852
    public function setRequireIndexes($requireIndexes)
812
    {
813 852
        $this->requireIndexes = $requireIndexes;
814 852
    }
815
816
    /**
817
     * Returns the array of indexes for this Document.
818
     *
819
     * @return array $indexes The array of indexes.
820
     */
821
    public function getIndexes()
822
    {
823
        return $this->indexes;
824
    }
825
826
    /**
827
     * Checks whether this document has indexes or not.
828
     *
829
     * @return boolean
830
     */
831
    public function hasIndexes()
832
    {
833
        return $this->indexes ? true : false;
834
    }
835
836
    /**
837
     * Set shard key for this Document.
838
     *
839
     * @param array $keys Array of document keys.
840
     * @param array $options Array of sharding options.
841
     *
842
     * @throws MappingException
843
     */
844 9
    public function setShardKey(array $keys, array $options = array())
845
    {
846 9
        if ($this->inheritanceType === self::INHERITANCE_TYPE_SINGLE_COLLECTION && !is_null($this->shardKey)) {
847 1
            throw MappingException::shardKeyInSingleCollInheritanceSubclass($this->getName());
848
        }
849
850 9
        if ($this->isEmbeddedDocument) {
851 1
            throw MappingException::embeddedDocumentCantHaveShardKey($this->getName());
852
        }
853
854 8
        foreach (array_keys($keys) as $field) {
855 8
            if (! isset($this->fieldMappings[$field])) {
856 8
                continue;
857
            }
858
859
            if (in_array($this->fieldMappings[$field]['type'], ['many', 'collection'])) {
860
                throw MappingException::noMultiKeyShardKeys($this->getName(), $field);
861
            }
862
863
            if ($this->fieldMappings[$field]['strategy'] !== static::STORAGE_STRATEGY_SET) {
864
                throw MappingException::onlySetStrategyAllowedInShardKey($this->getName(), $field);
865
            }
866
        }
867
868 8
        $this->shardKey = array(
869 View Code Duplication
            'keys' => array_map(function($value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
870 8
                if ($value == 1 || $value == -1) {
871
                    return (int) $value;
872
                }
873 8
                if (is_string($value)) {
874 8
                    $lower = strtolower($value);
875 8
                    if ($lower === 'asc') {
876 8
                        return 1;
877
                    } elseif ($lower === 'desc') {
878
                        return -1;
879
                    }
880
                }
881
                return $value;
882 8
            }, $keys),
883 8
            'options' => $options
884
        );
885 8
    }
886
887
    /**
888
     * @return array
889
     */
890 4
    public function getShardKey()
891
    {
892 4
        return $this->shardKey;
893
    }
894
895
    /**
896
     * Checks whether this document has shard key or not.
897
     *
898
     * @return bool
899
     */
900 214
    public function isSharded()
901
    {
902 214
        return $this->shardKey ? true : false;
903
    }
904
905
    /**
906
     * Sets the write concern used by this class.
907
     *
908
     * @param string $writeConcern
909
     */
910 215
    public function setWriteConcern($writeConcern)
911
    {
912 215
        $this->writeConcern = $writeConcern;
913 215
    }
914
915
    /**
916
     * @return string
917
     */
918
    public function getWriteConcern()
919
    {
920
        return $this->writeConcern;
921
    }
922
923
    /**
924
     * Whether there is a write concern configured for this class.
925
     *
926
     * @return bool
927
     */
928
    public function hasWriteConcern()
929
    {
930
        return $this->writeConcern !== null;
931
    }
932
933
    /**
934
     * Sets the change tracking policy used by this class.
935
     *
936
     * @param integer $policy
937
     */
938 214
    public function setChangeTrackingPolicy($policy)
939
    {
940 214
        $this->changeTrackingPolicy = $policy;
941 214
    }
942
943
    /**
944
     * Whether the change tracking policy of this class is "deferred explicit".
945
     *
946
     * @return boolean
947
     */
948
    public function isChangeTrackingDeferredExplicit()
949
    {
950
        return $this->changeTrackingPolicy == self::CHANGETRACKING_DEFERRED_EXPLICIT;
951
    }
952
953
    /**
954
     * Whether the change tracking policy of this class is "deferred implicit".
955
     *
956
     * @return boolean
957
     */
958
    public function isChangeTrackingDeferredImplicit()
959
    {
960
        return $this->changeTrackingPolicy == self::CHANGETRACKING_DEFERRED_IMPLICIT;
961
    }
962
963
    /**
964
     * Whether the change tracking policy of this class is "notify".
965
     *
966
     * @return boolean
967
     */
968
    public function isChangeTrackingNotify()
969
    {
970
        return $this->changeTrackingPolicy == self::CHANGETRACKING_NOTIFY;
971
    }
972
973
    /**
974
     * Gets the ReflectionProperties of the mapped class.
975
     *
976
     * @return array An array of ReflectionProperty instances.
977
     */
978 1
    public function getReflectionProperties()
979
    {
980 1
        return $this->reflFields;
981
    }
982
983
    /**
984
     * Gets a ReflectionProperty for a specific field of the mapped class.
985
     *
986
     * @param string $name
987
     *
988
     * @return \ReflectionProperty
989
     */
990
    public function getReflectionProperty($name)
991
    {
992
        return $this->reflFields[$name];
993
    }
994
995
    /**
996
     * {@inheritDoc}
997
     */
998 868
    public function getName()
999
    {
1000 868
        return $this->name;
1001
    }
1002
1003
    /**
1004
     * The namespace this Document class belongs to.
1005
     *
1006
     * @return string $namespace The namespace name.
1007
     */
1008
    public function getNamespace()
1009
    {
1010
        return $this->namespace;
1011
    }
1012
1013
    /**
1014
     * Returns the database this Document is mapped to.
1015
     *
1016
     * @return string $db The database name.
1017
     */
1018
    public function getDatabase()
1019
    {
1020
        return $this->db;
1021
    }
1022
1023
    /**
1024
     * Set the database this Document is mapped to.
1025
     *
1026
     * @param string $db The database name
1027
     */
1028 7
    public function setDatabase($db)
1029
    {
1030 7
        $this->db = $db;
1031 7
    }
1032
1033
    /**
1034
     * Get the collection this Document is mapped to.
1035
     *
1036
     * @return string $collection The collection name.
1037
     */
1038 1
    public function getCollection()
1039
    {
1040 1
        return $this->collection;
1041
    }
1042
1043
    /**
1044
     * Sets the collection this Document is mapped to.
1045
     *
1046
     * @param array|string $name
1047
     *
1048
     * @throws \InvalidArgumentException
1049
     */
1050 897
    public function setCollection($name)
1051
    {
1052 897
        if (is_array($name)) {
1053
            if ( ! isset($name['name'])) {
1054
                throw new \InvalidArgumentException('A name key is required when passing an array to setCollection()');
1055
            }
1056
            $this->collectionCapped = isset($name['capped']) ? $name['capped'] : false;
1057
            $this->collectionSize = isset($name['size']) ? $name['size'] : 0;
1058
            $this->collectionMax = isset($name['max']) ? $name['max'] : 0;
1059
            $this->collection = $name['name'];
1060
        } else {
1061 897
            $this->collection = $name;
1062
        }
1063 897
    }
1064
1065
    /**
1066
     * Get whether or not the documents collection is capped.
1067
     *
1068
     * @return boolean
1069
     */
1070
    public function getCollectionCapped()
1071
    {
1072
        return $this->collectionCapped;
1073
    }
1074
1075
    /**
1076
     * Set whether or not the documents collection is capped.
1077
     *
1078
     * @param boolean $bool
1079
     */
1080
    public function setCollectionCapped($bool)
1081
    {
1082
        $this->collectionCapped = $bool;
1083
    }
1084
1085
    /**
1086
     * Get the collection size
1087
     *
1088
     * @return integer
1089
     */
1090
    public function getCollectionSize()
1091
    {
1092
        return $this->collectionSize;
1093
    }
1094
1095
    /**
1096
     * Set the collection size.
1097
     *
1098
     * @param integer $size
1099
     */
1100
    public function setCollectionSize($size)
1101
    {
1102
        $this->collectionSize = $size;
1103
    }
1104
1105
    /**
1106
     * Get the collection max.
1107
     *
1108
     * @return integer
1109
     */
1110
    public function getCollectionMax()
1111
    {
1112
        return $this->collectionMax;
1113
    }
1114
1115
    /**
1116
     * Set the collection max.
1117
     *
1118
     * @param integer $max
1119
     */
1120
    public function setCollectionMax($max)
1121
    {
1122
        $this->collectionMax = $max;
1123
    }
1124
1125
    /**
1126
     * Returns TRUE if this Document is mapped to a collection FALSE otherwise.
1127
     *
1128
     * @return boolean
1129
     */
1130
    public function isMappedToCollection()
1131
    {
1132
        return $this->collection ? true : false;
1133
    }
1134
1135
    /**
1136
     * Returns TRUE if this Document is a file to be stored on the MongoGridFS FALSE otherwise.
1137
     *
1138
     * @return boolean
1139
     */
1140
    public function isFile()
1141
    {
1142
        return $this->file ? true : false;
1143
    }
1144
1145
    /**
1146
     * Returns the file field name.
1147
     *
1148
     * @return string $file The file field name.
1149
     */
1150 211
    public function getFile()
1151
    {
1152 211
        return $this->file;
1153
    }
1154
1155
    /**
1156
     * Set the field name that stores the grid file.
1157
     *
1158
     * @param string $file
1159
     */
1160 211
    public function setFile($file)
1161
    {
1162 211
        $this->file = $file;
1163 211
    }
1164
1165
    /**
1166
     * Returns the distance field name.
1167
     *
1168
     * @return string $distance The distance field name.
1169
     */
1170
    public function getDistance()
1171
    {
1172
        return $this->distance;
1173
    }
1174
1175
    /**
1176
     * Set the field name that stores the distance.
1177
     *
1178
     * @param string $distance
1179
     */
1180
    public function setDistance($distance)
1181
    {
1182
        $this->distance = $distance;
1183
    }
1184
1185
    /**
1186
     * Map a field.
1187
     *
1188
     * @param array $mapping The mapping information.
1189
     *
1190
     * @return array
1191
     *
1192
     * @throws MappingException
1193
     */
1194 912
    public function mapField(array $mapping)
1195
    {
1196 912
        if ( ! isset($mapping['fieldName']) && isset($mapping['name'])) {
1197 7
            $mapping['fieldName'] = $mapping['name'];
1198
        }
1199 912
        if ( ! isset($mapping['fieldName'])) {
1200
            throw MappingException::missingFieldName($this->name);
1201
        }
1202 912
        if ( ! isset($mapping['name'])) {
1203 903
            $mapping['name'] = $mapping['fieldName'];
1204
        }
1205 912
        if ($this->identifier === $mapping['name'] && empty($mapping['id'])) {
1206 1
            throw MappingException::mustNotChangeIdentifierFieldsType($this->name, $mapping['name']);
1207
        }
1208 911
        if (isset($this->fieldMappings[$mapping['fieldName']])) {
1209
            //throw MappingException::duplicateFieldMapping($this->name, $mapping['fieldName']);
1210
        }
1211 911
        if ($this->discriminatorField !== null && $this->discriminatorField == $mapping['name']) {
1212 1
            throw MappingException::discriminatorFieldConflict($this->name, $this->discriminatorField);
1213
        }
1214 910 View Code Duplication
        if (isset($mapping['targetDocument']) && strpos($mapping['targetDocument'], '\\') === false && strlen($this->namespace)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1215 4
            $mapping['targetDocument'] = $this->namespace . '\\' . $mapping['targetDocument'];
1216
        }
1217 910
        if (isset($mapping['collectionClass'])) {
1218 1 View Code Duplication
            if (strpos($mapping['collectionClass'], '\\') === false && strlen($this->namespace)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1219
                $mapping['collectionClass'] = $this->namespace . '\\' . $mapping['collectionClass'];
1220
            }
1221 1
            $mapping['collectionClass'] = ltrim($mapping['collectionClass'], '\\');
1222
        }
1223 910
        if ( ! empty($mapping['collectionClass'])) {
1224 1
            $rColl = new \ReflectionClass($mapping['collectionClass']);
1225 1
            if ( ! $rColl->implementsInterface('Doctrine\\Common\\Collections\\Collection')) {
1226 1
                throw MappingException::collectionClassDoesNotImplementCommonInterface($this->name, $mapping['fieldName'], $mapping['collectionClass']);
1227
            }
1228
        }
1229
1230 909
        if (isset($mapping['discriminatorMap'])) {
1231
            foreach ($mapping['discriminatorMap'] as $key => $class) {
1232 View Code Duplication
                if (strpos($class, '\\') === false && strlen($this->namespace)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1233
                    $mapping['discriminatorMap'][$key] = $this->namespace . '\\' . $class;
1234
                }
1235
            }
1236
        }
1237
1238 909
        if (isset($mapping['cascade']) && isset($mapping['embedded'])) {
1239 1
            throw MappingException::cascadeOnEmbeddedNotAllowed($this->name, $mapping['fieldName']);
1240
        }
1241
1242 908
        $cascades = isset($mapping['cascade']) ? array_map('strtolower', (array) $mapping['cascade']) : array();
1243
1244 908
        if (in_array('all', $cascades) || isset($mapping['embedded'])) {
1245 8
            $cascades = array('remove', 'persist', 'refresh', 'merge', 'detach');
1246
        }
1247
1248 908
        if (isset($mapping['embedded'])) {
1249 8
            unset($mapping['cascade']);
1250 900
        } elseif (isset($mapping['cascade'])) {
1251
            $mapping['cascade'] = $cascades;
1252
        }
1253
1254 908
        $mapping['isCascadeRemove'] = in_array('remove', $cascades);
1255 908
        $mapping['isCascadePersist'] = in_array('persist', $cascades);
1256 908
        $mapping['isCascadeRefresh'] = in_array('refresh', $cascades);
1257 908
        $mapping['isCascadeMerge'] = in_array('merge', $cascades);
1258 908
        $mapping['isCascadeDetach'] = in_array('detach', $cascades);
1259
1260 908
        if (isset($mapping['type']) && $mapping['type'] === 'file') {
1261
            $mapping['file'] = true;
1262
        }
1263 908
        if (isset($mapping['type']) && $mapping['type'] === 'increment') {
1264 1
            $mapping['strategy'] = self::STORAGE_STRATEGY_INCREMENT;
1265
        }
1266 908 View Code Duplication
        if (isset($mapping['file']) && $mapping['file'] === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1267
            $this->file = $mapping['fieldName'];
1268
            $mapping['name'] = 'file';
1269
        }
1270 908 View Code Duplication
        if (isset($mapping['distance']) && $mapping['distance'] === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1271
            $this->distance = $mapping['fieldName'];
1272
        }
1273 908
        if (isset($mapping['id']) && $mapping['id'] === true) {
1274 845
            $mapping['name'] = '_id';
1275 845
            $this->identifier = $mapping['fieldName'];
1276 845 View Code Duplication
            if (isset($mapping['strategy'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1277 835
                $this->generatorType = constant(ClassMetadata::class . '::GENERATOR_TYPE_' . strtoupper($mapping['strategy']));
1278
            }
1279 845
            $this->generatorOptions = isset($mapping['options']) ? $mapping['options'] : array();
1280 845
            switch ($this->generatorType) {
1281 845
                case self::GENERATOR_TYPE_AUTO:
1282 771
                    $mapping['type'] = 'id';
1283 771
                    break;
1284
                default:
1285 74
                    if ( ! empty($this->generatorOptions['type'])) {
1286 36
                        $mapping['type'] = $this->generatorOptions['type'];
1287 38
                    } elseif (empty($mapping['type'])) {
1288 26
                        $mapping['type'] = $this->generatorType === self::GENERATOR_TYPE_INCREMENT ? 'int_id' : 'custom_id';
1289
                    }
1290
            }
1291 845
            unset($this->generatorOptions['type']);
1292
        }
1293
1294 908
        if ( ! isset($mapping['nullable'])) {
1295 44
            $mapping['nullable'] = false;
1296
        }
1297
1298
        // Synchronize the "simple" and "storeAs" mapping information for backwards compatibility
1299 908
        if (isset($mapping['simple']) && ($mapping['simple'] === true || $mapping['simple'] === 'true')) {
1300 2
            $mapping['storeAs'] = ClassMetadataInfo::REFERENCE_STORE_AS_ID;
1301
        }
1302
        // Provide the correct value for the "simple" field for backwards compatibility
1303 908
        $mapping['simple'] = $mapping['storeAs'] == ClassMetadataInfo::REFERENCE_STORE_AS_ID;
1304
1305 4
        if (isset($mapping['reference'])
1306 4
            && isset($mapping['storeAs'])
1307 4
            && $mapping['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_ID
1308 4
            && ! isset($mapping['targetDocument'])
1309
        ) {
1310 3
            throw MappingException::simpleReferenceRequiresTargetDocument($this->name, $mapping['fieldName']);
1311
        }
1312
1313 1
        if (isset($mapping['reference']) && empty($mapping['targetDocument']) && empty($mapping['discriminatorMap']) &&
1314 1
                (isset($mapping['mappedBy']) || isset($mapping['inversedBy']))) {
1315
            throw MappingException::owningAndInverseReferencesRequireTargetDocument($this->name, $mapping['fieldName']);
1316
        }
1317
1318 1
        if ($this->isEmbeddedDocument && $mapping['type'] === 'many' && CollectionHelper::isAtomic($mapping['strategy'])) {
1319
            throw MappingException::atomicCollectionStrategyNotAllowed($mapping['strategy'], $this->name, $mapping['fieldName']);
1320
        }
1321
1322 1 View Code Duplication
        if (isset($mapping['reference']) && $mapping['type'] === 'one') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1323 1
            $mapping['association'] = self::REFERENCE_ONE;
1324
        }
1325 1 View Code Duplication
        if (isset($mapping['reference']) && $mapping['type'] === 'many') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1326
            $mapping['association'] = self::REFERENCE_MANY;
1327
        }
1328 1 View Code Duplication
        if (isset($mapping['embedded']) && $mapping['type'] === 'one') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1329
            $mapping['association'] = self::EMBED_ONE;
1330
        }
1331 1 View Code Duplication
        if (isset($mapping['embedded']) && $mapping['type'] === 'many') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1332
            $mapping['association'] = self::EMBED_MANY;
1333
        }
1334
1335 1
        if (isset($mapping['association']) && ! isset($mapping['targetDocument']) && ! isset($mapping['discriminatorField'])) {
1336
            $mapping['discriminatorField'] = self::DEFAULT_DISCRIMINATOR_FIELD;
1337
        }
1338
1339
        /*
1340
        if (isset($mapping['type']) && ($mapping['type'] === 'one' || $mapping['type'] === 'many')) {
1341
            $mapping['type'] = $mapping['type'] === 'one' ? self::ONE : self::MANY;
1342
        }
1343
        */
1344 1
        if (isset($mapping['version'])) {
1345
            $mapping['notSaved'] = true;
1346
            $this->setVersionMapping($mapping);
1347
        }
1348 1
        if (isset($mapping['lock'])) {
1349
            $mapping['notSaved'] = true;
1350
            $this->setLockMapping($mapping);
1351
        }
1352 1
        $mapping['isOwningSide'] = true;
1353 1
        $mapping['isInverseSide'] = false;
1354 1
        if (isset($mapping['reference'])) {
1355 1 View Code Duplication
            if (isset($mapping['inversedBy']) && $mapping['inversedBy']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1356
                $mapping['isOwningSide'] = true;
1357
                $mapping['isInverseSide'] = false;
1358
            }
1359 1 View Code Duplication
            if (isset($mapping['mappedBy']) && $mapping['mappedBy']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1360
                $mapping['isInverseSide'] = true;
1361
                $mapping['isOwningSide'] = false;
1362
            }
1363 1 View Code Duplication
            if (isset($mapping['repositoryMethod'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1364
                $mapping['isInverseSide'] = true;
1365
                $mapping['isOwningSide'] = false;
1366
            }
1367 1
            if (!isset($mapping['orphanRemoval'])) {
1368 1
                $mapping['orphanRemoval'] = false;
1369
            }
1370
        }
1371
1372 1
        $this->applyStorageStrategy($mapping);
1373
1374 1
        $this->fieldMappings[$mapping['fieldName']] = $mapping;
1375 1
        if (isset($mapping['association'])) {
1376 1
            $this->associationMappings[$mapping['fieldName']] = $mapping;
1377
        }
1378
1379 1
        return $mapping;
1380
    }
1381
1382
    /**
1383
     * Validates the storage strategy of a mapping for consistency
1384
     * @param array $mapping
1385
     * @throws \Doctrine\ODM\MongoDB\Mapping\MappingException
1386
     */
1387 1
    private function applyStorageStrategy(array &$mapping)
1388
    {
1389 1
        if (! isset($mapping['type']) || isset($mapping['id'])) {
1390
            return;
1391
        }
1392
1393
        switch (true) {
1394 1
            case $mapping['type'] == 'int':
1395 1
            case $mapping['type'] == 'float':
1396 1
            case $mapping['type'] == 'increment':
1397
                $defaultStrategy = self::STORAGE_STRATEGY_SET;
1398
                $allowedStrategies = [self::STORAGE_STRATEGY_SET, self::STORAGE_STRATEGY_INCREMENT];
1399
                break;
1400
1401 1
            case $mapping['type'] == 'many':
1402
                $defaultStrategy = CollectionHelper::DEFAULT_STRATEGY;
1403
                $allowedStrategies = [
1404
                    self::STORAGE_STRATEGY_PUSH_ALL,
1405
                    self::STORAGE_STRATEGY_ADD_TO_SET,
1406
                    self::STORAGE_STRATEGY_SET,
1407
                    self::STORAGE_STRATEGY_SET_ARRAY,
1408
                    self::STORAGE_STRATEGY_ATOMIC_SET,
1409
                    self::STORAGE_STRATEGY_ATOMIC_SET_ARRAY,
1410
                ];
1411
                break;
1412
1413
            default:
1414 1
                $defaultStrategy = self::STORAGE_STRATEGY_SET;
1415 1
                $allowedStrategies = [self::STORAGE_STRATEGY_SET];
1416
        }
1417
1418 1
        if (! isset($mapping['strategy'])) {
1419 1
            $mapping['strategy'] = $defaultStrategy;
1420
        }
1421
1422 1
        if (! in_array($mapping['strategy'], $allowedStrategies)) {
1423
            throw MappingException::invalidStorageStrategy($this->name, $mapping['fieldName'], $mapping['type'], $mapping['strategy']);
1424
        }
1425
1426 1
        if (isset($mapping['reference']) && $mapping['type'] === 'many' && $mapping['isOwningSide']
1427 1
            && ! empty($mapping['sort']) && ! CollectionHelper::usesSet($mapping['strategy'])) {
1428
            throw MappingException::referenceManySortMustNotBeUsedWithNonSetCollectionStrategy($this->name, $mapping['fieldName'], $mapping['strategy']);
1429
        }
1430 1
    }
1431
1432
    /**
1433
     * Map a MongoGridFSFile.
1434
     *
1435
     * @param array $mapping The mapping information.
1436
     */
1437
    public function mapFile(array $mapping)
1438
    {
1439
        $mapping['file'] = true;
1440
        $mapping['type'] = 'file';
1441
        $this->mapField($mapping);
1442
    }
1443
1444
    /**
1445
     * Map a single embedded document.
1446
     *
1447
     * @param array $mapping The mapping information.
1448
     */
1449 3
    public function mapOneEmbedded(array $mapping)
1450
    {
1451 3
        $mapping['embedded'] = true;
1452 3
        $mapping['type'] = 'one';
1453 3
        $this->mapField($mapping);
1454
    }
1455
1456
    /**
1457
     * Map a collection of embedded documents.
1458
     *
1459
     * @param array $mapping The mapping information.
1460
     */
1461 4
    public function mapManyEmbedded(array $mapping)
1462
    {
1463 4
        $mapping['embedded'] = true;
1464 4
        $mapping['type'] = 'many';
1465 4
        $this->mapField($mapping);
1466
    }
1467
1468
    /**
1469
     * Map a single document reference.
1470
     *
1471
     * @param array $mapping The mapping information.
1472
     */
1473
    public function mapOneReference(array $mapping)
1474
    {
1475
        $mapping['reference'] = true;
1476
        $mapping['type'] = 'one';
1477
        $this->mapField($mapping);
1478
    }
1479
1480
    /**
1481
     * Map a collection of document references.
1482
     *
1483
     * @param array $mapping The mapping information.
1484
     */
1485 1
    public function mapManyReference(array $mapping)
1486
    {
1487 1
        $mapping['reference'] = true;
1488 1
        $mapping['type'] = 'many';
1489 1
        $this->mapField($mapping);
1490
    }
1491
1492
    /**
1493
     * INTERNAL:
1494
     * Adds a field mapping without completing/validating it.
1495
     * This is mainly used to add inherited field mappings to derived classes.
1496
     *
1497
     * @param array $fieldMapping
1498
     */
1499
    public function addInheritedFieldMapping(array $fieldMapping)
1500
    {
1501
        $this->fieldMappings[$fieldMapping['fieldName']] = $fieldMapping;
1502
1503
        if (isset($fieldMapping['association'])) {
1504
            $this->associationMappings[$fieldMapping['fieldName']] = $fieldMapping;
1505
        }
1506
    }
1507
1508
    /**
1509
     * INTERNAL:
1510
     * Adds an association mapping without completing/validating it.
1511
     * This is mainly used to add inherited association mappings to derived classes.
1512
     *
1513
     * @param array $mapping
1514
     *
1515
     * @return void
1516
     *
1517
     * @throws MappingException
1518
     */
1519 1
    public function addInheritedAssociationMapping(array $mapping/*, $owningClassName = null*/)
1520
    {
1521 1
        $this->associationMappings[$mapping['fieldName']] = $mapping;
1522 1
    }
1523
1524
    /**
1525
     * Checks whether the class has a mapped association with the given field name.
1526
     *
1527
     * @param string $fieldName
1528
     * @return boolean
1529
     */
1530
    public function hasReference($fieldName)
1531
    {
1532
        return isset($this->fieldMappings[$fieldName]['reference']);
1533
    }
1534
1535
    /**
1536
     * Checks whether the class has a mapped embed with the given field name.
1537
     *
1538
     * @param string $fieldName
1539
     * @return boolean
1540
     */
1541
    public function hasEmbed($fieldName)
1542
    {
1543
        return isset($this->fieldMappings[$fieldName]['embedded']);
1544
    }
1545
1546
    /**
1547
     * {@inheritDoc}
1548
     *
1549
     * Checks whether the class has a mapped association (embed or reference) with the given field name.
1550
     */
1551
    public function hasAssociation($fieldName)
1552
    {
1553
        return $this->hasReference($fieldName) || $this->hasEmbed($fieldName);
1554
    }
1555
1556
    /**
1557
     * {@inheritDoc}
1558
     *
1559
     * Checks whether the class has a mapped reference or embed for the specified field and
1560
     * is a single valued association.
1561
     */
1562
    public function isSingleValuedAssociation($fieldName)
1563
    {
1564
        return $this->isSingleValuedReference($fieldName) || $this->isSingleValuedEmbed($fieldName);
1565
    }
1566
1567
    /**
1568
     * {@inheritDoc}
1569
     *
1570
     * Checks whether the class has a mapped reference or embed for the specified field and
1571
     * is a collection valued association.
1572
     */
1573
    public function isCollectionValuedAssociation($fieldName)
1574
    {
1575
        return $this->isCollectionValuedReference($fieldName) || $this->isCollectionValuedEmbed($fieldName);
1576
    }
1577
1578
    /**
1579
     * Checks whether the class has a mapped association for the specified field
1580
     * and if yes, checks whether it is a single-valued association (to-one).
1581
     *
1582
     * @param string $fieldName
1583
     * @return boolean TRUE if the association exists and is single-valued, FALSE otherwise.
1584
     */
1585
    public function isSingleValuedReference($fieldName)
1586
    {
1587
        return isset($this->fieldMappings[$fieldName]['association']) &&
1588
            $this->fieldMappings[$fieldName]['association'] === self::REFERENCE_ONE;
1589
    }
1590
1591
    /**
1592
     * Checks whether the class has a mapped association for the specified field
1593
     * and if yes, checks whether it is a collection-valued association (to-many).
1594
     *
1595
     * @param string $fieldName
1596
     * @return boolean TRUE if the association exists and is collection-valued, FALSE otherwise.
1597
     */
1598
    public function isCollectionValuedReference($fieldName)
1599
    {
1600
        return isset($this->fieldMappings[$fieldName]['association']) &&
1601
            $this->fieldMappings[$fieldName]['association'] === self::REFERENCE_MANY;
1602
    }
1603
1604
    /**
1605
     * Checks whether the class has a mapped embedded document for the specified field
1606
     * and if yes, checks whether it is a single-valued association (to-one).
1607
     *
1608
     * @param string $fieldName
1609
     * @return boolean TRUE if the association exists and is single-valued, FALSE otherwise.
1610
     */
1611
    public function isSingleValuedEmbed($fieldName)
1612
    {
1613
        return isset($this->fieldMappings[$fieldName]['association']) &&
1614
            $this->fieldMappings[$fieldName]['association'] === self::EMBED_ONE;
1615
    }
1616
1617
    /**
1618
     * Checks whether the class has a mapped embedded document for the specified field
1619
     * and if yes, checks whether it is a collection-valued association (to-many).
1620
     *
1621
     * @param string $fieldName
1622
     * @return boolean TRUE if the association exists and is collection-valued, FALSE otherwise.
1623
     */
1624
    public function isCollectionValuedEmbed($fieldName)
1625
    {
1626
        return isset($this->fieldMappings[$fieldName]['association']) &&
1627
            $this->fieldMappings[$fieldName]['association'] === self::EMBED_MANY;
1628
    }
1629
1630
    /**
1631
     * Sets the ID generator used to generate IDs for instances of this class.
1632
     *
1633
     * @param \Doctrine\ODM\MongoDB\Id\AbstractIdGenerator $generator
1634
     */
1635 213
    public function setIdGenerator($generator)
1636
    {
1637 213
        $this->idGenerator = $generator;
1638 213
    }
1639
1640
    /**
1641
     * Casts the identifier to its portable PHP type.
1642
     *
1643
     * @param mixed $id
1644
     * @return mixed $id
1645
     */
1646
    public function getPHPIdentifierValue($id)
1647
    {
1648
        $idType = $this->fieldMappings[$this->identifier]['type'];
1649
        return Type::getType($idType)->convertToPHPValue($id);
1650
    }
1651
1652
    /**
1653
     * Casts the identifier to its database type.
1654
     *
1655
     * @param mixed $id
1656
     * @return mixed $id
1657
     */
1658
    public function getDatabaseIdentifierValue($id)
1659
    {
1660
        $idType = $this->fieldMappings[$this->identifier]['type'];
1661
        return Type::getType($idType)->convertToDatabaseValue($id);
1662
    }
1663
1664
    /**
1665
     * Sets the document identifier of a document.
1666
     *
1667
     * The value will be converted to a PHP type before being set.
1668
     *
1669
     * @param object $document
1670
     * @param mixed $id
1671
     */
1672
    public function setIdentifierValue($document, $id)
1673
    {
1674
        $id = $this->getPHPIdentifierValue($id);
1675
        $this->reflFields[$this->identifier]->setValue($document, $id);
1676
    }
1677
1678
    /**
1679
     * Gets the document identifier as a PHP type.
1680
     *
1681
     * @param object $document
1682
     * @return mixed $id
1683
     */
1684
    public function getIdentifierValue($document)
1685
    {
1686
        return $this->reflFields[$this->identifier]->getValue($document);
1687
    }
1688
1689
    /**
1690
     * {@inheritDoc}
1691
     *
1692
     * Since MongoDB only allows exactly one identifier field this is a proxy
1693
     * to {@see getIdentifierValue()} and returns an array with the identifier
1694
     * field as a key.
1695
     */
1696
    public function getIdentifierValues($object)
1697
    {
1698
        return array($this->identifier => $this->getIdentifierValue($object));
1699
    }
1700
1701
    /**
1702
     * Get the document identifier object as a database type.
1703
     *
1704
     * @param object $document
1705
     *
1706
     * @return \MongoId $id The MongoID object.
1707
     */
1708
    public function getIdentifierObject($document)
1709
    {
1710
        return $this->getDatabaseIdentifierValue($this->getIdentifierValue($document));
1711
    }
1712
1713
    /**
1714
     * Sets the specified field to the specified value on the given document.
1715
     *
1716
     * @param object $document
1717
     * @param string $field
1718
     * @param mixed $value
1719
     */
1720
    public function setFieldValue($document, $field, $value)
1721
    {
1722
        if ($document instanceof Proxy && ! $document->__isInitialized()) {
1723
            //property changes to an uninitialized proxy will not be tracked or persisted,
1724
            //so the proxy needs to be loaded first.
1725
            $document->__load();
1726
        }
1727
1728
        $this->reflFields[$field]->setValue($document, $value);
1729
    }
1730
1731
    /**
1732
     * Gets the specified field's value off the given document.
1733
     *
1734
     * @param object $document
1735
     * @param string $field
1736
     *
1737
     * @return mixed
1738
     */
1739
    public function getFieldValue($document, $field)
1740
    {
1741
        if ($document instanceof Proxy && $field !== $this->identifier && ! $document->__isInitialized()) {
1742
            $document->__load();
1743
        }
1744
1745
        return $this->reflFields[$field]->getValue($document);
1746
    }
1747
1748
    /**
1749
     * Gets the mapping of a field.
1750
     *
1751
     * @param string $fieldName  The field name.
1752
     *
1753
     * @return array  The field mapping.
1754
     *
1755
     * @throws MappingException if the $fieldName is not found in the fieldMappings array
1756
     */
1757
    public function getFieldMapping($fieldName)
1758
    {
1759
        if ( ! isset($this->fieldMappings[$fieldName])) {
1760
            throw MappingException::mappingNotFound($this->name, $fieldName);
1761
        }
1762
        return $this->fieldMappings[$fieldName];
1763
    }
1764
1765
    /**
1766
     * Gets mappings of fields holding embedded document(s).
1767
     *
1768
     * @return array of field mappings
1769
     */
1770
    public function getEmbeddedFieldsMappings()
1771
    {
1772
        return array_filter(
1773
            $this->associationMappings,
1774
            function($assoc) { return ! empty($assoc['embedded']); }
1775
        );
1776
    }
1777
1778
    /**
1779
     * Gets the field mapping by its DB name.
1780
     * E.g. it returns identifier's mapping when called with _id.
1781
     *
1782
     * @param string $dbFieldName
1783
     *
1784
     * @return array
1785
     * @throws MappingException
1786
     */
1787
    public function getFieldMappingByDbFieldName($dbFieldName)
1788
    {
1789
        foreach ($this->fieldMappings as $mapping) {
1790
            if ($mapping['name'] == $dbFieldName) {
1791
                return $mapping;
1792
            }
1793
        }
1794
1795
        throw MappingException::mappingNotFoundByDbName($this->name, $dbFieldName);
1796
    }
1797
1798
    /**
1799
     * Check if the field is not null.
1800
     *
1801
     * @param string $fieldName  The field name
1802
     *
1803
     * @return boolean  TRUE if the field is not null, FALSE otherwise.
1804
     */
1805
    public function isNullable($fieldName)
1806
    {
1807
        $mapping = $this->getFieldMapping($fieldName);
1808
        if ($mapping !== false) {
1809
            return isset($mapping['nullable']) && $mapping['nullable'] == true;
1810
        }
1811
        return false;
1812
    }
1813
1814
    /**
1815
     * Checks whether the document has a discriminator field and value configured.
1816
     *
1817
     * @return boolean
1818
     */
1819
    public function hasDiscriminator()
1820
    {
1821
        return isset($this->discriminatorField, $this->discriminatorValue);
1822
    }
1823
1824
    /**
1825
     * Sets the type of Id generator to use for the mapped class.
1826
     *
1827
     * @param string $generatorType Generator type.
1828
     */
1829 211
    public function setIdGeneratorType($generatorType)
1830
    {
1831 211
        $this->generatorType = $generatorType;
1832 211
    }
1833
1834
    /**
1835
     * Sets the Id generator options.
1836
     *
1837
     * @param array $generatorOptions Generator options.
1838
     */
1839
    public function setIdGeneratorOptions($generatorOptions)
1840
    {
1841
        $this->generatorOptions = $generatorOptions;
1842
    }
1843
1844
    /**
1845
     * @return boolean
1846
     */
1847
    public function isInheritanceTypeNone()
1848
    {
1849
        return $this->inheritanceType == self::INHERITANCE_TYPE_NONE;
1850
    }
1851
1852
    /**
1853
     * Checks whether the mapped class uses the SINGLE_COLLECTION inheritance mapping strategy.
1854
     *
1855
     * @return boolean
1856
     */
1857
    public function isInheritanceTypeSingleCollection()
1858
    {
1859
        return $this->inheritanceType == self::INHERITANCE_TYPE_SINGLE_COLLECTION;
1860
    }
1861
1862
    /**
1863
     * Checks whether the mapped class uses the COLLECTION_PER_CLASS inheritance mapping strategy.
1864
     *
1865
     * @return boolean
1866
     */
1867
    public function isInheritanceTypeCollectionPerClass()
1868
    {
1869
        return $this->inheritanceType == self::INHERITANCE_TYPE_COLLECTION_PER_CLASS;
1870
    }
1871
1872
    /**
1873
     * Sets the mapped subclasses of this class.
1874
     *
1875
     * @param string[] $subclasses The names of all mapped subclasses.
1876
     */
1877 2 View Code Duplication
    public function setSubclasses(array $subclasses)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1878
    {
1879 2
        foreach ($subclasses as $subclass) {
1880 2
            if (strpos($subclass, '\\') === false && strlen($this->namespace)) {
1881 1
                $this->subClasses[] = $this->namespace . '\\' . $subclass;
1882
            } else {
1883 2
                $this->subClasses[] = $subclass;
1884
            }
1885
        }
1886 2
    }
1887
1888
    /**
1889
     * Sets the parent class names.
1890
     * Assumes that the class names in the passed array are in the order:
1891
     * directParent -> directParentParent -> directParentParentParent ... -> root.
1892
     *
1893
     * @param string[] $classNames
1894
     */
1895 214
    public function setParentClasses(array $classNames)
1896
    {
1897 214
        $this->parentClasses = $classNames;
1898
1899 214
        if (count($classNames) > 0) {
1900 1
            $this->rootDocumentName = array_pop($classNames);
1901
        }
1902 214
    }
1903
1904
    /**
1905
     * Checks whether the class will generate a new \MongoId instance for us.
1906
     *
1907
     * @return boolean TRUE if the class uses the AUTO generator, FALSE otherwise.
1908
     */
1909
    public function isIdGeneratorAuto()
1910
    {
1911
        return $this->generatorType == self::GENERATOR_TYPE_AUTO;
1912
    }
1913
1914
    /**
1915
     * Checks whether the class will use a collection to generate incremented identifiers.
1916
     *
1917
     * @return boolean TRUE if the class uses the INCREMENT generator, FALSE otherwise.
1918
     */
1919
    public function isIdGeneratorIncrement()
1920
    {
1921
        return $this->generatorType == self::GENERATOR_TYPE_INCREMENT;
1922
    }
1923
1924
    /**
1925
     * Checks whether the class will generate a uuid id.
1926
     *
1927
     * @return boolean TRUE if the class uses the UUID generator, FALSE otherwise.
1928
     */
1929
    public function isIdGeneratorUuid()
1930
    {
1931
        return $this->generatorType == self::GENERATOR_TYPE_UUID;
1932
    }
1933
1934
    /**
1935
     * Checks whether the class uses no id generator.
1936
     *
1937
     * @return boolean TRUE if the class does not use any id generator, FALSE otherwise.
1938
     */
1939
    public function isIdGeneratorNone()
1940
    {
1941
        return $this->generatorType == self::GENERATOR_TYPE_NONE;
1942
    }
1943
1944
    /**
1945
     * Sets the version field mapping used for versioning. Sets the default
1946
     * value to use depending on the column type.
1947
     *
1948
     * @param array $mapping   The version field mapping array
1949
     *
1950
     * @throws LockException
1951
     */
1952
    public function setVersionMapping(array &$mapping)
1953
    {
1954
        if ($mapping['type'] !== 'int' && $mapping['type'] !== 'date') {
1955
            throw LockException::invalidVersionFieldType($mapping['type']);
1956
        }
1957
1958
        $this->isVersioned  = true;
1959
        $this->versionField = $mapping['fieldName'];
1960
    }
1961
1962
    /**
1963
     * Sets whether this class is to be versioned for optimistic locking.
1964
     *
1965
     * @param boolean $bool
1966
     */
1967 211
    public function setVersioned($bool)
1968
    {
1969 211
        $this->isVersioned = $bool;
1970 211
    }
1971
1972
    /**
1973
     * Sets the name of the field that is to be used for versioning if this class is
1974
     * versioned for optimistic locking.
1975
     *
1976
     * @param string $versionField
1977
     */
1978 211
    public function setVersionField($versionField)
1979
    {
1980 211
        $this->versionField = $versionField;
1981 211
    }
1982
1983
    /**
1984
     * Sets the version field mapping used for versioning. Sets the default
1985
     * value to use depending on the column type.
1986
     *
1987
     * @param array $mapping   The version field mapping array
1988
     *
1989
     * @throws \Doctrine\ODM\MongoDB\LockException
1990
     */
1991
    public function setLockMapping(array &$mapping)
1992
    {
1993
        if ($mapping['type'] !== 'int') {
1994
            throw LockException::invalidLockFieldType($mapping['type']);
1995
        }
1996
1997
        $this->isLockable = true;
1998
        $this->lockField = $mapping['fieldName'];
1999
    }
2000
2001
    /**
2002
     * Sets whether this class is to allow pessimistic locking.
2003
     *
2004
     * @param boolean $bool
2005
     */
2006
    public function setLockable($bool)
2007
    {
2008
        $this->isLockable = $bool;
2009
    }
2010
2011
    /**
2012
     * Sets the name of the field that is to be used for storing whether a document
2013
     * is currently locked or not.
2014
     *
2015
     * @param string $lockField
2016
     */
2017
    public function setLockField($lockField)
2018
    {
2019
        $this->lockField = $lockField;
2020
    }
2021
2022
    /**
2023
     * {@inheritDoc}
2024
     */
2025
    public function getFieldNames()
2026
    {
2027
        return array_keys($this->fieldMappings);
2028
    }
2029
2030
    /**
2031
     * {@inheritDoc}
2032
     */
2033
    public function getAssociationNames()
2034
    {
2035
        return array_keys($this->associationMappings);
2036
    }
2037
2038
    /**
2039
     * {@inheritDoc}
2040
     */
2041
    public function getTypeOfField($fieldName)
2042
    {
2043
        return isset($this->fieldMappings[$fieldName]) ?
2044
            $this->fieldMappings[$fieldName]['type'] : null;
2045
    }
2046
2047
    /**
2048
     * {@inheritDoc}
2049
     */
2050
    public function getAssociationTargetClass($assocName)
2051
    {
2052
        if ( ! isset($this->associationMappings[$assocName])) {
2053
            throw new InvalidArgumentException("Association name expected, '" . $assocName . "' is not an association.");
2054
        }
2055
2056
        return $this->associationMappings[$assocName]['targetDocument'];
2057
    }
2058
2059
    /**
2060
     * {@inheritDoc}
2061
     */
2062
    public function isAssociationInverseSide($fieldName)
2063
    {
2064
        throw new \BadMethodCallException(__METHOD__ . '() is not implemented yet.');
2065
    }
2066
2067
    /**
2068
     * {@inheritDoc}
2069
     */
2070
    public function getAssociationMappedByTargetField($fieldName)
2071
    {
2072
        throw new \BadMethodCallException(__METHOD__ . '() is not implemented yet.');
2073
    }
2074
}
2075