Completed
Push — 3.x-dev-kit ( 4fe8f1 )
by
unknown
11:30 queued 08:33
created

NumberFilter::getRenderSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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\DoctrineORMAdminBundle\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 $queryBuilder, $alias, $field, $data)
23
    {
24
        if (!$data || !is_array($data) || !array_key_exists('value', $data) || !is_numeric($data['value'])) {
25
            return;
26
        }
27
28
        $type = isset($data['type']) ? $data['type'] : false;
29
30
        $operator = $this->getOperator($type);
31
32
        if (!$operator) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $operator of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
33
            $operator = '=';
34
        }
35
36
        // c.name > '1' => c.name OPERATOR :FIELDNAME
37
        $parameterName = $this->getNewParameterName($queryBuilder);
38
        $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName));
39
        $queryBuilder->setParameter($parameterName, $data['value']);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getDefaultOptions()
46
    {
47
        return array();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getRenderSettings()
54
    {
55
        return array('sonata_type_filter_number', array(
56
            'field_type' => $this->getFieldType(),
57
            'field_options' => $this->getFieldOptions(),
58
            'label' => $this->getLabel(),
59
        ));
60
    }
61
62
    /**
63
     * @param string $type
64
     *
65
     * @return bool
66
     */
67
    private function getOperator($type)
68
    {
69
        $choices = array(
70
            NumberType::TYPE_EQUAL => '=',
71
            NumberType::TYPE_GREATER_EQUAL => '>=',
72
            NumberType::TYPE_GREATER_THAN => '>',
73
            NumberType::TYPE_LESS_EQUAL => '<=',
74
            NumberType::TYPE_LESS_THAN => '<',
75
        );
76
77
        return isset($choices[$type]) ? $choices[$type] : false;
78
    }
79
}
80