Skip to content

Commit 8347ecd

Browse files
authored
Merge pull request #86 from apisearch-io/fix/fixed-filters-without-array-in-values
Filter values MUST be an array
2 parents 25b18fe + 6f880a5 commit 8347ecd

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Query/Filter.php

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Query;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Model\HttpTransportable;
1920

2021
/**
@@ -278,6 +279,13 @@ public function toArray(): array
278279
*/
279280
public static function createFromArray(array $array): self
280281
{
282+
if (
283+
isset($array['values']) &&
284+
!is_array($array['values'])
285+
) {
286+
throw InvalidFormatException::queryFormatNotValid($array);
287+
}
288+
281289
return self::create(
282290
$array['field'] ?? 'uuid.type',
283291
$array['values'] ?? [],

Tests/Query/FilterTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\Tests\Query;
1717

18+
use Apisearch\Exception\InvalidFormatException;
1819
use Apisearch\Query\Filter;
1920
use PHPUnit\Framework\TestCase;
2021

@@ -119,4 +120,19 @@ public function testCreateFromArrayWithDefaults()
119120
$this->assertEquals(Filter::TYPE_FIELD, $aggregation->getFilterType());
120121
$this->assertEquals([], $aggregation->getFilterTerms());
121122
}
123+
124+
/**
125+
* Test wrong values format.
126+
*/
127+
public function testWrongValuesFormat()
128+
{
129+
try {
130+
Filter::createFromArray([
131+
'values' => 'string',
132+
]);
133+
$this->fail('InvalidFormatException should be thrown');
134+
} catch (InvalidFormatException $exception) {
135+
// Silent pass
136+
}
137+
}
122138
}

Tests/Result/ResultTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function testCreateFromArrayAllValues()
196196
}
197197

198198
/**
199-
* Test multi result
199+
* Test multi result.
200200
*/
201201
public function testMultiResult()
202202
{

0 commit comments

Comments
 (0)