Skip to content

Commit

Permalink
Add the "supported_operators" as option to be overridden in Doctrine …
Browse files Browse the repository at this point in the history
…ORM filter types (#169)
  • Loading branch information
alexandre-castelain authored Jan 10, 2025
1 parent 3a8e671 commit 091bd07
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 72 deletions.
13 changes: 4 additions & 9 deletions src/Bridge/Doctrine/Orm/Filter/Type/BooleanFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type;

use Kreyu\Bundle\DataTableBundle\Filter\FilterBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Filter\FilterData;
use Kreyu\Bundle\DataTableBundle\Filter\Operator;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -14,19 +13,15 @@

final class BooleanFilterType extends AbstractDoctrineOrmFilterType
{
public function buildFilter(FilterBuilderInterface $builder, array $options): void
{
$builder->setSupportedOperators([
Operator::Equals,
Operator::NotEquals,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'form_type' => ChoiceType::class,
'supported_operators' => [
Operator::Equals,
Operator::NotEquals,
],
'active_filter_formatter' => function (FilterData $data) {
return new TranslatableMessage($data->getValue() ? 'Yes' : 'No', domain: 'KreyuDataTable');
},
Expand Down
21 changes: 8 additions & 13 deletions src/Bridge/Doctrine/Orm/Filter/Type/DateFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,26 @@
namespace Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type;

use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Formatter\DateActiveFilterFormatter;
use Kreyu\Bundle\DataTableBundle\Filter\FilterBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Filter\Operator;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class DateFilterType extends AbstractDoctrineOrmFilterType
{
public function buildFilter(FilterBuilderInterface $builder, array $options): void
{
$builder->setSupportedOperators([
Operator::Equals,
Operator::NotEquals,
Operator::GreaterThan,
Operator::GreaterThanEquals,
Operator::LessThan,
Operator::LessThanEquals,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'form_type' => DateType::class,
'supported_operators' => [
Operator::Equals,
Operator::NotEquals,
Operator::GreaterThan,
Operator::GreaterThanEquals,
Operator::LessThan,
Operator::LessThanEquals,
],
'active_filter_formatter' => new DateActiveFilterFormatter(),
])
->addNormalizer('form_options', function (Options $options, array $value) {
Expand Down
21 changes: 8 additions & 13 deletions src/Bridge/Doctrine/Orm/Filter/Type/DateTimeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,26 @@
namespace Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type;

use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Formatter\DateTimeActiveFilterFormatter;
use Kreyu\Bundle\DataTableBundle\Filter\FilterBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Filter\Operator;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class DateTimeFilterType extends AbstractDoctrineOrmFilterType
{
public function buildFilter(FilterBuilderInterface $builder, array $options): void
{
$builder->setSupportedOperators([
Operator::Equals,
Operator::NotEquals,
Operator::GreaterThan,
Operator::GreaterThanEquals,
Operator::LessThan,
Operator::LessThanEquals,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'form_type' => DateTimeType::class,
'supported_operators' => [
Operator::Equals,
Operator::NotEquals,
Operator::GreaterThan,
Operator::GreaterThanEquals,
Operator::LessThan,
Operator::LessThanEquals,
],
'active_filter_formatter' => new DateTimeActiveFilterFormatter(),
])
->addNormalizer('form_options', function (Options $options, array $value): array {
Expand Down
17 changes: 6 additions & 11 deletions src/Bridge/Doctrine/Orm/Filter/Type/EntityFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\Persistence\ManagerRegistry;
use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Formatter\EntityActiveFilterFormatter;
use Kreyu\Bundle\DataTableBundle\Filter\FilterBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Filter\Operator;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\Options;
Expand All @@ -19,21 +18,17 @@ public function __construct(
) {
}

public function buildFilter(FilterBuilderInterface $builder, array $options): void
{
$builder->setSupportedOperators([
Operator::Equals,
Operator::NotEquals,
Operator::In,
Operator::NotIn,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'form_type' => EntityType::class,
'supported_operators' => [
Operator::Equals,
Operator::NotEquals,
Operator::In,
Operator::NotIn,
],
'choice_label' => null,
'active_filter_formatter' => new EntityActiveFilterFormatter(),
])
Expand Down
21 changes: 8 additions & 13 deletions src/Bridge/Doctrine/Orm/Filter/Type/NumericFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@

namespace Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type;

use Kreyu\Bundle\DataTableBundle\Filter\FilterBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Filter\Operator;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class NumericFilterType extends AbstractDoctrineOrmFilterType
{
public function buildFilter(FilterBuilderInterface $builder, array $options): void
{
$builder->setSupportedOperators([
Operator::Equals,
Operator::NotEquals,
Operator::Contains,
Operator::NotContains,
Operator::StartsWith,
Operator::EndsWith,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'form_type' => NumberType::class,
'supported_operators' => [
Operator::Equals,
Operator::NotEquals,
Operator::Contains,
Operator::NotContains,
Operator::StartsWith,
Operator::EndsWith,
],
]);
}
}
21 changes: 8 additions & 13 deletions src/Bridge/Doctrine/Orm/Filter/Type/StringFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@

namespace Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type;

use Kreyu\Bundle\DataTableBundle\Filter\FilterBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Filter\Operator;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class StringFilterType extends AbstractDoctrineOrmFilterType
{
public function buildFilter(FilterBuilderInterface $builder, array $options): void
{
$builder->setSupportedOperators([
Operator::Equals,
Operator::NotEquals,
Operator::Contains,
Operator::NotContains,
Operator::StartsWith,
Operator::EndsWith,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'default_operator' => Operator::Contains,
'supported_operators' => [
Operator::Equals,
Operator::NotEquals,
Operator::Contains,
Operator::NotContains,
Operator::StartsWith,
Operator::EndsWith,
],
]);
}
}

0 comments on commit 091bd07

Please sign in to comment.