diff --git a/src/Meilisearch/Filter/BooleanFilterBuilder.php b/src/Meilisearch/Filter/BooleanFilterBuilder.php index b39541a..dcf531a 100644 --- a/src/Meilisearch/Filter/BooleanFilterBuilder.php +++ b/src/Meilisearch/Filter/BooleanFilterBuilder.php @@ -12,11 +12,28 @@ public function build(array $facets, array $facetsValues): array foreach ($facets as $facet) { if ($facet->type === 'bool' && isset($facetsValues[$facet->name])) { - $value = ($facetsValues[$facet->name] === '1') ? 'true' : 'false'; - $filters = [$facet->name . ' = ' . $value]; + $value = self::isTruthy($facetsValues[$facet->name]) ? 'true' : 'false'; + $filters[] = sprintf('%s = %s', $facet->name, $value); } } return $filters; } + + private static function isTruthy(mixed $value): bool + { + if (is_string($value)) { + return '1' === $value || 'true' === $value; + } + + if (is_bool($value)) { + return $value; + } + + if (is_int($value)) { + return 1 === $value; + } + + return false; + } } diff --git a/tests/Unit/Meilisearch/Builder/CompositeFilterBuilderTest.php b/tests/Unit/Meilisearch/Builder/CompositeFilterBuilderTest.php index 94eafa7..256549c 100644 --- a/tests/Unit/Meilisearch/Builder/CompositeFilterBuilderTest.php +++ b/tests/Unit/Meilisearch/Builder/CompositeFilterBuilderTest.php @@ -6,35 +6,35 @@ use PHPUnit\Framework\TestCase; use Setono\SyliusMeilisearchPlugin\Document\Metadata\Facet; +use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\ArrayFilterBuilder; +use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\BooleanFilterBuilder; use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\CompositeFilterBuilder; -use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\FilterBuilderInterface; +use Setono\SyliusMeilisearchPlugin\Meilisearch\Filter\FloatFilterBuilder; final class CompositeFilterBuilderTest extends TestCase { public function test_it_returns_filters(): void { - $brandFilterBuilder = $this->createMock(FilterBuilderInterface::class); - $brandFilterBuilder->method('build')->willReturn(['(brand = "brand1")']); - - $sizeFilterBuilder = $this->createMock(FilterBuilderInterface::class); - $sizeFilterBuilder->method('build')->willReturn(['(size = "size1" OR size = "size2")']); - $compositeFilterBuilder = new CompositeFilterBuilder(); - $compositeFilterBuilder->add($brandFilterBuilder); - $compositeFilterBuilder->add($sizeFilterBuilder); + $compositeFilterBuilder->add(new ArrayFilterBuilder()); + $compositeFilterBuilder->add(new BooleanFilterBuilder()); + $compositeFilterBuilder->add(new FloatFilterBuilder()); $onSaleFacet = new Facet('onSale', 'bool'); - $brandFacet = new Facet('brand', 'string'); - $sizeFacet = new Facet('size', 'string'); + $ecoFriendlyFacet = new Facet('ecoFriendly', 'bool'); + $brandFacet = new Facet('brand', 'array'); + $sizeFacet = new Facet('size', 'array'); $filters = $compositeFilterBuilder->build( - ['onSale' => $onSaleFacet, 'brand' => $brandFacet, 'size' => $sizeFacet], - ['onSale' => true, 'brand' => ['brand1'], 'size' => ['size1', 'size2']], + ['onSale' => $onSaleFacet, 'ecoFriendly' => $ecoFriendlyFacet, 'brand' => $brandFacet, 'size' => $sizeFacet], + ['onSale' => true, 'ecoFriendly' => true, 'brand' => ['brand1'], 'size' => ['size1', 'size2']], ); $this->assertSame([ '(brand = "brand1")', '(size = "size1" OR size = "size2")', + 'onSale = true', + 'ecoFriendly = true', ], $filters); } }