Skip to content

Commit

Permalink
Merge pull request #55 from cultuurnet/bugfix/WID-394-allow-mutliple-…
Browse files Browse the repository at this point in the history
…advanced-queries

WID-394 Allow multiple advanced queries (again)
  • Loading branch information
LucWollants authored May 3, 2021
2 parents 760a466 + d6a73d7 commit 63c9175
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace CultuurNet\SearchV3;

use CultuurNet\SearchV3\Parameter\Query;

final class SearchQuery implements SearchQueryInterface
{
/**
Expand Down Expand Up @@ -94,18 +96,32 @@ public function setLimit(int $limit): void
public function toArray(): array
{
$query = [];
$advancedQueries = [];

foreach ($this->parameters as $parameter) {
$key = $parameter->getKey();

if ($parameter instanceof Query) {
$advancedQueries[] = $parameter->getValue();
continue;
}

if (isset($query[$key])) {
if ($parameter->allowsMultiple()) {
$query[$key] = is_array($query[$key]) ? $query[$key] : [$query[$key]];
$query[$key][] = $parameter->getValue();
} else {
continue;
}
continue;
}

$query[$key] = $parameter->getValue();
}

if (!empty($advancedQueries)) {
if (count($advancedQueries) === 1) {
$query['q'] = $advancedQueries[0];
} else {
$query[$key] = $parameter->getValue();
$query['q'] = '(' . implode(') AND (', $advancedQueries) . ')';
}
}

Expand Down
52 changes: 52 additions & 0 deletions test/SearchQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CultuurNet\SearchV3\Parameter\Facet;
use CultuurNet\SearchV3\Parameter\Id;
use CultuurNet\SearchV3\Parameter\Label;
use CultuurNet\SearchV3\Parameter\Query;
use CultuurNet\SearchV3\ValueObjects\CalendarSummaryFormat;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -192,6 +193,57 @@ public function testToArrayMethodWithDuplicateFacetKeys(): void
$this->assertEquals($expectedQuery, $result);
}

public function testToArrayMethodWithOneAdvancedQuery(): void
{
$this->searchQuery->addParameter(new Query('foo:bar'));

$expectedQuery = [
'sort' => [
'title' => 'asc',
],
'embed' => true,
'start' => 10,
'limit' => 50,
'labels' => 'test-label',
'facets' => 'regions',
'embedCalendarSummaries' => [
'md-html',
'lg-text',
],
'q' => 'foo:bar',
];

$result = $this->searchQuery->toArray();

$this->assertEquals($expectedQuery, $result);
}

public function testToArrayMethodWithMultipleAdvancedQueries(): void
{
$this->searchQuery->addParameter(new Query('foo:bar'));
$this->searchQuery->addParameter(new Query('het depot'));

$expectedQuery = [
'sort' => [
'title' => 'asc',
],
'embed' => true,
'start' => 10,
'limit' => 50,
'labels' => 'test-label',
'facets' => 'regions',
'embedCalendarSummaries' => [
'md-html',
'lg-text',
],
'q' => '(foo:bar) AND (het depot)',
];

$result = $this->searchQuery->toArray();

$this->assertEquals($expectedQuery, $result);
}

public function testToStringMethod(): void
{
$expectedQueryString = 'labels=test-label&facets=regions&embedCalendarSummaries[0]=md-html&embedCalendarSummaries[1]=lg-text&sort[title]=asc&embed=1&start=10&limit=50';
Expand Down

0 comments on commit 63c9175

Please sign in to comment.