NumberFilter::filter()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
cc 5
nc 2
nop 4
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\DoctrineMongoDBAdminBundle\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
    private const CHOICES = [
23
        NumberOperatorType::TYPE_EQUAL => 'equals',
24
        NumberOperatorType::TYPE_GREATER_EQUAL => 'gte',
25
        NumberOperatorType::TYPE_GREATER_THAN => 'gt',
26
        NumberOperatorType::TYPE_LESS_EQUAL => 'lte',
27
        NumberOperatorType::TYPE_LESS_THAN => 'lt',
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
38
        $operator = $this->getOperator((int) $type);
39
40
        $queryBuilder->field($field)->$operator((float) $data['value']);
41
        $this->active = true;
42
    }
43
44
    public function getDefaultOptions()
45
    {
46
        return [];
47
    }
48
49
    public function getRenderSettings()
50
    {
51
        return [NumberType::class, [
52
            'field_type' => $this->getFieldType(),
53
            'field_options' => $this->getFieldOptions(),
54
            'label' => $this->getLabel(),
55
        ]];
56
    }
57
58
    private function getOperator(int $type): string
59
    {
60
        return self::CHOICES[$type] ?? self::CHOICES[NumberOperatorType::TYPE_EQUAL];
61
    }
62
}
63