Skip to content

Commit

Permalink
refacore: use ResourceLanguage for Query DTO's
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed May 13, 2024
1 parent dbb9ff6 commit 680231f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/Console/Command/Suggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Dto/Search/Query/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/Dto/Search/Query/SearchQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,7 +13,7 @@
class SearchQueryBuilder
{
private string $text = '';
private string $lang = '';
private ResourceLanguage $lang;
private int $offset = 0;
private int $limit = 10;
/**
Expand All @@ -36,6 +37,7 @@ class SearchQueryBuilder

public function __construct()
{
$this->lang = ResourceLanguage::default();
}

/**
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Dto/Search/Query/SuggestQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Atoolo\Search\Dto\Search\Query;

use Atoolo\Resource\ResourceLanguage;
use Atoolo\Search\Dto\Search\Query\Filter\Filter;

/**
Expand All @@ -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
) {
Expand Down
5 changes: 2 additions & 3 deletions src/Service/Search/SolrSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions src/Service/Search/SolrSuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions test/Dto/Search/Query/SearchQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
27 changes: 14 additions & 13 deletions test/Service/Search/SolrSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function testSelectEmpty(): void
{
$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
1,
[
Expand All @@ -124,7 +125,7 @@ public function testSelectWithText(): void
{
$query = new SearchQuery(
'cat dog',
'',
ResourceLanguage::default(),
0,
10,
[
Expand All @@ -148,7 +149,7 @@ public function testSelectWithSort(): void
{
$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[
Expand Down Expand Up @@ -177,7 +178,7 @@ public function testSelectWithAndDefaultOperator(): void
{
$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand All @@ -202,7 +203,7 @@ public function testSelectWithFilter(): void

$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand Down Expand Up @@ -236,7 +237,7 @@ public function testSelectWithFacets(): void

$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand Down Expand Up @@ -264,7 +265,7 @@ public function testSelectWithInvalidFacets(): void

$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand Down Expand Up @@ -298,7 +299,7 @@ public function testResulWithFacetField(): void

$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand Down Expand Up @@ -342,7 +343,7 @@ public function testResultWithFacetQuery(): void

$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand Down Expand Up @@ -392,7 +393,7 @@ public function testInvalidResultFacets(): void

$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand Down Expand Up @@ -421,7 +422,7 @@ public function testResultWithoutFacets(): void

$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand All @@ -443,7 +444,7 @@ public function testSetTimeZone(): void
{
$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand All @@ -464,7 +465,7 @@ public function testSetDefaultTimeZone(): void
{
$query = new SearchQuery(
'',
'',
ResourceLanguage::default(),
0,
10,
[],
Expand Down
7 changes: 4 additions & 3 deletions test/Service/Search/SolrSuggestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,8 +64,8 @@ public function testSuggest(): void
$filter = new ObjectTypeFilter(['test']);

$query = new SuggestQuery(
'myindex',
'cat',
ResourceLanguage::default(),
[$filter]
);

Expand Down Expand Up @@ -102,8 +103,8 @@ public function testSuggest(): void
public function testEmptySuggest(): void
{
$query = new SuggestQuery(
'myindex',
'cat',
ResourceLanguage::default(),
);

$response = new Response(<<<END
Expand All @@ -126,8 +127,8 @@ public function testEmptySuggest(): void
public function testInvalidSuggestResponse(): void
{
$query = new SuggestQuery(
'myindex',
'cat',
ResourceLanguage::default(),
);

$response = new Response("none json");
Expand Down

0 comments on commit 680231f

Please sign in to comment.