-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35529ba
commit 05313ad
Showing
4 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Dto\Search\Query\Filter; | ||
|
||
use DateInterval; | ||
use InvalidArgumentException; | ||
|
||
class RelativeDateRangeFilter extends Filter | ||
{ | ||
public function __construct( | ||
private readonly ?DateInterval $from, | ||
private readonly ?DateInterval $to, | ||
?string $key = null | ||
) { | ||
parent::__construct( | ||
$key, | ||
$key !== null ? [$key] : [] | ||
); | ||
} | ||
|
||
public function getQuery(): string | ||
{ | ||
return 'sp_date_list:' . $this->toSolrDateRage(); | ||
} | ||
|
||
private function toSolrDateRage(): string | ||
{ | ||
if ($this->from === null) { | ||
$from = "NOW/DAY"; | ||
} else { | ||
$from = $this->toSolrIntervalSyntax($this->from); | ||
} | ||
|
||
if ($this->to === null) { | ||
$to = "NOW/DAY+1DAY-1SECOND"; | ||
} else { | ||
$to = $this->toSolrIntervalSyntax($this->to); | ||
} | ||
|
||
return '[' . $from . ' TO ' . $to . ']'; | ||
} | ||
|
||
private function toSolrIntervalSyntax(DateInterval $value): string | ||
{ | ||
$interval = 'NOW'; | ||
if ($value->y > 0) { | ||
$interval = $interval . '-' . $value->y . 'YEARS'; | ||
} | ||
if ($value->m > 0) { | ||
$interval = $interval . '-' . $value->m . 'MONTHS'; | ||
} | ||
if ($value->d > 0) { | ||
$interval = $interval . '-' . $value->d . 'DAYS'; | ||
} | ||
if ($value->h > 0) { | ||
throw new InvalidArgumentException( | ||
'Hours are not supported for the RelativeDateRangeFilter' | ||
); | ||
} | ||
if ($value->i > 0) { | ||
throw new InvalidArgumentException( | ||
'Minutes are not supported for the RelativeDateRangeFilter' | ||
); | ||
} | ||
if ($value->s > 0) { | ||
throw new InvalidArgumentException( | ||
'Seconds are not supported for the RelativeDateRangeFilter' | ||
); | ||
} | ||
|
||
return $interval . '/DAY'; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test/Dto/Search/Query/Filter/AbsoluteDateRangeFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Test\Dto\Search\Query\Filter; | ||
|
||
use Atoolo\Search\Dto\Search\Query\Filter\AbsoluteDateRangeFilter; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(AbsoluteDateRangeFilter::class)] | ||
class AbsoluteDateRangeFilterTest extends TestCase | ||
{ | ||
public function testGetQueryWithFromAndTo(): void | ||
{ | ||
$from = new \DateTime('2021-01-01 00:00:00'); | ||
$to = new \DateTime('2021-01-02 00:00:00'); | ||
$filter = new AbsoluteDateRangeFilter($from, $to, 'sp_date_list'); | ||
$this->assertEquals( | ||
'sp_date_list:[2021-01-01T00:00:00Z TO 2021-01-02T00:00:00Z]', | ||
$filter->getQuery() | ||
); | ||
} | ||
|
||
public function testGetQueryWithFrom(): void | ||
{ | ||
$from = new \DateTime('2021-01-01 00:00:00'); | ||
$filter = new AbsoluteDateRangeFilter($from, null, 'sp_date_list'); | ||
$this->assertEquals( | ||
'sp_date_list:[2021-01-01T00:00:00Z TO *]', | ||
$filter->getQuery() | ||
); | ||
} | ||
|
||
public function testGetQueryWithTo(): void | ||
{ | ||
$to = new \DateTime('2021-01-02 00:00:00'); | ||
$filter = new AbsoluteDateRangeFilter(null, $to, 'sp_date_list'); | ||
$this->assertEquals( | ||
'sp_date_list:[* TO 2021-01-02T00:00:00Z]', | ||
$filter->getQuery() | ||
); | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
test/Dto/Search/Query/Filter/RelativeDateRangeFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Search\Test\Dto\Search\Query\Filter; | ||
|
||
use Atoolo\Search\Dto\Search\Query\Filter\RelativeDateRangeFilter; | ||
use DateInterval; | ||
use Exception; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(RelativeDateRangeFilter::class)] | ||
class RelativeDateRangeFilterTest extends TestCase | ||
{ | ||
/** | ||
* @return array<array{string, string}> | ||
*/ | ||
public static function additionProviderForFromIntervals(): array | ||
{ | ||
return [ | ||
['P1D', 'sp_date_list:[NOW-1DAYS/DAY TO NOW/DAY+1DAY-1SECOND]'], | ||
['P1W', 'sp_date_list:[NOW-7DAYS/DAY TO NOW/DAY+1DAY-1SECOND]'], | ||
['P2M', 'sp_date_list:[NOW-2MONTHS/DAY TO NOW/DAY+1DAY-1SECOND]'], | ||
['P3Y', 'sp_date_list:[NOW-3YEARS/DAY TO NOW/DAY+1DAY-1SECOND]'], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array<array{string, string}> | ||
*/ | ||
public static function additionProviderForToIntervals(): 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]'], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array<array{string, string, string}> | ||
*/ | ||
public static function additionProviderForFromAndToIntervals(): 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]'], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array<array{string}> | ||
*/ | ||
public static function additionProviderForInvalidIntervals(): array | ||
{ | ||
return [ | ||
['PT1H'], | ||
['PT1M'], | ||
['PT1S'], | ||
]; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
#[DataProvider('additionProviderForFromIntervals')] | ||
public function testGetQueryWithFrom( | ||
string $from, | ||
string $expected | ||
): void { | ||
$filter = new RelativeDateRangeFilter( | ||
new DateInterval($from), | ||
null, | ||
); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$filter->getQuery(), | ||
'unexpected query' | ||
); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
#[DataProvider('additionProviderForToIntervals')] | ||
public function testGetQueryWithTo( | ||
string $to, | ||
string $expected | ||
): void { | ||
$filter = new RelativeDateRangeFilter( | ||
null, | ||
new DateInterval($to), | ||
); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$filter->getQuery(), | ||
'unexpected query' | ||
); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
#[DataProvider('additionProviderForFromAndToIntervals')] | ||
public function testGetQueryWithFromAndTo( | ||
string $from, | ||
string $to, | ||
string $expected | ||
): void { | ||
$filter = new RelativeDateRangeFilter( | ||
new DateInterval($from), | ||
new DateInterval($to), | ||
); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$filter->getQuery(), | ||
'unexpected query' | ||
); | ||
} | ||
|
||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
#[DataProvider('additionProviderForInvalidIntervals')] | ||
public function testGetQueryWithInvalidIntervals( | ||
string $interval | ||
): void { | ||
$filter = new RelativeDateRangeFilter( | ||
null, | ||
new DateInterval($interval), | ||
); | ||
$this->expectException(InvalidArgumentException::class); | ||
$filter->getQuery(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters