Skip to content

Commit bb6a4df

Browse files
committed
feat(doctrine): support backed enum for faceted properties
1 parent 6862480 commit bb6a4df

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/Adapter/Doctrine/DoctrineAdapter.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,17 @@ private function getFacetDistributions(Query $query, SearchInterface $search): a
9393
$uncheckedFacets = [];
9494
$qb = $helper->getFacetTermQuery($facet);
9595
foreach ($qb->getQuery()->getArrayResult() as $row) {
96-
if (\in_array($row['value'], $checkedValues)) {
97-
$checkedFacets[$row['value']] = $row['total'];
96+
$rowValue = null;
97+
if ($row['value'] instanceof \BackedEnum) {
98+
$rowValue = $row['value']->value;
9899
} else {
99-
$uncheckedFacets[$row['value']] = $row['total'];
100+
$rowValue = $row['value'];
101+
}
102+
103+
if (\in_array($rowValue, $checkedValues)) {
104+
$checkedFacets[$rowValue] = $row['total'];
105+
} else {
106+
$uncheckedFacets[$rowValue] = $row['total'];
100107
}
101108
}
102109

tests/Adapter/Doctrine/DoctrineAdapterTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Mezcalito\UxSearchBundle\Search\Filter\RangeFilter;
1717
use Mezcalito\UxSearchBundle\Search\Filter\TermFilter;
1818
use Mezcalito\UxSearchBundle\Search\ResultSet\ResultSet;
19+
use Mezcalito\UxSearchBundle\Tests\Fixtures\Adapter\Doctrine\CategoryEnum;
1920
use Mezcalito\UxSearchBundle\Tests\Fixtures\Adapter\Doctrine\Foo;
2021

2122
class DoctrineAdapterTest extends AbstractDoctrineTestCase
@@ -53,14 +54,16 @@ public function testSearch(): void
5354
public function testSearchWithFilter(): void
5455
{
5556
$this->createDatabase([
56-
new Foo('A', '1', 10),
57-
new Foo('A', '1', 13), // filtered
58-
new Foo('B', '2', 12), // filtered
59-
new Foo('C', '2', 13), // filtered
57+
new Foo('A', '1', 10, CategoryEnum::PHARMACY),
58+
new Foo('A', '1', 13, CategoryEnum::PHARMACY), // filtered
59+
new Foo('B', '2', 12, CategoryEnum::PHARMACY), // filtered
60+
new Foo('C', '2', 13, CategoryEnum::PHARMACY), // filtered
61+
new Foo('D', '2', 25, CategoryEnum::GRILL), // filtered
6062
]);
6163

6264
$this->query->addActiveFilter(new RangeFilter('o.price', 10, 12));
6365
$this->query->addActiveFilter(new TermFilter('o.type', ['A']));
66+
$this->query->addActiveFilter(new TermFilter('o.category', [CategoryEnum::PHARMACY]));
6467

6568
$resultSet = $this->adapter->search($this->query, $this->search);
6669

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the UxSearch project.
5+
*
6+
* (c) Mezcalito (https://www.mezcalito.fr)
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Mezcalito\UxSearchBundle\Tests\Fixtures\Adapter\Doctrine;
15+
16+
enum CategoryEnum: string
17+
{
18+
case GRILL = 'grill';
19+
case PHARMACY = 'pharmacy';
20+
}

tests/Fixtures/Adapter/Doctrine/Foo.php

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct(
2727
#[ORM\Column] public ?string $type = null,
2828
#[ORM\Column] public ?string $brand = null,
2929
#[ORM\Column] public ?float $price = null,
30+
#[ORM\Column(nullable: true)] public ?CategoryEnum $category = null,
3031
) {
3132
}
3233
}

0 commit comments

Comments
 (0)