From 680231fc3d7a4756c1ec184c9c982f777e0fa05b Mon Sep 17 00:00:00 2001 From: Holger Veltrup Date: Mon, 13 May 2024 10:18:32 +0200 Subject: [PATCH] refacore: use ResourceLanguage for Query DTO's --- src/Console/Command/Suggest.php | 3 ++- src/Dto/Search/Query/SearchQuery.php | 3 ++- src/Dto/Search/Query/SearchQueryBuilder.php | 6 +++-- src/Dto/Search/Query/SuggestQuery.php | 3 ++- src/Service/Search/SolrSearch.php | 5 ++-- src/Service/Search/SolrSuggest.php | 4 +-- .../Search/Query/SearchQueryBuilderTest.php | 9 +++++-- test/Service/Search/SolrSearchTest.php | 27 ++++++++++--------- test/Service/Search/SolrSuggestTest.php | 7 ++--- 9 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/Console/Command/Suggest.php b/src/Console/Command/Suggest.php index bfdfb2d..eb788ca 100644 --- a/src/Console/Command/Suggest.php +++ b/src/Console/Command/Suggest.php @@ -5,6 +5,7 @@ namespace Atoolo\Search\Console\Command; use Atoolo\Resource\ResourceChannelFactory; +use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Console\Command\Io\TypifiedInput; use Atoolo\Search\Dto\Search\Query\Filter\ArchiveFilter; use Atoolo\Search\Dto\Search\Query\Filter\NotFilter; @@ -80,7 +81,7 @@ protected function buildQuery(string $terms, string $lang): SuggestQuery $excludeMedia = new NotFilter(new ObjectTypeFilter(['media'])); return new SuggestQuery( $terms, - $lang, + ResourceLanguage::of($lang), [ new ArchiveFilter(), $excludeMedia diff --git a/src/Dto/Search/Query/SearchQuery.php b/src/Dto/Search/Query/SearchQuery.php index 98f4c5a..03ca13d 100644 --- a/src/Dto/Search/Query/SearchQuery.php +++ b/src/Dto/Search/Query/SearchQuery.php @@ -4,6 +4,7 @@ namespace Atoolo\Search\Dto\Search\Query; +use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Dto\Search\Query\Facet\Facet; use Atoolo\Search\Dto\Search\Query\Filter\Filter; use Atoolo\Search\Dto\Search\Query\Sort\Criteria; @@ -23,7 +24,7 @@ class SearchQuery */ public function __construct( public readonly string $text, - public readonly string $lang, + public readonly ResourceLanguage $lang, public readonly int $offset, public readonly int $limit, public readonly array $sort, diff --git a/src/Dto/Search/Query/SearchQueryBuilder.php b/src/Dto/Search/Query/SearchQueryBuilder.php index 0063a58..9fac8b6 100644 --- a/src/Dto/Search/Query/SearchQueryBuilder.php +++ b/src/Dto/Search/Query/SearchQueryBuilder.php @@ -4,6 +4,7 @@ namespace Atoolo\Search\Dto\Search\Query; +use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Dto\Search\Query\Facet\Facet; use Atoolo\Search\Dto\Search\Query\Filter\Filter; use Atoolo\Search\Dto\Search\Query\Sort\Criteria; @@ -12,7 +13,7 @@ class SearchQueryBuilder { private string $text = ''; - private string $lang = ''; + private ResourceLanguage $lang; private int $offset = 0; private int $limit = 10; /** @@ -36,6 +37,7 @@ class SearchQueryBuilder public function __construct() { + $this->lang = ResourceLanguage::default(); } /** @@ -50,7 +52,7 @@ public function text(string $text): static /** * @return $this */ - public function lang(string $lang): static + public function lang(ResourceLanguage $lang): static { $this->lang = $lang; return $this; diff --git a/src/Dto/Search/Query/SuggestQuery.php b/src/Dto/Search/Query/SuggestQuery.php index 771e0d9..5083389 100644 --- a/src/Dto/Search/Query/SuggestQuery.php +++ b/src/Dto/Search/Query/SuggestQuery.php @@ -4,6 +4,7 @@ namespace Atoolo\Search\Dto\Search\Query; +use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Dto\Search\Query\Filter\Filter; /** @@ -20,7 +21,7 @@ class SuggestQuery */ public function __construct( public readonly string $text, - public readonly string $lang = '', + public readonly ResourceLanguage $lang, public readonly array $filter = [], public readonly int $limit = 10 ) { diff --git a/src/Service/Search/SolrSearch.php b/src/Service/Search/SolrSearch.php index 343b100..0abe651 100644 --- a/src/Service/Search/SolrSearch.php +++ b/src/Service/Search/SolrSearch.php @@ -41,14 +41,13 @@ public function __construct( public function search(SearchQuery $query): SearchResult { - $lang = ResourceLanguage::of($query->lang); - $index = $this->index->name($lang); + $index = $this->index->name($query->lang); $client = $this->clientFactory->create($index); $solrQuery = $this->buildSolrQuery($client, $query); /** @var SelectResult $result */ $result = $client->execute($solrQuery); - return $this->buildResult($query, $result, $lang); + return $this->buildResult($query, $result, $query->lang); } private function buildSolrQuery( diff --git a/src/Service/Search/SolrSuggest.php b/src/Service/Search/SolrSuggest.php index bdd5c0e..25da135 100644 --- a/src/Service/Search/SolrSuggest.php +++ b/src/Service/Search/SolrSuggest.php @@ -4,7 +4,6 @@ namespace Atoolo\Search\Service\Search; -use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Dto\Search\Query\Filter\Filter; use Atoolo\Search\Dto\Search\Query\SuggestQuery; use Atoolo\Search\Dto\Search\Result\Suggestion; @@ -43,8 +42,7 @@ public function __construct( */ public function suggest(SuggestQuery $query): SuggestResult { - $lang = ResourceLanguage::of($query->lang); - $index = $this->index->name($lang); + $index = $this->index->name($query->lang); $client = $this->clientFactory->create($index); $solrQuery = $this->buildSolrQuery($client, $query); diff --git a/test/Dto/Search/Query/SearchQueryBuilderTest.php b/test/Dto/Search/Query/SearchQueryBuilderTest.php index 15eea98..0cb8997 100644 --- a/test/Dto/Search/Query/SearchQueryBuilderTest.php +++ b/test/Dto/Search/Query/SearchQueryBuilderTest.php @@ -4,6 +4,7 @@ namespace Atoolo\Search\Test\Dto\Search\Query; +use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Dto\Search\Query\Facet\Facet; use Atoolo\Search\Dto\Search\Query\Filter\Filter; use Atoolo\Search\Dto\Search\Query\QueryOperator; @@ -34,9 +35,13 @@ public function testSetText(): void public function testSetLang(): void { - $this->builder->lang('en'); + $this->builder->lang(ResourceLanguage::of('en')); $query = $this->builder->build(); - $this->assertEquals('en', $query->lang, 'unexpected lang'); + $this->assertEquals( + ResourceLanguage::of('en'), + $query->lang, + 'unexpected lang' + ); } public function testSetOffset(): void diff --git a/test/Service/Search/SolrSearchTest.php b/test/Service/Search/SolrSearchTest.php index 1cfe7e8..e7c53f3 100644 --- a/test/Service/Search/SolrSearchTest.php +++ b/test/Service/Search/SolrSearchTest.php @@ -5,6 +5,7 @@ namespace Atoolo\Search\Test\Service\Search; use Atoolo\Resource\Resource; +use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Dto\Search\Query\Facet\Facet; use Atoolo\Search\Dto\Search\Query\Facet\MultiQueryFacet; use Atoolo\Search\Dto\Search\Query\Facet\ObjectTypeFacet; @@ -100,7 +101,7 @@ public function testSelectEmpty(): void { $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 1, [ @@ -124,7 +125,7 @@ public function testSelectWithText(): void { $query = new SearchQuery( 'cat dog', - '', + ResourceLanguage::default(), 0, 10, [ @@ -148,7 +149,7 @@ public function testSelectWithSort(): void { $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [ @@ -177,7 +178,7 @@ public function testSelectWithAndDefaultOperator(): void { $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -202,7 +203,7 @@ public function testSelectWithFilter(): void $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -236,7 +237,7 @@ public function testSelectWithFacets(): void $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -264,7 +265,7 @@ public function testSelectWithInvalidFacets(): void $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -298,7 +299,7 @@ public function testResulWithFacetField(): void $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -342,7 +343,7 @@ public function testResultWithFacetQuery(): void $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -392,7 +393,7 @@ public function testInvalidResultFacets(): void $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -421,7 +422,7 @@ public function testResultWithoutFacets(): void $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -443,7 +444,7 @@ public function testSetTimeZone(): void { $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], @@ -464,7 +465,7 @@ public function testSetDefaultTimeZone(): void { $query = new SearchQuery( '', - '', + ResourceLanguage::default(), 0, 10, [], diff --git a/test/Service/Search/SolrSuggestTest.php b/test/Service/Search/SolrSuggestTest.php index e15c629..7386779 100644 --- a/test/Service/Search/SolrSuggestTest.php +++ b/test/Service/Search/SolrSuggestTest.php @@ -4,6 +4,7 @@ namespace Atoolo\Search\Test\Service\Search; +use Atoolo\Resource\ResourceLanguage; use Atoolo\Search\Dto\Search\Query\Filter\ObjectTypeFilter; use Atoolo\Search\Dto\Search\Query\SuggestQuery; use Atoolo\Search\Dto\Search\Result\Suggestion; @@ -63,8 +64,8 @@ public function testSuggest(): void $filter = new ObjectTypeFilter(['test']); $query = new SuggestQuery( - 'myindex', 'cat', + ResourceLanguage::default(), [$filter] ); @@ -102,8 +103,8 @@ public function testSuggest(): void public function testEmptySuggest(): void { $query = new SuggestQuery( - 'myindex', 'cat', + ResourceLanguage::default(), ); $response = new Response(<<