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

BooleanFilter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 10 1
A isApplicable() 0 4 1
A getExpression() 0 8 1
A configureOptions() 0 3 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