Skip to content

Commit

Permalink
#5 support for string timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Nov 19, 2021
1 parent 5788170 commit 9a3a686
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/EntityHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 9a3a686

Please sign in to comment.