Skip to content

Commit bc740ff

Browse files
authored
Merge pull request #2340 from franmomu/add_constants
Use ClassMetadata constants
2 parents 37ac643 + 3854c13 commit bc740ff

File tree

11 files changed

+51
-40
lines changed

11 files changed

+51
-40
lines changed

lib/Doctrine/ODM/MongoDB/Mapping/Annotations/EmbedMany.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\Mapping\Annotations;
66

7+
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
78
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
89

910
/**
@@ -14,7 +15,7 @@
1415
final class EmbedMany extends AbstractField
1516
{
1617
/** @var string */
17-
public $type = 'many';
18+
public $type = ClassMetadata::MANY;
1819

1920
/** @var bool */
2021
public $embedded = true;

lib/Doctrine/ODM/MongoDB/Mapping/Annotations/EmbedOne.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Doctrine\ODM\MongoDB\Mapping\Annotations;
66

7+
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
8+
79
/**
810
* Embeds a single document
911
*
@@ -12,7 +14,7 @@
1214
final class EmbedOne extends AbstractField
1315
{
1416
/** @var string */
15-
public $type = 'one';
17+
public $type = ClassMetadata::ONE;
1618

1719
/** @var bool */
1820
public $embedded = true;

lib/Doctrine/ODM/MongoDB/Mapping/Annotations/File/Metadata.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\ODM\MongoDB\Mapping\Annotations\File;
66

77
use Doctrine\ODM\MongoDB\Mapping\Annotations\AbstractField;
8+
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
89

910
/**
1011
* @Annotation
@@ -15,7 +16,7 @@ final class Metadata extends AbstractField
1516
public $name = 'metadata';
1617

1718
/** @var string */
18-
public $type = 'one';
19+
public $type = ClassMetadata::ONE;
1920

2021
/** @var bool */
2122
public $embedded = true;

lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
final class ReferenceMany extends AbstractField
1616
{
1717
/** @var string */
18-
public $type = 'many';
18+
public $type = ClassMetadata::MANY;
1919

2020
/** @var bool */
2121
public $reference = true;

lib/Doctrine/ODM/MongoDB/Mapping/Annotations/ReferenceOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
final class ReferenceOne extends AbstractField
1515
{
1616
/** @var string */
17-
public $type = 'one';
17+
public $type = ClassMetadata::ONE;
1818

1919
/** @var bool */
2020
public $reference = true;

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,19 @@
231231
*/
232232
public const DEFAULT_DISCRIMINATOR_FIELD = '_doctrine_class_name';
233233

234+
/**
235+
* Association types
236+
*/
234237
public const REFERENCE_ONE = 1;
235238
public const REFERENCE_MANY = 2;
236239
public const EMBED_ONE = 3;
237240
public const EMBED_MANY = 4;
238-
public const MANY = 'many';
239-
public const ONE = 'one';
241+
242+
/**
243+
* Mapping types
244+
*/
245+
public const MANY = 'many';
246+
public const ONE = 'one';
240247

241248
/**
242249
* The types of storeAs references
@@ -1136,7 +1143,7 @@ public function setShardKey(array $keys, array $options = []): void
11361143
continue;
11371144
}
11381145

1139-
if (in_array($this->fieldMappings[$field]['type'], ['many', 'collection'])) {
1146+
if (in_array($this->fieldMappings[$field]['type'], [self::MANY, Type::COLLECTION])) {
11401147
throw MappingException::noMultiKeyShardKeys($this->getName(), $field);
11411148
}
11421149

@@ -1456,7 +1463,7 @@ private function applyStorageStrategy(array &$mapping): void
14561463
}
14571464

14581465
switch (true) {
1459-
case $mapping['type'] === 'many':
1466+
case $mapping['type'] === self::MANY:
14601467
$defaultStrategy = CollectionHelper::DEFAULT_STRATEGY;
14611468
$allowedStrategies = [
14621469
self::STORAGE_STRATEGY_PUSH_ALL,
@@ -1468,7 +1475,7 @@ private function applyStorageStrategy(array &$mapping): void
14681475
];
14691476
break;
14701477

1471-
case $mapping['type'] === 'one':
1478+
case $mapping['type'] === self::ONE:
14721479
$defaultStrategy = self::STORAGE_STRATEGY_SET;
14731480
$allowedStrategies = [self::STORAGE_STRATEGY_SET];
14741481
break;
@@ -1491,7 +1498,7 @@ private function applyStorageStrategy(array &$mapping): void
14911498
}
14921499

14931500
if (
1494-
isset($mapping['reference']) && $mapping['type'] === 'many' && $mapping['isOwningSide']
1501+
isset($mapping['reference']) && $mapping['type'] === self::MANY && $mapping['isOwningSide']
14951502
&& ! empty($mapping['sort']) && ! CollectionHelper::usesSet($mapping['strategy'])
14961503
) {
14971504
throw MappingException::referenceManySortMustNotBeUsedWithNonSetCollectionStrategy($this->name, $mapping['fieldName'], $mapping['strategy']);
@@ -1506,7 +1513,7 @@ private function applyStorageStrategy(array &$mapping): void
15061513
public function mapOneEmbedded(array $mapping): void
15071514
{
15081515
$mapping['embedded'] = true;
1509-
$mapping['type'] = 'one';
1516+
$mapping['type'] = self::ONE;
15101517
$this->mapField($mapping);
15111518
}
15121519

@@ -1518,7 +1525,7 @@ public function mapOneEmbedded(array $mapping): void
15181525
public function mapManyEmbedded(array $mapping): void
15191526
{
15201527
$mapping['embedded'] = true;
1521-
$mapping['type'] = 'many';
1528+
$mapping['type'] = self::MANY;
15221529
$this->mapField($mapping);
15231530
}
15241531

@@ -1530,7 +1537,7 @@ public function mapManyEmbedded(array $mapping): void
15301537
public function mapOneReference(array $mapping): void
15311538
{
15321539
$mapping['reference'] = true;
1533-
$mapping['type'] = 'one';
1540+
$mapping['type'] = self::ONE;
15341541
$this->mapField($mapping);
15351542
}
15361543

@@ -1542,7 +1549,7 @@ public function mapOneReference(array $mapping): void
15421549
public function mapManyReference(array $mapping): void
15431550
{
15441551
$mapping['reference'] = true;
1545-
$mapping['type'] = 'many';
1552+
$mapping['type'] = self::MANY;
15461553
$this->mapField($mapping);
15471554
}
15481555

@@ -2223,7 +2230,7 @@ public function mapField(array $mapping): array
22232230
throw MappingException::owningAndInverseReferencesRequireTargetDocument($this->name, $mapping['fieldName']);
22242231
}
22252232

2226-
if ($this->isEmbeddedDocument && $mapping['type'] === 'many' && isset($mapping['strategy']) && CollectionHelper::isAtomic($mapping['strategy'])) {
2233+
if ($this->isEmbeddedDocument && $mapping['type'] === self::MANY && isset($mapping['strategy']) && CollectionHelper::isAtomic($mapping['strategy'])) {
22272234
throw MappingException::atomicCollectionStrategyNotAllowed($mapping['strategy'], $this->name, $mapping['fieldName']);
22282235
}
22292236

@@ -2241,19 +2248,19 @@ public function mapField(array $mapping): array
22412248
);
22422249
}
22432250

2244-
if (isset($mapping['reference']) && $mapping['type'] === 'one') {
2251+
if (isset($mapping['reference']) && $mapping['type'] === self::ONE) {
22452252
$mapping['association'] = self::REFERENCE_ONE;
22462253
}
22472254

2248-
if (isset($mapping['reference']) && $mapping['type'] === 'many') {
2255+
if (isset($mapping['reference']) && $mapping['type'] === self::MANY) {
22492256
$mapping['association'] = self::REFERENCE_MANY;
22502257
}
22512258

2252-
if (isset($mapping['embedded']) && $mapping['type'] === 'one') {
2259+
if (isset($mapping['embedded']) && $mapping['type'] === self::ONE) {
22532260
$mapping['association'] = self::EMBED_ONE;
22542261
}
22552262

2256-
if (isset($mapping['embedded']) && $mapping['type'] === 'many') {
2263+
if (isset($mapping['embedded']) && $mapping['type'] === self::MANY) {
22572264
$mapping['association'] = self::EMBED_MANY;
22582265
}
22592266

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,25 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C
300300

301301
if (isset($xmlRoot->{'embed-one'})) {
302302
foreach ($xmlRoot->{'embed-one'} as $embed) {
303-
$this->addEmbedMapping($metadata, $embed, 'one');
303+
$this->addEmbedMapping($metadata, $embed, ClassMetadata::ONE);
304304
}
305305
}
306306

307307
if (isset($xmlRoot->{'embed-many'})) {
308308
foreach ($xmlRoot->{'embed-many'} as $embed) {
309-
$this->addEmbedMapping($metadata, $embed, 'many');
309+
$this->addEmbedMapping($metadata, $embed, ClassMetadata::MANY);
310310
}
311311
}
312312

313313
if (isset($xmlRoot->{'reference-many'})) {
314314
foreach ($xmlRoot->{'reference-many'} as $reference) {
315-
$this->addReferenceMapping($metadata, $reference, 'many');
315+
$this->addReferenceMapping($metadata, $reference, ClassMetadata::MANY);
316316
}
317317
}
318318

319319
if (isset($xmlRoot->{'reference-one'})) {
320320
foreach ($xmlRoot->{'reference-one'} as $reference) {
321-
$this->addReferenceMapping($metadata, $reference, 'one');
321+
$this->addReferenceMapping($metadata, $reference, ClassMetadata::ONE);
322322
}
323323
}
324324

@@ -379,7 +379,7 @@ private function addFieldMapping(ClassMetadata $class, array $mapping): void
379379
private function addEmbedMapping(ClassMetadata $class, SimpleXMLElement $embed, string $type): void
380380
{
381381
$attributes = $embed->attributes();
382-
$defaultStrategy = $type === 'one' ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
382+
$defaultStrategy = $type === ClassMetadata::ONE ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
383383
$mapping = [
384384
'type' => $type,
385385
'embedded' => true,
@@ -428,7 +428,7 @@ private function addReferenceMapping(ClassMetadata $class, $reference, string $t
428428
}
429429

430430
$attributes = $reference->attributes();
431-
$defaultStrategy = $type === 'one' ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
431+
$defaultStrategy = $type === ClassMetadata::ONE ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
432432
$mapping = [
433433
'cascade' => $cascade,
434434
'orphanRemoval' => isset($attributes['orphan-removal']) ? ((string) $attributes['orphan-removal'] === 'true') : false,
@@ -746,7 +746,7 @@ private function addGridFSMappings(ClassMetadata $class, SimpleXMLElement $xmlRo
746746
}
747747

748748
$xmlRoot->metadata->addAttribute('field', 'metadata');
749-
$this->addEmbedMapping($class, $xmlRoot->metadata, 'one');
749+
$this->addEmbedMapping($class, $xmlRoot->metadata, ClassMetadata::ONE);
750750
}
751751
}
752752

lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ private function prepareQueryElement(string $fieldName, $value = null, ?ClassMet
12181218
}
12191219

12201220
if (
1221-
$mapping['type'] === 'many' && CollectionHelper::isHash($mapping['strategy'])
1221+
$mapping['type'] === ClassMetadata::MANY && CollectionHelper::isHash($mapping['strategy'])
12221222
&& isset($e[2])
12231223
) {
12241224
$objectProperty = $e[2];
@@ -1575,7 +1575,7 @@ private function prepareReference(string $fieldName, $value, array $mapping, boo
15751575
throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $mapping['storeAs']));
15761576
}
15771577

1578-
if ($mapping['type'] === 'many') {
1578+
if ($mapping['type'] === ClassMetadata::MANY) {
15791579
return [[$fieldName, ['$elemMatch' => array_intersect_key($reference, $keys)]]];
15801580
}
15811581

lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function prepareUpdateData($document)
182182
}
183183

184184
// @ReferenceMany, @EmbedMany
185-
} elseif (isset($mapping['association']) && $mapping['type'] === 'many' && $new) {
185+
} elseif (isset($mapping['association']) && $mapping['type'] === ClassMetadata::MANY && $new) {
186186
if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($new)) {
187187
$updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
188188
} elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($new)) {

lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ public function primeReferences(ClassMetadata $class, $documents, string $fieldN
139139
continue;
140140
}
141141

142-
if ($mapping['type'] === 'one' && $fieldValue instanceof GhostObjectInterface && ! $fieldValue->isProxyInitialized()) {
142+
if ($mapping['type'] === ClassMetadata::ONE && $fieldValue instanceof GhostObjectInterface && ! $fieldValue->isProxyInitialized()) {
143143
$refClass = $this->dm->getClassMetadata(get_class($fieldValue));
144144
$id = $this->uow->getDocumentIdentifier($fieldValue);
145145
$groupedIds[$refClass->name][serialize($id)] = $id;
146-
} elseif ($mapping['type'] === 'many' && $fieldValue instanceof PersistentCollectionInterface) {
146+
} elseif ($mapping['type'] === ClassMetadata::MANY && $fieldValue instanceof PersistentCollectionInterface) {
147147
$this->addManyReferences($fieldValue, $groupedIds);
148148
}
149149
}

0 commit comments

Comments
 (0)