Skip to content

Commit

Permalink
feat: add custom sort field
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed May 10, 2024
1 parent 3643bfe commit 46e38fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Dto/Search/Query/Sort/CustomField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Sort;

/**
* @codeCoverageIgnore
*/
class CustomField extends Criteria
{
public function __construct(
public readonly string $field,
Direction $direction = Direction::ASC
) {
parent::__construct($direction);
}
}
3 changes: 3 additions & 0 deletions src/Service/Search/Schema2xFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Atoolo\Search\Dto\Search\Query\Filter\RelativeDateRangeFilter;
use Atoolo\Search\Dto\Search\Query\Filter\SiteFilter;
use Atoolo\Search\Dto\Search\Query\Sort\Criteria;
use Atoolo\Search\Dto\Search\Query\Sort\CustomField;
use Atoolo\Search\Dto\Search\Query\Sort\Date;
use Atoolo\Search\Dto\Search\Query\Sort\Headline;
use Atoolo\Search\Dto\Search\Query\Sort\Name;
Expand Down Expand Up @@ -92,6 +93,8 @@ public function getSortField(Criteria $criteria): string
return 'sp_sortvalue';
case $criteria instanceof Score:
return 'score';
case $criteria instanceof CustomField:
return $criteria->field;
default:
throw new InvalidArgumentException(
'Unsupported sort criteria: ' . get_class($criteria)
Expand Down
10 changes: 10 additions & 0 deletions test/Service/Search/Schema2xFieldMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Atoolo\Search\Dto\Search\Query\Filter\RelativeDateRangeFilter;
use Atoolo\Search\Dto\Search\Query\Filter\SiteFilter;
use Atoolo\Search\Dto\Search\Query\Sort\Criteria;
use Atoolo\Search\Dto\Search\Query\Sort\CustomField;
use Atoolo\Search\Dto\Search\Query\Sort\Date;
use Atoolo\Search\Dto\Search\Query\Sort\Headline;
use Atoolo\Search\Dto\Search\Query\Sort\Name;
Expand Down Expand Up @@ -158,6 +159,15 @@ public function testGetSortField(
);
}

public function testGetSortCustomField(): void
{
$criteria = new CustomField('custom_field');
$this->assertEquals(
'custom_field',
$this->mapper->getSortField($criteria)
);
}

public function testUnsupportedSortField(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit 46e38fa

Please sign in to comment.