-
-
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.
* create CountFilter (#1280) * 3.28 (#1284) * DevKit updates (#1286) * Close API (#1292) * fix CountFilter * fix CountFilter after merge * fix CountFilterTest after merge Co-authored-by: rgrassian <[email protected]> Co-authored-by: Vincent Langlet <[email protected]> Co-authored-by: Sonata CI <[email protected]>
- Loading branch information
1 parent
35aad95
commit e440690
Showing
9 changed files
with
191 additions
and
16 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
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* 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\Form\Type\Filter\NumberType; | ||
use Sonata\AdminBundle\Form\Type\Operator\NumberOperatorType; | ||
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQueryInterface; | ||
use Symfony\Component\Form\Extension\Core\Type\NumberType as FormNumberType; | ||
|
||
final class CountFilter extends Filter | ||
{ | ||
public const CHOICES = [ | ||
NumberOperatorType::TYPE_EQUAL => '=', | ||
NumberOperatorType::TYPE_GREATER_EQUAL => '>=', | ||
NumberOperatorType::TYPE_GREATER_THAN => '>', | ||
NumberOperatorType::TYPE_LESS_EQUAL => '<=', | ||
NumberOperatorType::TYPE_LESS_THAN => '<', | ||
]; | ||
|
||
public function filter(ProxyQueryInterface $query, string $alias, string $field, array $data): void | ||
{ | ||
if (!\array_key_exists('value', $data) || !is_numeric($data['value'])) { | ||
return; | ||
} | ||
|
||
$type = $data['type'] ?? NumberOperatorType::TYPE_EQUAL; | ||
$operator = $this->getOperator((int) $type); | ||
|
||
// c.name > '1' => c.name OPERATOR :FIELDNAME | ||
$parameterName = $this->getNewParameterName($query); | ||
$rootAlias = current($query->getQueryBuilder()->getRootAliases()); | ||
$query->getQueryBuilder()->addGroupBy($rootAlias); | ||
$this->applyWhere($query, sprintf('COUNT(%s.%s) %s :%s', $alias, $field, $operator, $parameterName)); | ||
$query->getQueryBuilder()->setParameter($parameterName, $data['value']); | ||
} | ||
|
||
public function getDefaultOptions(): array | ||
{ | ||
return [ | ||
'field_type' => FormNumberType::class, | ||
]; | ||
} | ||
|
||
public function getRenderSettings(): array | ||
{ | ||
return [NumberType::class, [ | ||
'field_type' => $this->getFieldType(), | ||
'field_options' => $this->getFieldOptions(), | ||
'label' => $this->getLabel(), | ||
]]; | ||
} | ||
|
||
private function getOperator(int $type): string | ||
{ | ||
return self::CHOICES[$type] ?? self::CHOICES[NumberOperatorType::TYPE_EQUAL]; | ||
} | ||
} |
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* 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 Sonata\AdminBundle\Form\Type\Operator\NumberOperatorType; | ||
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; | ||
use Sonata\DoctrineORMAdminBundle\Filter\CountFilter; | ||
|
||
class CountFilterTest extends FilterTestCase | ||
{ | ||
public function testFilterEmpty(): void | ||
{ | ||
$filter = new CountFilter(); | ||
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); | ||
|
||
$builder = new ProxyQuery($this->createQueryBuilderStub()); | ||
|
||
$filter->filter($builder, 'alias', 'field', []); | ||
|
||
$this->assertSame([], $builder->query); | ||
$this->assertFalse($filter->isActive()); | ||
} | ||
|
||
public function testFilterInvalidOperator(): void | ||
{ | ||
$filter = new CountFilter(); | ||
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); | ||
|
||
$builder = new ProxyQuery($this->createQueryBuilderStub()); | ||
|
||
$filter->filter($builder, 'alias', 'field', ['type' => 'foo']); | ||
|
||
$this->assertSame([], $builder->query); | ||
$this->assertFalse($filter->isActive()); | ||
} | ||
|
||
/** | ||
* @dataProvider filterDataProvider | ||
*/ | ||
public function testFilter(string $expected, ?int $type): void | ||
{ | ||
$filter = new CountFilter(); | ||
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]); | ||
|
||
$builder = new ProxyQuery($this->createQueryBuilderStub()); | ||
|
||
$filter->filter($builder, 'alias', 'field', ['type' => $type, 'value' => 42]); | ||
|
||
$this->assertSame(['GROUP BY o', $expected], $builder->query); | ||
$this->assertTrue($filter->isActive()); | ||
} | ||
|
||
public function filterDataProvider(): array | ||
{ | ||
return [ | ||
['COUNT(alias.field) = :field_name_0', NumberOperatorType::TYPE_EQUAL], | ||
['COUNT(alias.field) >= :field_name_0', NumberOperatorType::TYPE_GREATER_EQUAL], | ||
['COUNT(alias.field) > :field_name_0', NumberOperatorType::TYPE_GREATER_THAN], | ||
['COUNT(alias.field) <= :field_name_0', NumberOperatorType::TYPE_LESS_EQUAL], | ||
['COUNT(alias.field) < :field_name_0', NumberOperatorType::TYPE_LESS_THAN], | ||
['COUNT(alias.field) = :field_name_0', null], | ||
]; | ||
} | ||
} |
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