Skip to content

Commit

Permalink
Simplify and improve consistency in methods that provide current dates
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jun 19, 2023
1 parent 0db3937 commit af50ac1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
15 changes: 9 additions & 6 deletions src/SoftDeleteable/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ final class ODM extends BaseAdapterODM implements SoftDeleteableAdapter
*/
public function getDateValue($meta, $field)
{
$datetime = new \DateTime();
$mapping = $meta->getFieldMapping($field);
if (isset($mapping['type']) && 'timestamp' === $mapping['type']) {
return time();
$type = $mapping['type'] ?? null;

if ('timestamp' === $type) {
return (int) $datetime->format('U');
}
if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return new \DateTimeImmutable();

if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return \DateTimeImmutable::createFromMutable($datetime);
}

return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''))
->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
return $datetime;
}
}
14 changes: 8 additions & 6 deletions src/SoftDeleteable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ public function getDateValue($meta, $field)
*/
private function getRawDateValue(array $mapping)
{
if (isset($mapping['type']) && 'integer' === $mapping['type']) {
return time();
$datetime = new \DateTime();
$type = $mapping['type'] ?? null;

if ('integer' === $type) {
return (int) $datetime->format('U');
}

if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return new \DateTimeImmutable();
if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return \DateTimeImmutable::createFromMutable($datetime);
}

return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''))
->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
return $datetime;
}
}
15 changes: 9 additions & 6 deletions src/Timestampable/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ final class ODM extends BaseAdapterODM implements TimestampableAdapter
*/
public function getDateValue($meta, $field)
{
$datetime = new \DateTime();
$mapping = $meta->getFieldMapping($field);
if (isset($mapping['type']) && 'timestamp' === $mapping['type']) {
return time();
$type = $mapping['type'] ?? null;

if ('timestamp' === $type) {
return (int) $datetime->format('U');
}
if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return new \DateTimeImmutable();

if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return \DateTimeImmutable::createFromMutable($datetime);
}

return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''))
->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
return $datetime;
}
}
14 changes: 8 additions & 6 deletions src/Timestampable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ public function getDateValue($meta, $field)
*/
private function getRawDateValue(array $mapping)
{
if (isset($mapping['type']) && 'integer' === $mapping['type']) {
return time();
$datetime = new \DateTime();
$type = $mapping['type'] ?? null;

if ('integer' === $type) {
return (int) $datetime->format('U');
}

if (isset($mapping['type']) && in_array($mapping['type'], ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return new \DateTimeImmutable();
if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return \DateTimeImmutable::createFromMutable($datetime);
}

return \DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''))
->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
return $datetime;
}
}

0 comments on commit af50ac1

Please sign in to comment.