Skip to content

Commit

Permalink
fix: after date range was wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Apr 26, 2024
1 parent e4afb03 commit f1799c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
16 changes: 9 additions & 7 deletions src/Dto/Search/Query/Filter/RelativeDateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ private function toSolrDateRage(): string
if ($this->before === null) {
$from = $this->getBaseInSolrSyntax() . "/DAY";
} else {
$from = $this->toSolrIntervalSyntax($this->before);
$from = $this->toSolrIntervalSyntax($this->before, '-') .
'/DAY';
}

if ($this->after === null) {
$to = $this->getBaseInSolrSyntax() . "/DAY+1DAY-1SECOND";
} else {
$to = $this->toSolrIntervalSyntax($this->after);
$to = $this->toSolrIntervalSyntax($this->after, '+') .
"/DAY+1DAY-1SECOND";
}

return '[' . $from . ' TO ' . $to . ']';
Expand All @@ -54,17 +56,17 @@ private function getBaseInSolrSyntax(): string
return $formatter->format('Y-m-d\TH:i:s\Z');
}

private function toSolrIntervalSyntax(DateInterval $value): string
private function toSolrIntervalSyntax(DateInterval $value, string $operator): string
{
$interval = $this->getBaseInSolrSyntax();
if ($value->y > 0) {
$interval = $interval . '-' . $value->y . 'YEARS';
$interval = $interval . $operator . $value->y . 'YEARS';
}
if ($value->m > 0) {
$interval = $interval . '-' . $value->m . 'MONTHS';
$interval = $interval . $operator . $value->m . 'MONTHS';
}
if ($value->d > 0) {
$interval = $interval . '-' . $value->d . 'DAYS';
$interval = $interval . $operator . $value->d . 'DAYS';
}
if ($value->h > 0) {
throw new InvalidArgumentException(
Expand All @@ -82,6 +84,6 @@ private function toSolrIntervalSyntax(DateInterval $value): string
);
}

return $interval . '/DAY';
return $interval;
}
}
24 changes: 16 additions & 8 deletions test/Dto/Search/Query/Filter/RelativeDateRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public static function additionProviderForBeforeIntervals(): array
public static function additionProviderForAfterIntervals(): array
{
return [
['P1D', 'sp_date_list:[NOW/DAY TO NOW-1DAYS/DAY]'],
['P1W', 'sp_date_list:[NOW/DAY TO NOW-7DAYS/DAY]'],
['P2M', 'sp_date_list:[NOW/DAY TO NOW-2MONTHS/DAY]'],
['P3Y', 'sp_date_list:[NOW/DAY TO NOW-3YEARS/DAY]'],
['P1D', 'sp_date_list:[NOW/DAY TO NOW+1DAYS/DAY+1DAY-1SECOND]'],
['P1W', 'sp_date_list:[NOW/DAY TO NOW+7DAYS/DAY+1DAY-1SECOND]'],
['P2M', 'sp_date_list:[NOW/DAY TO NOW+2MONTHS/DAY+1DAY-1SECOND]'],
['P3Y', 'sp_date_list:[NOW/DAY TO NOW+3YEARS/DAY+1DAY-1SECOND]'],
];
}

Expand All @@ -48,8 +48,16 @@ public static function additionProviderForAfterIntervals(): array
public static function additionProviderForBeforeAndAfterIntervals(): array
{
return [
['P1D', 'P1D', 'sp_date_list:[NOW-1DAYS/DAY TO NOW-1DAYS/DAY]'],
['P1W', 'P2M', 'sp_date_list:[NOW-7DAYS/DAY TO NOW-2MONTHS/DAY]'],
[
'P1D',
'P1D',
'sp_date_list:[NOW-1DAYS/DAY TO NOW+1DAYS/DAY+1DAY-1SECOND]'
],
[
'P1W',
'P2M',
'sp_date_list:[NOW-7DAYS/DAY TO NOW+2MONTHS/DAY+1DAY-1SECOND]'
],
];
}

Expand All @@ -71,14 +79,14 @@ public static function additionProviderWithBase(): array
null,
'P2M',
'sp_date_list:[2021-01-01T00:00:00Z/DAY' .
' TO 2021-01-01T00:00:00Z-2MONTHS/DAY]'
' TO 2021-01-01T00:00:00Z+2MONTHS/DAY+1DAY-1SECOND]'
],
[
new DateTime('2021-01-01 00:00:00'),
'P1W',
'P2M',
'sp_date_list:[2021-01-01T00:00:00Z-7DAYS/DAY' .
' TO 2021-01-01T00:00:00Z-2MONTHS/DAY]'
' TO 2021-01-01T00:00:00Z+2MONTHS/DAY+1DAY-1SECOND]'
],
];
}
Expand Down

0 comments on commit f1799c3

Please sign in to comment.