From bd67fa3d088763f6a9c342ea3ff557de568b8541 Mon Sep 17 00:00:00 2001 From: Johannes Przymusinski Date: Mon, 1 Jul 2024 23:15:12 +0200 Subject: [PATCH] [Timestampable] Prevent deprecated ArrayAccess on FieldMapping in doctrine/orm 3 --- CHANGELOG.md | 2 ++ src/Timestampable/Mapping/Event/Adapter/ORM.php | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea079941e4..ae5154bc69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ a release. --- ## [Unreleased] +### Fixed +- Prevent depected ArrayAccess on FieldMapping in Timestampable ORM with `doctrine/orm` 3 ## [3.16.1] ### Fixed diff --git a/src/Timestampable/Mapping/Event/Adapter/ORM.php b/src/Timestampable/Mapping/Event/Adapter/ORM.php index f00874ee7e..5d3608eeb0 100644 --- a/src/Timestampable/Mapping/Event/Adapter/ORM.php +++ b/src/Timestampable/Mapping/Event/Adapter/ORM.php @@ -42,7 +42,7 @@ public function setClock(ClockInterface $clock): void public function getDateValue($meta, $field) { $mapping = $meta->getFieldMapping($field); - $converter = Type::getType($mapping['type'] ?? Types::DATETIME_MUTABLE); + $converter = Type::getType($this->getType($mapping) ?? Types::DATETIME_MUTABLE); $platform = $this->getObjectManager()->getConnection()->getDriver()->getDatabasePlatform(); return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform); @@ -58,7 +58,7 @@ public function getDateValue($meta, $field) private function getRawDateValue($mapping) { $datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable(); - $type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? ''); + $type = $this->getType($mapping) ?? ''; if ('integer' === $type) { return (int) $datetime->format('U'); @@ -70,4 +70,17 @@ private function getRawDateValue($mapping) return \DateTime::createFromImmutable($datetime); } + + /** + * Extract the type from the given FieldMapping. + * Supports both, doctrine/orm 2 and 3 and can be replaces with $mapping->type after doctrine/orm 2 support was dropped. + */ + private function getType(FieldMapping|array $mapping): string|null + { + if(! $mapping instanceof FieldMapping) { + return $mapping['type'] ?? null; + } + + return $mapping->type; + } }