From 294b6787c1a7a8a09e59196bcab4dadb2a7cca59 Mon Sep 17 00:00:00 2001 From: Ar0010r Date: Tue, 20 Jun 2023 15:21:44 +0300 Subject: [PATCH 1/4] allow custom order by --- src/Infrastructure/Scout/ScoutSearchCommandBuilder.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php b/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php index 5611450..0c5ff87 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; } @@ -307,6 +308,8 @@ public function getOffset(): ?int /** @return Sort[] */ 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); } } From 765ce8e2100dd99d9aeeea2315ec3839b9657c0b Mon Sep 17 00:00:00 2001 From: Ar0010r Date: Tue, 20 Jun 2023 21:11:24 +0300 Subject: [PATCH 2/4] fix test_it_only_accepts_sort_classes test and rename it to test_it_only_accepts_syntax_interface_classes --- tests/Unit/ScoutSearchCommandBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/ScoutSearchCommandBuilderTest.php b/tests/Unit/ScoutSearchCommandBuilderTest.php index 3e78f3d..41164c5 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']); } From 50c92b114aa948483bd605d74b09080253268f96 Mon Sep 17 00:00:00 2001 From: Ar0010r Date: Tue, 20 Jun 2023 21:12:32 +0300 Subject: [PATCH 3/4] fix phpdoc for getSorts() method --- src/Infrastructure/Scout/ScoutSearchCommandBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php b/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php index 0c5ff87..8c29493 100644 --- a/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php +++ b/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php @@ -305,7 +305,7 @@ public function getOffset(): ?int return $this->offset; } - /** @return Sort[] */ + /** @return SyntaxInterface[] */ private static function getSorts(Builder $builder): array { return array_map(static function($order) { From ff34a4e3313bb74491909bdf7b744194a079682a Mon Sep 17 00:00:00 2001 From: Ar0010r Date: Fri, 28 Jul 2023 00:34:52 +0300 Subject: [PATCH 4/4] adjust test_it_can_get_the_sorting_from_the_scout_builder test --- tests/Unit/ScoutSearchCommandBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/ScoutSearchCommandBuilderTest.php b/tests/Unit/ScoutSearchCommandBuilderTest.php index 41164c5..93d843e 100644 --- a/tests/Unit/ScoutSearchCommandBuilderTest.php +++ b/tests/Unit/ScoutSearchCommandBuilderTest.php @@ -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