-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
339 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Builder; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class FilterBuilder implements FilterBuilderInterface | ||
{ | ||
public function build(Request $request): array | ||
{ | ||
$filters = []; | ||
|
||
if ($request->query->has('onSale')) { | ||
$filters[] = 'onSale = true'; | ||
} | ||
|
||
return $filters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Builder; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
interface FilterBuilderInterface | ||
{ | ||
/** | ||
* Takes a Symfony request and returns a filter ready for the Meilisearch client | ||
*/ | ||
public function build(Request $request): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Form\Builder; | ||
|
||
use Setono\CompositeCompilerPass\CompositeService; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
/** | ||
* @extends CompositeService<FacetFormBuilderInterface> | ||
*/ | ||
final class CompositeFacetFormBuilder extends CompositeService implements FacetFormBuilderInterface | ||
{ | ||
public function build(FormBuilderInterface $builder, string $name, array $values, array $stats = null): void | ||
{ | ||
foreach ($this->services as $service) { | ||
if ($service->supports($name, $values, $stats)) { | ||
$service->build($builder, $name, $values, $stats); | ||
|
||
return; | ||
} | ||
} | ||
|
||
throw new \RuntimeException(sprintf('No facet form builder supports the facet with name "%s"', $name)); | ||
} | ||
|
||
public function supports(string $name, array $values, array $stats = null): bool | ||
{ | ||
foreach ($this->services as $service) { | ||
if ($service->supports($name, $values, $stats)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Form\Builder; | ||
|
||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
interface FacetFormBuilderInterface | ||
{ | ||
/** | ||
* @param string $name The name of the facet. This could be 'price' or 'color' for instance | ||
* @param array<string, int> $values The values of the facet. This could be ['red' => 10, 'blue' => 5] where the key is the facet value and the value is the number of matching documents | ||
* @param array{min: int|float, max: int|float}|null $stats The stats of the facet. This could be ['min' => 10, 'max' => 100] where min is the minimum value and max is the maximum value | ||
*/ | ||
public function build(FormBuilderInterface $builder, string $name, array $values, array $stats = null): void; | ||
|
||
/** | ||
* @param string $name The name of the facet. This could be 'price' or 'color' for instance | ||
* @param array<string, int> $values | ||
* @param array{min: int|float, max: int|float}|null $stats | ||
*/ | ||
public function supports(string $name, array $values, array $stats = null): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Form\Builder; | ||
|
||
use Meilisearch\Search\SearchResult; | ||
use Symfony\Component\Form\FormFactoryInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
final class SearchFormBuilder implements SearchFormBuilderInterface | ||
{ | ||
public function __construct(private readonly FormFactoryInterface $formFactory, private readonly FacetFormBuilderInterface $facetFormBuilder) | ||
{ | ||
} | ||
|
||
public function build(SearchResult $searchResult): FormInterface | ||
{ | ||
$builder = $this->formFactory->createNamedBuilder('', options: [ | ||
'csrf_protection' => false, | ||
]); | ||
|
||
/** | ||
* Here is an example of the facet stats array | ||
* | ||
* [ | ||
* "price" => [ | ||
* "min" => 1.24 | ||
* "max" => 47.42 | ||
* ] | ||
* ] | ||
* | ||
* @var array<string, array{min: int|float, max: int|float}> $facetStats | ||
*/ | ||
$facetStats = $searchResult->getFacetStats(); | ||
|
||
/** | ||
* Here is an example of the facet distribution array | ||
* | ||
* [ | ||
* "onSale" => [ | ||
* "false" => 16 | ||
* "true" => 1 | ||
* ] | ||
* "size" => [ | ||
* "L" => 17 | ||
* "M" => 17 | ||
* "S" => 17 | ||
* "XL" => 17 | ||
* "XXL" => 17 | ||
* ] | ||
* ] | ||
* | ||
* @var string $name | ||
* @var array<string, int> $values | ||
*/ | ||
foreach ($searchResult->getFacetDistribution() as $name => $values) { | ||
if ($this->facetFormBuilder->supports($name, $values, $facetStats[$name] ?? null)) { | ||
$this->facetFormBuilder->build($builder, $name, $values, $facetStats[$name] ?? null); | ||
} | ||
} | ||
|
||
$builder->setMethod('GET'); | ||
|
||
return $builder->getForm(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Form\Builder; | ||
|
||
use Meilisearch\Search\SearchResult; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
interface SearchFormBuilderInterface | ||
{ | ||
public function build(SearchResult $searchResult): FormInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Form\Builder; | ||
|
||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use function Symfony\Component\String\u; | ||
|
||
final class ToggleFacetFormBuilder implements FacetFormBuilderInterface | ||
{ | ||
public function build(FormBuilderInterface $builder, string $name, array $values, array $stats = null): void | ||
{ | ||
$builder->add($name, CheckboxType::class, [ | ||
'label' => sprintf('setono_sylius_meilisearch.form.search.facet.%s', u($name)->snake()), | ||
'required' => false, | ||
]); | ||
} | ||
|
||
public function supports(string $name, array $values, array $stats = null): bool | ||
{ | ||
$c = count($values); | ||
|
||
return match ($c) { | ||
1 => isset($values['true']) || isset($values['false']), | ||
2 => isset($values['true'], $values['false']), | ||
default => false, | ||
}; | ||
} | ||
} |
Oops, something went wrong.