Completed
Pull Request — master (#68)
by Daniel
02:11
created

BooleanFilter::isApplicable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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