Skip to content

Commit

Permalink
Merge pull request #203 from Ar0010r/allow-custom-orderby
Browse files Browse the repository at this point in the history
allow custom order by
  • Loading branch information
Jeroen-G authored Jul 28, 2023
2 parents 49f512d + ff34a4e commit 5bca9bc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/Infrastructure/Scout/ScoutSearchCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use JeroenG\Explorer\Domain\Syntax\Compound\QueryType;
use JeroenG\Explorer\Domain\Syntax\MultiMatch;
use JeroenG\Explorer\Domain\Syntax\Sort;
use JeroenG\Explorer\Domain\Syntax\SyntaxInterface;
use JeroenG\Explorer\Domain\Syntax\Term;
use JeroenG\Explorer\Domain\Syntax\Terms;
use Laravel\Scout\Builder;
Expand All @@ -33,7 +34,7 @@ class ScoutSearchCommandBuilder implements SearchCommandInterface

private ?string $minimumShouldMatch = null;

/** @var Sort[] */
/** @var SyntaxInterface[] */
private array $sort = [];

private array $aggregations = [];
Expand Down Expand Up @@ -207,7 +208,7 @@ public function setLimit(?int $limit): void

public function setSort(array $sort): void
{
Assert::allIsInstanceOf($sort, Sort::class);
Assert::allIsInstanceOf($sort, SyntaxInterface::class);
$this->sort = $sort;
}

Expand Down Expand Up @@ -304,9 +305,11 @@ public function getOffset(): ?int
return $this->offset;
}

/** @return Sort[] */
/** @return SyntaxInterface[] */
private static function getSorts(Builder $builder): array
{
return array_map(static fn ($order) => new Sort($order['column'], $order['direction']), $builder->orders);
return array_map(static function($order) {
return $order instanceof SyntaxInterface ? $order : new Sort($order['column'], $order['direction']);
}, $builder->orders);
}
}
8 changes: 4 additions & 4 deletions tests/Unit/ScoutSearchCommandBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ public function test_it_can_set_the_sort_order(): void
$command->setSort([new Sort('id', 'invalid')]);
}

public function test_it_only_accepts_sort_classes(): void
public function test_it_only_accepts_syntax_interface_classes(): void
{
$command = new ScoutSearchCommandBuilder();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected an instance of JeroenG\Explorer\Domain\Syntax\Sort. Got: string');
$this->expectExceptionMessage('Expected an instance of JeroenG\Explorer\Domain\Syntax\SyntaxInterface. Got: string');

$command->setSort(['not' => 'a class']);
}
Expand Down Expand Up @@ -174,11 +174,11 @@ public function test_it_can_get_the_sorting_from_the_scout_builder(): void
$builder->model = Mockery::mock(Model::class);

$builder->index = self::TEST_INDEX;
$builder->orders = [[ 'column' => 'id', 'direction' => 'asc']];
$builder->orders = [['column' => 'id', 'direction' => 'asc'], new Sort('name')];

$subject = ScoutSearchCommandBuilder::wrap($builder);

self::assertSame([['id' => 'asc']], $subject->getSort());
self::assertSame([['id' => 'asc'], ['name' => 'asc']], $subject->getSort());
}

public function test_it_can_get_the_fields_from_scout_builder(): void
Expand Down

0 comments on commit 5bca9bc

Please sign in to comment.