Completed
Push — master ( 02db1d...96e327 )
by Daniel
02:23 queued 15s
created

BooleanFilter::getExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Filter;
6
7
use Psi\Component\Grid\FilterInterface;
8
use Psi\Component\ObjectAgent\Query\Comparison;
9
use Psi\Component\ObjectAgent\Query\Expression;
10
use Psi\Component\ObjectAgent\Query\Query;
11
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
12
use Symfony\Component\Form\FormBuilderInterface;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
class BooleanFilter implements FilterInterface
16
{
17
    const CHOICE_ANY = 'any';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function buildForm(FormBuilderInterface $builder, array $options)
23
    {
24
        $builder->add('value', ChoiceType::class, [
25
            'choices' => [
26
                self::CHOICE_ANY => 'any',
27
                1 => 'yes',
28
                0 => 'no',
29
            ],
30
        ]);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function isApplicable(array $filterData): bool
37
    {
38
        return self::CHOICE_ANY !== $filterData['value'];
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getExpression(string $fieldName, array $data): Expression
45
    {
46
        return Query::comparison(
47
            Comparison::EQUALS,
48
            $fieldName,
49
            $data['value']
50
        );
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function configureOptions(OptionsResolver $options)
57
    {
58
    }
59
}
60