diff --git a/src/lib/Search/Common/FieldValueMapper/DateMapper.php b/src/lib/Search/Common/FieldValueMapper/DateMapper.php index b6b64fe006..ac4062716b 100644 --- a/src/lib/Search/Common/FieldValueMapper/DateMapper.php +++ b/src/lib/Search/Common/FieldValueMapper/DateMapper.php @@ -8,6 +8,7 @@ namespace Ibexa\Core\Search\Common\FieldValueMapper; use DateTime; +use DateTimeInterface; use Exception; use Ibexa\Contracts\Core\Search\Field; use Ibexa\Contracts\Core\Search\FieldType\DateField; @@ -26,17 +27,28 @@ public function canMap(Field $field): bool public function map(Field $field): string { - $value = $field->getValue(); - if (is_numeric($value)) { - $date = new DateTime("@{$value}"); - } else { - try { - $date = new DateTime($value); - } catch (Exception $e) { - throw new InvalidArgumentException('Invalid date provided: ' . $value); - } + $date = $field->getValue(); + + if (!$date instanceof DateTimeInterface) { + $date = $this->convertToDateTime($date); } return $date->format('Y-m-d\\TH:i:s\\Z'); } + + /** + * @param string|int $value + */ + private function convertToDateTime($value): DateTimeInterface + { + if (is_numeric($value)) { + return new DateTime("@{$value}"); + } + + try { + return new DateTime($value); + } catch (Exception $e) { + throw new InvalidArgumentException('Invalid date provided: ' . $value); + } + } }