Completed
Push — master ( 1ca8dc...37c80a )
by Daniel
11s
created

ChoiceFilter::getExpression()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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\OptionsResolver\OptionsResolver;
14
15
class ChoiceFilter implements FilterInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function buildForm(FormBuilderInterface $builder, array $options)
21
    {
22
        $builder->add('value', ChoiceType::class, [
23
            'choices' => $options['choices'],
24
            'placeholder' => $options['placeholder'],
25
            'expanded' => $options['expanded'],
26
            'multiple' => $options['multiple'],
27
        ]);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 View Code Duplication
    public function getExpression(string $fieldName, array $data): Expression
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $comparator = is_array($data['value']) ? Comparison::IN : Comparison::EQUALS;
36
37
        return Query::comparison(
38
            $comparator,
39
            $fieldName,
40
            $data['value']
41
        );
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function isApplicable(array $filterData): bool
48
    {
49
        return isset($filterData['value']);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function configureOptions(OptionsResolver $options)
56
    {
57
        $options->setDefault('choices', []);
58
        $options->setDefault('placeholder', null);
59
        $options->setDefault('expanded', false);
60
        $options->setDefault('multiple', false);
61
62
        $options->setAllowedTypes('choices', ['array']);
63
        $options->setAllowedTypes('placeholder', ['string', 'null']);
64
        $options->setAllowedTypes('expanded', ['bool']);
65
        $options->setAllowedTypes('multiple', ['bool']);
66
    }
67
}
68