diff --git a/src/Behavior/EnumCast.php b/src/Behavior/EnumCast.php index 36a50ab..752deb1 100644 --- a/src/Behavior/EnumCast.php +++ b/src/Behavior/EnumCast.php @@ -12,7 +12,7 @@ * Convert backed-enum database values to and from PHP enum instances * * The enum's backing value (string|int) is stored in the database. On retrieval the raw scalar - * is converted to the corresponding enum case via {@see BackedEnum::from()}; on persistence the + * is converted to the corresponding enum case via {@see BackedEnum::tryFrom()}; on persistence the * enum instance is unwrapped back to its backing value. Null is preserved as null in both directions. * * ```php @@ -47,8 +47,6 @@ public function __construct(string $enum, array $properties) * @param int|string|null $value * * @return ?BackedEnum - * - * @throws ValueConversionException */ public function fromDb($value, $key, $_) { @@ -56,15 +54,7 @@ public function fromDb($value, $key, $_) return $value; } - try { - return $this->enum::from($value); - } catch (ValueError $e) { - throw new ValueConversionException(sprintf( - "%s (key: '%s')", - $e->getMessage(), - $key - )); - } + return $this->enum::tryFrom($value); } /** diff --git a/tests/Behavior/EnumCastTest.php b/tests/Behavior/EnumCastTest.php index cb6f24b..0384dd3 100644 --- a/tests/Behavior/EnumCastTest.php +++ b/tests/Behavior/EnumCastTest.php @@ -79,9 +79,7 @@ public function testToDbPassesThroughNull(): void public function testFromDbThrowsForUnknownBackingValue(): void { - $this->expectException(ValueConversionException::class); - - $this->behavior->fromDb('purple', 'color', null); + $this->assertNull($this->behavior->fromDb('purple', 'color', null)); } public function testFromDbIsIdempotentWhenValueIsAlreadyAnEnumInstance(): void