-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
11 changed files
with
130 additions
and
21 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
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Form\Builder; | ||
|
||
use Setono\SyliusMeilisearchPlugin\Document\Attribute\Sortable as SortableAttribute; | ||
use Setono\SyliusMeilisearchPlugin\Document\Metadata\MetadataInterface; | ||
use Setono\SyliusMeilisearchPlugin\Document\Metadata\Sortable; | ||
use Setono\SyliusMeilisearchPlugin\Engine\SearchRequest; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
final class SortingFormBuilder implements SortingFormBuilderInterface | ||
{ | ||
public function build(FormBuilderInterface $searchFormBuilder, MetadataInterface $metadata): void | ||
{ | ||
$choices = []; | ||
foreach ($metadata->getSortableAttributes() as $sortable) { | ||
foreach (self::resolveDirections($sortable) as $direction) { | ||
$choices[sprintf('setono_sylius_meilisearch.form.search.sorting.%s.%s', $sortable->name, $direction)] = sprintf('%s:%s', $sortable->name, $direction); | ||
} | ||
} | ||
$searchFormBuilder->add(SearchRequest::QUERY_PARAMETER_SORT, ChoiceType::class, [ | ||
'choices' => $choices, | ||
'required' => false, | ||
'placeholder' => 'setono_sylius_meilisearch.form.search.sorting.placeholder', | ||
]); | ||
} | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
private static function resolveDirections(Sortable $sortable): array | ||
{ | ||
if (null === $sortable->direction) { | ||
return [SortableAttribute::ASC, SortableAttribute::DESC]; | ||
} | ||
|
||
return [$sortable->direction]; | ||
} | ||
} |
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 Setono\SyliusMeilisearchPlugin\Document\Metadata\MetadataInterface; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
interface SortingFormBuilderInterface | ||
{ | ||
public function build(FormBuilderInterface $searchFormBuilder, MetadataInterface $metadata): void; | ||
} |
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 |
---|---|---|
|
@@ -89,9 +89,5 @@ | |
margin: 0 !important; | ||
text-align-last: right; | ||
} | ||
.ssm-sort option { | ||
direction: rtl; | ||
} | ||
</style> | ||
{% endblock %} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Tests\Unit\Form\Builder; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Setono\SyliusMeilisearchPlugin\Document\Attribute\Sortable as SortableAttribute; | ||
use Setono\SyliusMeilisearchPlugin\Document\Metadata\MetadataInterface; | ||
use Setono\SyliusMeilisearchPlugin\Document\Metadata\Sortable; | ||
use Setono\SyliusMeilisearchPlugin\Engine\SearchRequest; | ||
use Setono\SyliusMeilisearchPlugin\Form\Builder\SortingFormBuilder; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
final class SortingFormBuilderTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_builds(): void | ||
{ | ||
$searchFormBuilder = $this->prophesize(FormBuilderInterface::class); | ||
$searchFormBuilder->add(SearchRequest::QUERY_PARAMETER_SORT, ChoiceType::class, [ | ||
'choices' => [ | ||
'setono_sylius_meilisearch.form.search.sorting.name.asc' => 'name:asc', | ||
'setono_sylius_meilisearch.form.search.sorting.name.desc' => 'name:desc', | ||
'setono_sylius_meilisearch.form.search.sorting.price.desc' => 'price:desc', | ||
], | ||
'required' => false, | ||
'placeholder' => 'setono_sylius_meilisearch.form.search.sorting.placeholder', | ||
])->willReturn($searchFormBuilder)->shouldBeCalledOnce(); | ||
|
||
$metadata = $this->prophesize(MetadataInterface::class); | ||
$metadata->getSortableAttributes()->willReturn([ | ||
new Sortable('name'), | ||
new Sortable('price', SortableAttribute::DESC), | ||
]); | ||
|
||
$builder = new SortingFormBuilder(); | ||
$builder->build($searchFormBuilder->reveal(), $metadata->reveal()); | ||
} | ||
} |