Skip to content

Commit

Permalink
Merge branch '4.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
tbialcz committed Feb 24, 2025
2 parents cbd646f + 438946d commit 3365458
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/lib/Search/Common/FieldValueMapper/DateMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}

0 comments on commit 3365458

Please sign in to comment.