From 9a3a686c8f49b93bae405227085a855acf3bc393 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Fri, 19 Nov 2021 06:40:02 +0500 Subject: [PATCH] #5 support for string timestamps --- src/EntityHydrator.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/EntityHydrator.php b/src/EntityHydrator.php index 134dfb6..a6440b5 100644 --- a/src/EntityHydrator.php +++ b/src/EntityHydrator.php @@ -37,6 +37,7 @@ */ use function array_key_exists; use function class_exists; +use function ctype_digit; use function date_create; use function date_create_immutable; use function date_diff; @@ -391,7 +392,7 @@ private function hydrateFieldWithManyAssociations( private function resolveAssociation(string $targetEntity, $value) : ?object { if (is_int($value) || is_string($value)) { - return $this->entityManager->getReference($targetEntity, $value); + return $this->entityManager->getRepository($targetEntity)->find($value); } if (is_array($value)) { @@ -571,6 +572,10 @@ private function createDateTime($value) : ?DateTime if ($value instanceof DateTime) { return $value; } elseif (is_string($value)) { + if (ctype_digit($value)) { + return date_create()->setTimestamp((int) $value) ?: null; + } + return date_create($value) ?: null; } elseif (is_int($value)) { return date_create()->setTimestamp($value); @@ -593,6 +598,10 @@ private function createDateTimeImmutable($value) : ?DateTimeImmutable if ($value instanceof DateTimeImmutable) { return $value; } elseif (is_string($value)) { + if (ctype_digit($value)) { + return date_create_immutable()->setTimestamp((int) $value) ?: null; + } + return date_create_immutable($value) ?: null; } elseif (is_int($value)) { return date_create_immutable()->setTimestamp($value);