NumberFilter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 14 5
A getDefaultOptions() 0 4 1
A getRenderSettings() 0 8 1
A getOperator() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineORMAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
18
use Sonata\AdminBundle\Form\Type\Operator\NumberOperatorType;
19
20
class NumberFilter extends Filter
21
{
22
    public const CHOICES = [
23
        NumberOperatorType::TYPE_EQUAL => '=',
24
        NumberOperatorType::TYPE_GREATER_EQUAL => '>=',
25
        NumberOperatorType::TYPE_GREATER_THAN => '>',
26
        NumberOperatorType::TYPE_LESS_EQUAL => '<=',
27
        NumberOperatorType::TYPE_LESS_THAN => '<',
28
    ];
29
30
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
31
    {
32
        if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || !is_numeric($data['value'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            return;
34
        }
35
36
        $type = $data['type'] ?? NumberOperatorType::TYPE_EQUAL;
37
        $operator = $this->getOperator((int) $type);
38
39
        // c.name > '1' => c.name OPERATOR :FIELDNAME
40
        $parameterName = $this->getNewParameterName($queryBuilder);
41
        $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName));
42
        $queryBuilder->setParameter($parameterName, $data['value']);
43
    }
44
45
    public function getDefaultOptions()
46
    {
47
        return [];
48
    }
49
50
    public function getRenderSettings()
51
    {
52
        return [NumberType::class, [
53
            'field_type' => $this->getFieldType(),
54
            'field_options' => $this->getFieldOptions(),
55
            'label' => $this->getLabel(),
56
        ]];
57
    }
58
59
    private function getOperator(int $type): string
60
    {
61
        return self::CHOICES[$type] ?? self::CHOICES[NumberOperatorType::TYPE_EQUAL];
62
    }
63
}
64