Completed
Push — master ( 02db1d...96e327 )
by Daniel
02:23 queued 15s
created

NumberFilter::isApplicable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Query\Comparison;
8
use Psi\Component\ObjectAgent\Query\Expression;
9
use Psi\Component\ObjectAgent\Query\Query;
10
use Symfony\Component\Form\Extension\Core\Type\NumberType;
11
use Symfony\Component\Form\FormBuilderInterface;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14 View Code Duplication
class NumberFilter extends AbstractComparatorFilter
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function buildForm(FormBuilderInterface $builder, array $options)
20
    {
21
        $this->addComparatorChoice($builder, $options);
22
23
        $builder->add('value', NumberType::class, [
24
            'required' => false,
25
        ]);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getExpression(string $fieldName, array $data): Expression
32
    {
33
        $comparator = $data['comparator'] ?: Comparison::EQUALS;
34
35
        return Query::comparison(
36
            $comparator,
37
            $fieldName,
38
            $data['value']
39
        );
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function configureOptions(OptionsResolver $options)
46
    {
47
        $options->setDefault('comparators', $this->getComparatorMap());
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function isApplicable(array $filterData): bool
54
    {
55
        return isset($filterData['value']);
56
    }
57
58
    protected function getComparatorMap(): array
59
    {
60
        $supported = [
61
            Comparison::EQUALS,
62
            Comparison::NOT_EQUALS,
63
            Comparison::GREATER_THAN,
64
            Comparison::GREATER_THAN_EQUAL,
65
            Comparison::LESS_THAN_EQUAL,
66
            Comparison::LESS_THAN,
67
        ];
68
69
        return array_combine($supported, $supported);
70
    }
71
}
72