diff --git a/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php b/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php index 5611450..8c29493 100644 --- a/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php +++ b/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php @@ -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; @@ -33,7 +34,7 @@ class ScoutSearchCommandBuilder implements SearchCommandInterface private ?string $minimumShouldMatch = null; - /** @var Sort[] */ + /** @var SyntaxInterface[] */ private array $sort = []; private array $aggregations = []; @@ -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; } @@ -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); } } diff --git a/tests/Unit/ScoutSearchCommandBuilderTest.php b/tests/Unit/ScoutSearchCommandBuilderTest.php index 3e78f3d..93d843e 100644 --- a/tests/Unit/ScoutSearchCommandBuilderTest.php +++ b/tests/Unit/ScoutSearchCommandBuilderTest.php @@ -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']); } @@ -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