Skip to content

Commit

Permalink
feat: date filter and facets
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed May 3, 2024
1 parent 2e2b71a commit 9208e14
Show file tree
Hide file tree
Showing 24 changed files with 646 additions and 96 deletions.
24 changes: 24 additions & 0 deletions src/Dto/Search/Query/DateRangeRound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query;

/**
* @codeCoverageIgnore
*/
enum DateRangeRound : string
{
case START_OF_DAY = 'START_OF_DAY';
case START_OF_PREVIOUS_DAY = 'START_OF_PREVIOUS_DAY';
case END_OF_DAY = 'END_OF_DAY';
case END_OF_PREVIOUS_DAY = 'END_OF_PREVIOUS_DAY';
case START_OF_MONTH = 'START_OF_MONTH';
case START_OF_PREVIOUS_MONTH = 'START_OF_PREVIOUS_MONTH';
case END_OF_MONTH = 'END_OF_MONTH';
case END_OF_PREVIOUS_MONTH = 'END_OF_PREVIOUS_MONTH';
case START_OF_YEAR = 'START_OF_YEAR';
case START_OF_PREVIOUS_YEAR = 'START_OF_PREVIOUS_YEAR';
case END_OF_YEAR = 'END_OF_YEAR';
case END_OF_PREVIOUS_YEAR = 'END_OF_PREVIOUS_YEAR';
}
27 changes: 27 additions & 0 deletions src/Dto/Search/Query/Facet/AbsoluteDateRangeFacet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Facet;

use DateInterval;
use DateTime;

class AbsoluteDateRangeFacet extends Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly DateTime $from,
public readonly DateTime $to,
public readonly ?DateInterval $gap,
array $excludeFilter = []
) {
parent::__construct(
$key,
$excludeFilter
);
}
}
4 changes: 2 additions & 2 deletions src/Dto/Search/Query/Facet/CategoryFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class CategoryFacet extends FacetField
{
/**
* @param string[] $categories
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
array $categories,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct(
$key,
'sp_category_path',
$categories,
$excludeFilter
);
Expand Down
4 changes: 2 additions & 2 deletions src/Dto/Search/Query/Facet/ContentSectionTypeFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class ContentSectionTypeFacet extends FacetField
{
/**
* @param string[] $contentSectionTypes
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
array $contentSectionTypes,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct(
$key,
'sp_contenttype',
$contentSectionTypes,
$excludeFilter
);
Expand Down
5 changes: 4 additions & 1 deletion src/Dto/Search/Query/Facet/Facet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
*/
abstract class Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
public readonly string $key,
public readonly ?string $excludeFilter = null
public readonly array $excludeFilter = []
) {
}
}
4 changes: 2 additions & 2 deletions src/Dto/Search/Query/Facet/FacetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class FacetField extends Facet
{
/**
* @param string[] $terms
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly string $field,
public readonly array $terms,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct($key, $excludeFilter);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Dto/Search/Query/Facet/FacetMultiQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class FacetMultiQuery extends Facet
{
/**
* @param FacetQuery[] $queries
* @param string|null $excludeFilter
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly array $queries,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct($key, $excludeFilter);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Dto/Search/Query/Facet/FacetQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
*/
class FacetQuery extends Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly string $query,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct($key, $excludeFilter);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Dto/Search/Query/Facet/GroupFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class GroupFacet extends FacetField
{
/**
* @param string[] $groups
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly array $groups,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct(
$key,
'sp_group_path',
$groups,
$excludeFilter
);
Expand Down
4 changes: 2 additions & 2 deletions src/Dto/Search/Query/Facet/ObjectTypeFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class ObjectTypeFacet extends FacetField
{
/**
* @param string[] $objectTypes
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly array $objectTypes,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct(
$key,
'sp_objecttype',
$objectTypes,
$excludeFilter
);
Expand Down
30 changes: 30 additions & 0 deletions src/Dto/Search/Query/Facet/RelativeDateRangeFacet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Facet;

use Atoolo\Search\Dto\Search\Query\DateRangeRound;
use DateInterval;

class RelativeDateRangeFacet extends Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly ?\DateTime $base,
public readonly ?DateInterval $before,
public readonly ?DateInterval $after,
public readonly ?DateInterval $gap,
public readonly ?DateRangeRound $roundStart,
public readonly ?DateRangeRound $roundEnd,
array $excludeFilter = []
) {
parent::__construct(
$key,
$excludeFilter
);
}
}
4 changes: 2 additions & 2 deletions src/Dto/Search/Query/Facet/SiteFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class SiteFacet extends FacetField
{
/**
* @param string[] $sites
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly array $sites,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct(
$key,
'sp_site',
$sites,
$excludeFilter
);
Expand Down
6 changes: 6 additions & 0 deletions src/Dto/Search/Query/Filter/AbsoluteDateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Atoolo\Search\Dto\Search\Query\Filter;

use DateTime;
use InvalidArgumentException;

class AbsoluteDateRangeFilter extends Filter
{
Expand All @@ -17,6 +18,11 @@ public function __construct(
$key,
$key !== null ? [$key] : []
);
if ($this->from === null && $this->to === null) {
throw new InvalidArgumentException(
'At least `from` or `to` must be specified'
);
}
}

public function getQuery(): string
Expand Down
74 changes: 67 additions & 7 deletions src/Dto/Search/Query/Filter/RelativeDateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

namespace Atoolo\Search\Dto\Search\Query\Filter;

use Atoolo\Search\Dto\Search\Query\DateRangeRound;
use DateInterval;
use DateTime;
use InvalidArgumentException;

class RelativeDateRangeFilter extends Filter
{
public function __construct(
private readonly ?\DateTime $base,
private readonly ?DateTime $base,
private readonly ?DateInterval $before,
private readonly ?DateInterval $after,
private readonly ?DateRangeRound $roundStart,
private readonly ?DateRangeRound $roundEnd,
?string $key = null
) {
parent::__construct(
Expand All @@ -29,22 +33,38 @@ public function getQuery(): string
private function toSolrDateRage(): string
{
if ($this->before === null) {
$from = $this->getBaseInSolrSyntax() . "/DAY";
$from = $this->roundStart($this->getBaseInSolrSyntax());
} else {
$from = $this->toSolrIntervalSyntax($this->before, '-') .
'/DAY';
$from = $this->roundStart(
$this->toSolrIntervalSyntax($this->before, '-')
);
}

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

return '[' . $from . ' TO ' . $to . ']';
}

private function roundStart(string $start): string
{
return $start . $this->toSolrRound(
$this->roundStart ?? DateRangeRound::START_OF_DAY
);
}

private function roundEnd(string $start): string
{
return $start . $this->toSolrRound(
$this->roundEnd ?? DateRangeRound::END_OF_DAY
);
}

private function getBaseInSolrSyntax(): string
{
if ($this->base === null) {
Expand Down Expand Up @@ -88,4 +108,44 @@ private function toSolrIntervalSyntax(

return $interval;
}

private function toSolrRound(DateRangeRound $round): string
{
if ($round === DateRangeRound::START_OF_DAY) {
return '/DAY';
}
if ($round === DateRangeRound::START_OF_PREVIOUS_DAY) {
return '/DAY-1DAY';
}
if ($round === DateRangeRound::END_OF_DAY) {
return '/DAY+1DAY-1SECOND';
}
if ($round === DateRangeRound::END_OF_PREVIOUS_DAY) {
return '/DAY-1SECOND';
}
if ($round === DateRangeRound::START_OF_MONTH) {
return '/MONTH';
}
if ($round === DateRangeRound::START_OF_PREVIOUS_MONTH) {
return '/MONTH-1MONTH';
}
if ($round === DateRangeRound::END_OF_MONTH) {
return '/MONTH+1MONTH-1SECOND';
}
if ($round === DateRangeRound::END_OF_PREVIOUS_MONTH) {
return '/MONTH-1SECOND';
}
if ($round === DateRangeRound::START_OF_YEAR) {
return '/YEAR';
}
if ($round === DateRangeRound::START_OF_PREVIOUS_YEAR) {
return '/YEAR-1YEAR';
}
if ($round === DateRangeRound::END_OF_YEAR) {
return '/YEAR+1YEAR-1SECOND';
}
if ($round === DateRangeRound::END_OF_PREVIOUS_YEAR) {
return '/YEAR-1SECOND';
}
}
}
4 changes: 3 additions & 1 deletion src/Dto/Search/Query/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atoolo\Search\Dto\Search\Query\Facet\Facet;
use Atoolo\Search\Dto\Search\Query\Filter\Filter;
use Atoolo\Search\Dto\Search\Query\Sort\Criteria;
use DateTimeZone;

/**
* @codeCoverageIgnore
Expand All @@ -28,7 +29,8 @@ public function __construct(
public readonly array $sort,
public readonly array $filter,
public readonly array $facets,
public readonly QueryOperator $defaultQueryOperator
public readonly QueryOperator $defaultQueryOperator,
public readonly ?DateTimeZone $timeZone
) {
}
}
Loading

0 comments on commit 9208e14

Please sign in to comment.