Completed
Pull Request — master (#5)
by Daniel
01:51
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\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