Completed
Push — master ( 6734fa...551366 )
by
unknown
07:45
created

src/Filter/NumberFilter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
16
17
class NumberFilter extends Filter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
23
    {
24
        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...
25
            return;
26
        }
27
28
        $type = isset($data['type']) ? $data['type'] : false;
29
        $where = $this->getWhere($proxyQuery);
30
31
        $value = $data['value'];
32
33
        switch ($type) {
34
            case NumberType::TYPE_GREATER_EQUAL:
35
                $where->gte()->field('a.'.$field)->literal($value);
36
37
                break;
38
            case NumberType::TYPE_GREATER_THAN:
39
                $where->gt()->field('a.'.$field)->literal($value);
40
41
                break;
42
            case NumberType::TYPE_LESS_EQUAL:
43
                $where->lte()->field('a.'.$field)->literal($value);
44
45
                break;
46
            case NumberType::TYPE_LESS_THAN:
47
                $where->lt()->field('a.'.$field)->literal($value);
48
49
                break;
50
            case NumberType::TYPE_EQUAL:
51
            default:
52
                $where->eq()->field('a.'.$field)->literal($value);
53
        }
54
55
        // filter is active as we have now modified the query
56
        $this->active = true;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getDefaultOptions()
63
    {
64
        return [];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getRenderSettings()
71
    {
72
        return ['sonata_type_filter_number', [
73
            'field_type' => $this->getFieldType(),
74
            'field_options' => $this->getFieldOptions(),
75
            'label' => $this->getLabel(),
76
        ]];
77
    }
78
}
79