-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StringList filter to search value in
@ORM\Column(type="array")
(#…
…1042) * Use FQCN class in doc * Add StringListFilter
- Loading branch information
1 parent
f5506b1
commit 90e2bbe
Showing
3 changed files
with
263 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Filter; | ||
|
||
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; | ||
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType; | ||
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType; | ||
|
||
final class StringListFilter extends Filter | ||
{ | ||
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void | ||
{ | ||
if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) { | ||
return; | ||
} | ||
|
||
if (!\is_array($data['value'])) { | ||
$data['value'] = [$data['value']]; | ||
} | ||
|
||
$operator = ContainsOperatorType::TYPE_NOT_CONTAINS === $data['type'] ? 'NOT LIKE' : 'LIKE'; | ||
|
||
$andConditions = $queryBuilder->expr()->andX(); | ||
foreach ($data['value'] as $value) { | ||
$parameterName = $this->getNewParameterName($queryBuilder); | ||
$andConditions->add(sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName)); | ||
|
||
$queryBuilder->setParameter($parameterName, '%'.serialize($value).'%'); | ||
} | ||
|
||
if (ContainsOperatorType::TYPE_EQUAL === $data['type']) { | ||
$andConditions->add(sprintf("%s.%s LIKE 'a:%s:%%'", $alias, $field, \count($data['value']))); | ||
} | ||
|
||
$this->applyWhere($queryBuilder, $andConditions); | ||
} | ||
|
||
public function getDefaultOptions(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function getRenderSettings(): array | ||
{ | ||
return [ChoiceType::class, [ | ||
'field_type' => $this->getFieldType(), | ||
'field_options' => $this->getFieldOptions(), | ||
'label' => $this->getLabel(), | ||
]]; | ||
} | ||
} |
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,155 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\DoctrineORMAdminBundle\Tests\Filter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType; | ||
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; | ||
use Sonata\DoctrineORMAdminBundle\Filter\StringListFilter; | ||
|
||
class StringListFilterTest extends TestCase | ||
{ | ||
public function testItStaysDisabledWhenFilteringWithAnEmptyValue(): void | ||
{ | ||
$filter = new StringListFilter(); | ||
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); | ||
|
||
$builder = new ProxyQuery(new QueryBuilder()); | ||
|
||
$filter->filter($builder, 'alias', 'field', null); | ||
$filter->filter($builder, 'alias', 'field', ''); | ||
|
||
$this->assertSame([], $builder->query); | ||
$this->assertFalse($filter->isActive()); | ||
} | ||
|
||
public function testFilteringWithNullReturnsArraysThatContainNull(): void | ||
{ | ||
$filter = new StringListFilter(); | ||
$filter->initialize('field_name'); | ||
|
||
$builder = new ProxyQuery(new QueryBuilder()); | ||
$this->assertSame([], $builder->query); | ||
|
||
$filter->filter($builder, 'alias', 'field', ['value' => null, 'type' => null]); | ||
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); | ||
$this->assertSame(['field_name_0' => '%N;%'], $builder->parameters); | ||
$this->assertTrue($filter->isActive()); | ||
} | ||
|
||
/** | ||
* @dataProvider containsDataProvider | ||
*/ | ||
public function testContains(?int $type): void | ||
{ | ||
$filter = new StringListFilter(); | ||
$filter->initialize('field_name'); | ||
|
||
$builder = new ProxyQuery(new QueryBuilder()); | ||
$this->assertSame([], $builder->query); | ||
|
||
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => $type]); | ||
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query); | ||
$this->assertSame(['field_name_0' => '%s:3:"asd";%'], $builder->parameters); | ||
$this->assertTrue($filter->isActive()); | ||
} | ||
|
||
public function containsDataProvider(): iterable | ||
{ | ||
yield 'explicit contains' => [ContainsOperatorType::TYPE_CONTAINS]; | ||
yield 'implicit contains' => [null]; | ||
} | ||
|
||
public function testNotContains(): void | ||
{ | ||
$filter = new StringListFilter(); | ||
$filter->initialize('field_name'); | ||
|
||
$builder = new ProxyQuery(new QueryBuilder()); | ||
$this->assertSame([], $builder->query); | ||
|
||
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_NOT_CONTAINS]); | ||
$this->assertSame(['alias.field NOT LIKE :field_name_0'], $builder->query); | ||
$this->assertSame(['field_name_0' => '%s:3:"asd";%'], $builder->parameters); | ||
$this->assertTrue($filter->isActive()); | ||
} | ||
|
||
public function testEquals(): void | ||
{ | ||
$filter = new StringListFilter(); | ||
$filter->initialize('field_name'); | ||
|
||
$builder = new ProxyQuery(new QueryBuilder()); | ||
$this->assertSame([], $builder->query); | ||
|
||
$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_EQUAL]); | ||
$this->assertSame(['alias.field LIKE :field_name_0 AND alias.field LIKE \'a:1:%\''], $builder->query); | ||
$this->assertSame(['field_name_0' => '%s:3:"asd";%'], $builder->parameters); | ||
$this->assertTrue($filter->isActive()); | ||
} | ||
|
||
/** | ||
* @param array<string> $value | ||
* @param array<string> $query | ||
* @param array<string, string> $parameters | ||
* | ||
* @dataProvider multipleValuesDataProvider | ||
*/ | ||
public function testMultipleValues(array $value, ?int $type, array $query, array $parameters): void | ||
{ | ||
$filter = new StringListFilter(); | ||
$filter->initialize('field_name'); | ||
|
||
$builder = new ProxyQuery(new QueryBuilder()); | ||
$this->assertSame([], $builder->query); | ||
|
||
$filter->filter($builder, 'alias', 'field', ['value' => $value, 'type' => $type]); | ||
$this->assertSame($query, $builder->query); | ||
$this->assertSame($parameters, $builder->parameters); | ||
$this->assertTrue($filter->isActive()); | ||
} | ||
|
||
public function multipleValuesDataProvider(): iterable | ||
{ | ||
yield 'equal choice' => [ | ||
['asd', 'qwe'], | ||
ContainsOperatorType::TYPE_EQUAL, | ||
["alias.field LIKE :field_name_0 AND alias.field LIKE :field_name_1 AND alias.field LIKE 'a:2:%'"], | ||
[ | ||
'field_name_0' => '%s:3:"asd";%', | ||
'field_name_1' => '%s:3:"qwe";%', | ||
], | ||
]; | ||
|
||
yield 'contains choice' => [ | ||
['asd', 'qwe'], | ||
ContainsOperatorType::TYPE_CONTAINS, | ||
['alias.field LIKE :field_name_0 AND alias.field LIKE :field_name_1'], | ||
[ | ||
'field_name_0' => '%s:3:"asd";%', | ||
'field_name_1' => '%s:3:"qwe";%', | ||
], | ||
]; | ||
|
||
yield 'not contains choice' => [ | ||
['asd', 'qwe'], | ||
ContainsOperatorType::TYPE_NOT_CONTAINS, | ||
['alias.field NOT LIKE :field_name_0 AND alias.field NOT LIKE :field_name_1'], | ||
[ | ||
'field_name_0' => '%s:3:"asd";%', | ||
'field_name_1' => '%s:3:"qwe";%', | ||
], | ||
]; | ||
} | ||
} |