Completed
Pull Request — master (#5)
by Daniel
01:51
created

BooleanFilter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 4 1
A getExpression() 0 8 1
A configureOptions() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Filter;
6
7
use Psi\Component\Grid\FilterDataInterface;
8
use Psi\Component\Grid\FilterInterface;
9
use Psi\Component\ObjectAgent\Query\Comparison;
10
use Psi\Component\ObjectAgent\Query\Expression;
11
use Psi\Component\ObjectAgent\Query\Query;
12
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
13
use Symfony\Component\Form\FormBuilderInterface;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
class BooleanFilter implements FilterInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function buildForm(FormBuilderInterface $builder, array $options)
23
    {
24
        $builder->add('checkbox', CheckboxType::class);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getExpression(string $fieldName, FilterDataInterface $data): Expression
31
    {
32
        return Query::comparison(
33
            Comparison::EQUALS,
34
            $fieldName,
35
            $data->getValue()
36
        );
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function configureOptions(OptionsResolver $options)
43
    {
44
        $options->setDefault('data_class', StringFilterData::class);
45
        $options->setDefault('empty_data', function (FormInterface $form) {
46
            return new BooleanFilterData(
47
                $form->get('value')->getData()
48
            );
49
        });
50
    }
51
}
52