Skip to content

Commit

Permalink
Merge pull request Sylius#7837 from NeverResponse/grid-not-equal-filter
Browse files Browse the repository at this point in the history
[Grid] Added `notEqual` type to StringFilter
  • Loading branch information
pjedrzejewski committed Mar 27, 2017
2 parents a7ce677 + 899d4a5 commit 10cb9f5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/bundles/SyliusGridBundle/filters.rst
Expand Up @@ -30,6 +30,7 @@ The filter allows the user to select following search options:
* contains
* not contains
* equal
* not equal
* starts with
* ends with
* empty
Expand Down
Expand Up @@ -35,6 +35,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'sylius.ui.contains' => StringFilter::TYPE_CONTAINS,
'sylius.ui.not_contains' => StringFilter::TYPE_NOT_CONTAINS,
'sylius.ui.equal' => StringFilter::TYPE_EQUAL,
'sylius.ui.not_equal' => StringFilter::TYPE_NOT_EQUAL,
'sylius.ui.empty' => StringFilter::TYPE_EMPTY,
'sylius.ui.not_empty' => StringFilter::TYPE_NOT_EMPTY,
'sylius.ui.starts_with' => StringFilter::TYPE_STARTS_WITH,
Expand Down Expand Up @@ -65,6 +66,7 @@ public function configureOptions(OptionsResolver $resolver)
StringFilter::TYPE_CONTAINS,
StringFilter::TYPE_NOT_CONTAINS,
StringFilter::TYPE_EQUAL,
StringFilter::TYPE_NOT_EQUAL,
StringFilter::TYPE_EMPTY,
StringFilter::TYPE_NOT_EMPTY,
StringFilter::TYPE_STARTS_WITH,
Expand Down
Expand Up @@ -542,6 +542,7 @@ sylius:
not_available_in_your_country: 'Not available in your country'
not_contains: 'Not contains'
not_empty: 'Not empty'
not_equal: 'Not equal'
not_in: 'Not in'
not_verified: 'Not verified'
not_tracked: 'Not tracked'
Expand Down
9 changes: 9 additions & 0 deletions src/Sylius/Component/Grid/Filter/StringFilter.php
Expand Up @@ -23,6 +23,7 @@ final class StringFilter implements FilterInterface
const NAME = 'string';

const TYPE_EQUAL = 'equal';
const TYPE_NOT_EQUAL = 'not_equal';
const TYPE_EMPTY = 'empty';
const TYPE_NOT_EMPTY = 'not_empty';
const TYPE_CONTAINS = 'contains';
Expand Down Expand Up @@ -67,6 +68,12 @@ public function apply(DataSourceInterface $dataSource, $name, $data, array $opti
$expressions[] = $this->getExpression($expressionBuilder, $type, $field, $value);
}

if (self::TYPE_NOT_EQUAL === $type) {
$dataSource->restrict($expressionBuilder->andX(...$expressions));

return;
}

$dataSource->restrict($expressionBuilder->orX(...$expressions));
}

Expand All @@ -83,6 +90,8 @@ private function getExpression(ExpressionBuilderInterface $expressionBuilder, $t
switch ($type) {
case self::TYPE_EQUAL:
return $expressionBuilder->equals($field, $value);
case self::TYPE_NOT_EQUAL:
return $expressionBuilder->notEquals($field, $value);
case self::TYPE_EMPTY:
return $expressionBuilder->isNull($field);
case self::TYPE_NOT_EMPTY:
Expand Down
13 changes: 13 additions & 0 deletions src/Sylius/Component/Grid/spec/Filter/StringFilterSpec.php
Expand Up @@ -56,6 +56,18 @@ function it_filters_equal_strings(
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_EQUAL, 'value' => 'John'], []);
}

function it_filters_not_equal_strings(
DataSourceInterface $dataSource,
ExpressionBuilderInterface $expressionBuilder
) {
$dataSource->getExpressionBuilder()->willReturn($expressionBuilder);

$expressionBuilder->notEquals('firstName', 'John')->willReturn('EXPR');
$dataSource->restrict('EXPR')->shouldBeCalled();

$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_NOT_EQUAL, 'value' => 'John'], []);
}

function it_filters_data_containing_empty_strings(
DataSourceInterface $dataSource,
ExpressionBuilderInterface $expressionBuilder
Expand Down Expand Up @@ -203,6 +215,7 @@ function it_ignores_filter_if_its_value_is_empty_and_the_filter_depends_on_it(
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_CONTAINS, 'value' => ''], []);
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_ENDS_WITH, 'value' => ''], []);
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_EQUAL, 'value' => ''], []);
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_NOT_EQUAL, 'value' => ''], []);
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_IN, 'value' => ''], []);
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_NOT_CONTAINS, 'value' => ''], []);
$this->apply($dataSource, 'firstName', ['type' => StringFilter::TYPE_NOT_IN, 'value' => ''], []);
Expand Down

0 comments on commit 10cb9f5

Please sign in to comment.