Skip to content

Commit

Permalink
Fix exception messages and XML Driver
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed Feb 22, 2022
1 parent 76d0553 commit cf4e5b2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
22 changes: 22 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Hydrator/HydratorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Doctrine\ODM\MongoDB\Hydrator;

use BackedEnum;
use Doctrine\ODM\MongoDB\MongoDBException;
use ValueError;

use function sprintf;

Expand Down Expand Up @@ -53,4 +55,24 @@ public static function associationItemTypeMismatch(string $className, string $fi
$actualType
));
}

/**
* @param class-string $className
* @param class-string<BackedEnum> $enumType
*/
public static function invalidEnumValue(
string $className,
string $fieldName,
string $value,
string $enumType,
ValueError $previous
): self {
return new self(sprintf(
'Trying to hydrate an enum property "%s::%s", but "%s" is not listed as one of "%s" cases',
$className,
$fieldName,
$value,
$enumType
), 0, $previous);
}
}
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C
$mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
}

if (isset($mapping['enum-type'])) {
$mapping['enumType'] = (string) $mapping['enum-type'];
if (isset($attributes['enum-type'])) {
$mapping['enumType'] = (string) $attributes['enum-type'];
}

if (isset($attributes['field-name'])) {
Expand Down
24 changes: 2 additions & 22 deletions lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,36 +281,16 @@ public static function schemaValidationError(int $errorCode, string $errorMessag

public static function enumsRequirePhp81(string $className, string $fieldName): self
{
return new self(sprintf('Enum types require PHP 8.1 in %s::$%s', $className, $fieldName));
return new self(sprintf('Enum types require PHP 8.1 in %s::%s', $className, $fieldName));
}

public static function nonEnumTypeMapped(string $className, string $fieldName, string $enumType): self
{
return new self(sprintf(
'Attempting to map non-enum type %s as enum in document class %s::$%s',
'Attempting to map a non-enum type %s as an enum: %s::%s',
$enumType,
$className,
$fieldName
));
}

/**
* @param class-string $className
* @param class-string<BackedEnum> $enumType
*/
public static function invalidEnumValue(
string $className,
string $fieldName,
string $value,
string $enumType,
ValueError $previous
): self {
return new self(sprintf(
'Trying to hydrate enum property "%s::$%s". Problem: Case "%s" is not listed in enum "%s"',
$className,
$fieldName,
$value,
$enumType
), 0, $previous);
}
}
11 changes: 3 additions & 8 deletions lib/Doctrine/ODM/MongoDB/Mapping/ReflectionEnumProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Mapping;

use BackedEnum;
use Doctrine\ODM\MongoDB\Hydrator\HydratorException;
use ReflectionProperty;
use ReturnTypeWillChange;
use ValueError;
Expand Down Expand Up @@ -45,13 +46,7 @@ public function getValue($object = null)
return null;
}

$enum = $this->originalReflectionProperty->getValue($object);

if ($enum === null) {
return null;
}

return $enum->value;
return $this->originalReflectionProperty->getValue($object)->value;
}

/**
Expand All @@ -67,7 +62,7 @@ public function setValue($object, $value = null): void
} catch (ValueError $e) {
assert(is_string($value) || is_int($value));

throw MappingException::invalidEnumValue(
throw HydratorException::invalidEnumValue(
get_class($object),
$this->originalReflectionProperty->getName(),
(string) $value,
Expand Down

0 comments on commit cf4e5b2

Please sign in to comment.