Completed
Pull Request — master (#5)
by Daniel
08:11 queued 06:07
created

BooleanFilter::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid\Filter;
6
7
use Psi\Component\ObjectAgent\Capabilities;
8
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
9
use Symfony\Component\Form\Extension\Core\Type\TextType;
10
use Symfony\Component\Form\Extension\Core\Type\FormType;
11
use Symfony\Component\Form\FormBuilderInterface;
12
use Psi\Component\Grid\Metadata\GridMetadata;
13
use Psi\Component\ObjectAgent\Query\Comparison;
14
use Psi\Component\ObjectAgent\Query\Query;
15
use Psi\Component\Grid\FilterInterface;
16
use Psi\Component\ObjectAgent\Query\Expression;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\Form\AbstractType;
20
use Symfony\Component\Validator\Constraints\NotNull;
21
use Psi\Component\Grid\FilterDataInterface;
22
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
23
24
class BooleanFilter implements FilterInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function buildForm(FormBuilderInterface $builder, array $options)
30
    {
31
        $builder->add('checkbox', CheckboxType::class);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getExpression(string $fieldName, FilterDataInterface $data): Expression
38
    {
39
        return Query::comparison(
40
            Comparison::EQUALS,
41
            $fieldName,
42
            $data->getValue()
43
        );
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function configureOptions(OptionsResolver $options)
50
    {
51
        $options->setDefault('data_class', StringFilterData::class);
52
        $options->setDefault('empty_data', function (FormInterface $form) {
53
            return new BooleanFilterData(
54
                $form->get('value')->getData()
55
            );
56
        });
57
    }
58
}
59