-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor the Search Engine & Filter Builder #17
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Meilisearch\Builder; | ||
|
||
final class CompositeFilterBuilder implements FilterBuilderInterface | ||
{ | ||
/** | ||
* @param iterable<FilterBuilderInterface> $filterBuilders | ||
*/ | ||
public function __construct( | ||
private readonly iterable $filterBuilders, | ||
) { | ||
} | ||
|
||
public function build(array $facets): string|array | ||
{ | ||
$filters = []; | ||
|
||
foreach ($this->filterBuilders as $filterBuilder) { | ||
if ($filterBuilder->supports($facets)) { | ||
$filters[] = $filterBuilder->build($facets); | ||
} | ||
} | ||
|
||
return $filters; | ||
} | ||
|
||
public function supports(array $facets): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
26 changes: 26 additions & 0 deletions
26
tests/Application/src/FilterBuilder/BrandFilterBuilder.php
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TestApp\FilterBuilder; | ||
|
||
use Setono\SyliusMeilisearchPlugin\Meilisearch\Builder\FilterBuilderInterface; | ||
|
||
final class BrandFilterBuilder implements FilterBuilderInterface | ||
{ | ||
public function build(array $facets): string|array | ||
{ | ||
$brandQuery = []; | ||
/** @var string $size */ | ||
foreach ($facets['brand'] as $size) { | ||
$brandQuery[] = sprintf('brand = "%s"', $size); | ||
} | ||
|
||
return '(' . implode(' OR ', $brandQuery) . ')'; | ||
} | ||
|
||
public function supports(array $facets): bool | ||
{ | ||
return isset($facets['brand']); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/Application/src/FilterBuilder/OnSaleFilterBuilder.php
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Tests\Application\src\FilterBuilder; | ||
|
||
use Setono\SyliusMeilisearchPlugin\Meilisearch\Builder\FilterBuilderInterface; | ||
|
||
final class OnSaleFilterBuilder implements FilterBuilderInterface | ||
{ | ||
public function build(array $facets): string|array | ||
{ | ||
return 'onSale = true'; | ||
} | ||
|
||
public function supports(array $facets): bool | ||
{ | ||
return isset($facets['onSale']); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TestApp\FilterBuilder; | ||
|
||
use Setono\SyliusMeilisearchPlugin\Meilisearch\Builder\FilterBuilderInterface; | ||
|
||
final class SizeFilterBuilder implements FilterBuilderInterface | ||
{ | ||
public function build(array $facets): string|array | ||
{ | ||
$sizeQuery = []; | ||
/** @var string $size */ | ||
foreach ($facets['size'] as $size) { | ||
$sizeQuery[] = sprintf('size = "%s"', $size); | ||
} | ||
|
||
return '(' . implode(' OR ', $sizeQuery) . ')'; | ||
} | ||
|
||
public function supports(array $facets): bool | ||
{ | ||
return isset($facets['size']); | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
tests/Unit/Meilisearch/Builder/CompositeFilterBuilderTest.php
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Tests\Unit\Meilisearch\Builder; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Setono\SyliusMeilisearchPlugin\Meilisearch\Builder\CompositeFilterBuilder; | ||
use Setono\SyliusMeilisearchPlugin\Meilisearch\Builder\FilterBuilderInterface; | ||
|
||
final class CompositeFilterBuilderTest extends TestCase | ||
{ | ||
public function test_it_returns_filters(): void | ||
{ | ||
$brandFilterBuilder = $this->createMock(FilterBuilderInterface::class); | ||
$brandFilterBuilder->method('build')->willReturn('(brand = "brand1")'); | ||
$brandFilterBuilder->method('supports')->willReturn(true); | ||
|
||
$sizeFilterBuilder = $this->createMock(FilterBuilderInterface::class); | ||
$sizeFilterBuilder->method('build')->willReturn('(size = "size1" OR size = "size2")'); | ||
$sizeFilterBuilder->method('supports')->willReturn(true); | ||
|
||
$compositeFilterBuilder = new CompositeFilterBuilder([$brandFilterBuilder, $sizeFilterBuilder]); | ||
|
||
$filters = $compositeFilterBuilder->build([ | ||
'onSale' => true, | ||
'brand' => ['brand1'], | ||
'size' => ['size1', 'size2'], | ||
]); | ||
|
||
$this->assertSame([ | ||
'(brand = "brand1")', | ||
'(size = "size1" OR size = "size2")', | ||
], $filters); | ||
} | ||
|
||
public function test_it_uses_only_supported_filter_builders(): void | ||
{ | ||
$brandFilterBuilder = $this->createMock(FilterBuilderInterface::class); | ||
$brandFilterBuilder->expects($this->never())->method('build'); | ||
$brandFilterBuilder->method('supports')->willReturn(false); | ||
|
||
$sizeFilterBuilder = $this->createMock(FilterBuilderInterface::class); | ||
$sizeFilterBuilder->method('build')->willReturn('(size = "size1" OR size = "size2")'); | ||
$sizeFilterBuilder->method('supports')->willReturn(true); | ||
|
||
$compositeFilterBuilder = new CompositeFilterBuilder([$brandFilterBuilder, $sizeFilterBuilder]); | ||
|
||
$filters = $compositeFilterBuilder->build([ | ||
'onSale' => true, | ||
'size' => ['size1', 'size2'], | ||
]); | ||
|
||
$this->assertSame([ | ||
'(size = "size1" OR size = "size2")', | ||
], $filters); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could potentially unify the return type, to always return array (no matter if with one or multiple elements)