Skip to content

Commit

Permalink
support for php 8 enums in hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleta committed May 2, 2022
1 parent 2d88616 commit ac69d17
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Hydrator/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ protected function doHydrateRowData($className, array $data)
if ($property->isPublic()) {
$entity->$name = $value;
} else {
/** @var \UnitEnum|\BackedEnum $enumClass */
$enumClass = null;
foreach ($property->getAttributes() as $attribute) {
$enumClass = $attribute->getArguments()['enumType'] ?? null;
if($enumClass !== null) {
break;
}
}

if($enumClass !== null) {
$enumInterfaces = array_keys(class_implements($enumClass));

if(in_array(\BackedEnum::class, $enumInterfaces)) {
$value = $enumClass::tryFrom($value);
} elseif (in_array(\UnitEnum::class, $enumInterfaces)) {
$value = $enumClass::$value;
}
}

$property->setAccessible(true);
$property->setValue($entity, $value);
$property->setAccessible(false);
Expand Down

0 comments on commit ac69d17

Please sign in to comment.