diff --git a/src/Orm/MongoHydrator.php b/src/Orm/MongoHydrator.php index c6c742b..d503c6b 100644 --- a/src/Orm/MongoHydrator.php +++ b/src/Orm/MongoHydrator.php @@ -20,9 +20,7 @@ final class MongoHydrator $propName = $field->property->getName(); $value = $doc[$key] ?? null; - if ($value instanceof \MongoDB\Model\BSONArray) { - $value = $value->getArrayCopy(); - } + $value = $this->normalizeBson($value); if ($field->isEmbedded && $value !== null) { $value = $this->hydrate($field->embeddedClass, $this->toArray($value)); @@ -72,6 +70,19 @@ final class MongoHydrator return $result; } + private function normalizeBson(mixed $value): mixed + { + if ($value instanceof \MongoDB\Model\BSONArray) { + return array_map($this->normalizeBson(...), $value->getArrayCopy()); + } + + if ($value instanceof BSONDocument) { + return array_map($this->normalizeBson(...), iterator_to_array($value)); + } + + return $value; + } + private function toArray(array|BSONDocument $doc): array { if ($doc instanceof BSONDocument) {