Skip to content

Commit 6220a23

Browse files
committed
Added a dedicated MappingException extending the Common one.
1 parent d0a31eb commit 6220a23

File tree

11 files changed

+138
-110
lines changed

11 files changed

+138
-110
lines changed

lib/Doctrine/ODM/MongoDB/DocumentRepository.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace Doctrine\ODM\MongoDB;
2222

2323
use Doctrine\Common\Persistence\ObjectRepository;
24+
use Doctrine\ODM\MongoDB\Mapping\MappingException;
2425

2526
/**
2627
* An DocumentRepository serves as a repository for documents with generic as well as
@@ -53,7 +54,7 @@ class DocumentRepository implements ObjectRepository
5354
protected $uow;
5455

5556
/**
56-
* @var Doctrine\ODM\MongoDB\Mapping\ClassMetadata
57+
* @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata
5758
*/
5859
protected $class;
5960

@@ -62,7 +63,7 @@ class DocumentRepository implements ObjectRepository
6263
*
6364
* @param DocumentManager $dm The DocumentManager to use.
6465
* @param UnitOfWork $uow The UnitOfWork to use.
65-
* @param ClassMetadata $classMetadata The class descriptor.
66+
* @param Mapping\ClassMetadata $classMetadata The class descriptor.
6667
*/
6768
public function __construct(DocumentManager $dm, UnitOfWork $uow, Mapping\ClassMetadata $class)
6869
{
@@ -108,12 +109,12 @@ public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
108109
list($identifierFieldName) = $this->class->getIdentifierFieldNames();
109110

110111
if (!isset($id[$identifierFieldName])) {
111-
throw MongoDBException::missingIdentifierField($this->documentName, $identifierFieldName);
112+
throw MappingException::missingIdentifierField($this->documentName, $identifierFieldName);
112113
}
113114

114115
$id = $id[$identifierFieldName];
115116
}
116-
117+
117118
// Check identity map first
118119
if ($document = $this->uow->tryGetById($id, $this->class->rootDocumentName)) {
119120
if ($lockMode != LockMode::NONE) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
namespace Doctrine\ODM\MongoDB\Mapping;
2121

22-
use Doctrine\ODM\MongoDB\MongoDBException,
23-
Doctrine\ODM\MongoDB\LockException;
22+
use Doctrine\ODM\MongoDB\LockException;
2423

2524
/**
2625
* A <tt>ClassMetadata</tt> instance holds all the object-document mapping metadata
@@ -195,6 +194,7 @@ public function newInstance()
195194
if ($this->prototype === null) {
196195
$this->prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
197196
}
197+
198198
return clone $this->prototype;
199199
}
200200
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Doctrine\ODM\MongoDB\DocumentManager,
2626
Doctrine\ODM\MongoDB\Configuration,
2727
Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
28-
Doctrine\ODM\MongoDB\MongoDBException,
28+
Doctrine\ODM\MongoDB\Mapping\MappingException,
2929
Doctrine\ODM\MongoDB\Events;
3030

3131
/**
@@ -153,7 +153,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
153153
try {
154154
$this->driver->loadMetadataForClass($class->getName(), $class);
155155
} catch (\ReflectionException $e) {
156-
throw MongoDBException::reflectionFailure($class->getName(), $e);
156+
throw MappingException::reflectionFailure($class->getName(), $e);
157157
}
158158

159159
$this->validateIdentifier($class);
@@ -193,7 +193,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
193193
protected function validateIdentifier($class)
194194
{
195195
if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument) {
196-
throw MongoDBException::identifierRequired($class->name);
196+
throw MappingException::identifierRequired($class->name);
197197
}
198198
}
199199

@@ -246,7 +246,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)
246246
case ClassMetadata::GENERATOR_TYPE_NONE;
247247
break;
248248
default:
249-
throw new MongoDBException("Unknown generator type: " . $class->generatorType);
249+
throw new MappingException("Unknown generator type: " . $class->generatorType);
250250
}
251251
}
252252

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Doctrine\ODM\MongoDB\Mapping;
2121

22-
use Doctrine\ODM\MongoDB\MongoDBException,
22+
use Doctrine\ODM\MongoDB\Mapping\MappingException,
2323
Doctrine\ODM\MongoDB\LockException,
2424
Doctrine\ODM\MongoDB\Proxy\Proxy,
2525
ReflectionClass;
@@ -568,7 +568,7 @@ public function setDiscriminatorField($discriminatorField)
568568
$discriminatorField['name'] = $discriminatorField['fieldName'];
569569
}
570570
if (isset($this->fieldMappings[$discriminatorField['name']])) {
571-
throw MongoDBException::duplicateFieldMapping($this->name, $discriminatorField['name']);
571+
throw MappingException::duplicateFieldMapping($this->name, $discriminatorField['name']);
572572
}
573573
$this->discriminatorField = $discriminatorField;
574574
}
@@ -590,7 +590,7 @@ public function setDiscriminatorMap(array $map)
590590
$this->discriminatorValue = $value;
591591
} else {
592592
if ( ! class_exists($className)) {
593-
throw MongoDBException::invalidClassInDiscriminatorMap($className, $this->name);
593+
throw MappingException::invalidClassInDiscriminatorMap($className, $this->name);
594594
}
595595
if (is_subclass_of($className, $this->name)) {
596596
$this->subClasses[] = $className;
@@ -943,16 +943,16 @@ public function mapField(array $mapping)
943943
$mapping['fieldName'] = $mapping['name'];
944944
}
945945
if ( ! isset($mapping['fieldName'])) {
946-
throw MongoDBException::missingFieldName($this->name);
946+
throw MappingException::missingFieldName($this->name);
947947
}
948948
if ( ! isset($mapping['name'])) {
949949
$mapping['name'] = $mapping['fieldName'];
950950
}
951951
if (isset($this->fieldMappings[$mapping['fieldName']])) {
952-
throw MongoDBException::duplicateFieldMapping($this->name, $mapping['fieldName']);
952+
throw MappingException::duplicateFieldMapping($this->name, $mapping['fieldName']);
953953
}
954954
if ($this->discriminatorField['name'] === $mapping['fieldName']) {
955-
throw MongoDBException::duplicateFieldMapping($this->name, $mapping['fieldName']);
955+
throw MappingException::duplicateFieldMapping($this->name, $mapping['fieldName']);
956956
}
957957
if (isset($mapping['targetDocument']) && strpos($mapping['targetDocument'], '\\') === false && strlen($this->namespace)) {
958958
$mapping['targetDocument'] = $this->namespace . '\\' . $mapping['targetDocument'];
@@ -1355,7 +1355,7 @@ public function getFieldValue($document, $field)
13551355
public function getFieldMapping($fieldName)
13561356
{
13571357
if ( ! isset($this->fieldMappings[$fieldName])) {
1358-
throw MongoDBException::mappingNotFound($this->name, $fieldName);
1358+
throw MappingException::mappingNotFound($this->name, $fieldName);
13591359
}
13601360
return $this->fieldMappings[$fieldName];
13611361
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
Doctrine\ODM\MongoDB\Events,
2828
Doctrine\ODM\MongoDB\Mapping\Annotations as ODM,
2929
Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo,
30-
Doctrine\ODM\MongoDB\MongoDBException;
30+
Doctrine\ODM\MongoDB\Mapping\MappingException;
3131

3232
/**
3333
* The AnnotationDriver reads the mapping metadata from docblock annotations.
@@ -96,7 +96,7 @@ public function loadMetadataForClass($className, ClassMetadata $class)
9696
}
9797

9898
if (!$documentAnnots) {
99-
throw MongoDBException::classIsNotAValidDocument($className);
99+
throw MappingException::classIsNotAValidDocument($className);
100100
}
101101

102102
// find the winning document annotation
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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\Common\Persistence\Mapping\MappingException as BaseMappingException;
23+
24+
/**
25+
* Class for all exceptions related to the Doctrine MongoDB ODM
26+
*
27+
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28+
* @link www.doctrine-project.org
29+
* @since 1.0
30+
* @author Jonathan H. Wage <jonwage@gmail.com>
31+
*/
32+
class MappingException extends BaseMappingException
33+
{
34+
public static function typeExists($name)
35+
{
36+
return new self('Type '.$name.' already exists.');
37+
}
38+
39+
public static function typeNotFound($name)
40+
{
41+
return new self('Type to be overwritten '.$name.' does not exist.');
42+
}
43+
44+
public static function mappingNotFound($className, $fieldName)
45+
{
46+
return new self("No mapping found for field '$fieldName' in class '$className'.");
47+
}
48+
49+
public static function duplicateFieldMapping($document, $fieldName)
50+
{
51+
return new self('Property "'.$fieldName.'" in "'.$document.'" was already declared, but it must be declared only once');
52+
}
53+
54+
/**
55+
* Throws an exception that indicates that a class used in a discriminator map does not exist.
56+
* An example would be an outdated (maybe renamed) classname.
57+
*
58+
* @param string $className The class that could not be found
59+
* @param string $owningClass The class that declares the discriminator map.
60+
* @return self
61+
*/
62+
public static function invalidClassInDiscriminatorMap($className, $owningClass)
63+
{
64+
return new self(
65+
"Document class '$className' used in the discriminator map of class '$owningClass' ".
66+
"does not exist."
67+
);
68+
}
69+
70+
public static function missingFieldName($className)
71+
{
72+
return new self("The Document class '$className' field mapping misses the 'fieldName' attribute.");
73+
}
74+
75+
public static function classIsNotAValidDocument($className)
76+
{
77+
return new self('Class '.$className.' is not a valid document or mapped super class.');
78+
}
79+
80+
/**
81+
* Exception for reflection exceptions - adds the document name,
82+
* because there might be long classnames that will be shortened
83+
* within the stacktrace
84+
*
85+
* @param string $document The document's name
86+
* @param \ReflectionException $previousException
87+
*/
88+
public static function reflectionFailure($document, \ReflectionException $previousException)
89+
{
90+
return new self('An error occurred in ' . $document, 0, $previousException);
91+
}
92+
93+
public static function identifierRequired($documentName)
94+
{
95+
return new self("No identifier/primary key specified for Document '$documentName'."
96+
. " Every Document must have an identifier/primary key.");
97+
}
98+
99+
public static function missingIdentifierField($className, $fieldName)
100+
{
101+
return new self("The identifier $fieldName is missing for a query of " . $className);
102+
}
103+
}

lib/Doctrine/ODM/MongoDB/Mapping/Types/Type.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Doctrine\ODM\MongoDB\Mapping\Types;
2121

22-
use Doctrine\ODM\MongoDB\MongoDBException;
22+
use Doctrine\ODM\MongoDB\Mapping\MappingException;
2323

2424
/**
2525
* The Type interface.
@@ -130,7 +130,7 @@ public static function getType($type)
130130
/**
131131
* Get a Type instance based on the type of the passed php variable.
132132
*
133-
* @param mixed $variable
133+
* @param mixed $variable
134134
* @return Doctrine\ODM\MongoDB\Mapping\Types\Type $type
135135
* @throws InvalidArgumentException
136136
*/
@@ -167,12 +167,12 @@ public static function convertPHPToDatabaseValue($value)
167167
* @static
168168
* @param string $name Name of the type. This should correspond to what getName() returns.
169169
* @param string $className The class name of the custom type.
170-
* @throws MongoDBException
170+
* @throws MappingException
171171
*/
172172
public static function addType($name, $className)
173173
{
174174
if (isset(self::$typesMap[$name])) {
175-
throw MongoDBException::typeExists($name);
175+
throw MappingException::typeExists($name);
176176
}
177177

178178
self::$typesMap[$name] = $className;
@@ -196,12 +196,12 @@ public static function hasType($name)
196196
* @static
197197
* @param string $name
198198
* @param string $className
199-
* @throws MongoDBException
199+
* @throws MappingException
200200
*/
201201
public static function overrideType($name, $className)
202202
{
203203
if ( ! isset(self::$typesMap[$name])) {
204-
throw MongoDBException::typeNotFound($name);
204+
throw MappingException::typeNotFound($name);
205205
}
206206

207207
self::$typesMap[$name] = $className;

0 commit comments

Comments
 (0)