Completed
Pull Request — 3.x (#703)
by
unknown
01:53
created

StringFilter::filter()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 5.3846
cc 8
eloc 16
nc 10
nop 4
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\ChoiceType;
16
17
class StringFilter 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)) {
25
            return;
26
        }
27
28
        $data['value'] = trim($data['value']);
29
30
        if (strlen($data['value']) == 0) {
31
            return;
32
        }
33
34
        $data['type'] = !isset($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
35
36
        $operator = $this->getOperator((int) $data['type']);
37
38
        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...
39
            $operator = 'LIKE';
40
        }
41
42
        // c.name > '1' => c.name OPERATOR :FIELDNAME
43
        $parameterName = $this->getNewParameterName($queryBuilder);
44
        $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName));
45
46
        if ($data['type'] == ChoiceType::TYPE_EQUAL) {
47
            $queryBuilder->setParameter($parameterName, $data['value']);
48
        } else {
49
            $queryBuilder->setParameter($parameterName, sprintf($this->getOption('format'), $data['value']));
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getDefaultOptions()
57
    {
58
        return array(
59
            'format' => '%%%s%%',
60
        );
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getRenderSettings()
67
    {
68
        return array('Sonata\AdminBundle\Form\Type\Filter\ChoiceType', array(
69
            'field_type' => $this->getFieldType(),
70
            'field_options' => $this->getFieldOptions(),
71
            'label' => $this->getLabel(),
72
        ));
73
    }
74
75
    /**
76
     * @param string $type
77
     *
78
     * @return bool
79
     */
80
    private function getOperator($type)
81
    {
82
        $choices = array(
83
            ChoiceType::TYPE_CONTAINS => 'LIKE',
84
            ChoiceType::TYPE_NOT_CONTAINS => 'NOT LIKE',
85
            ChoiceType::TYPE_EQUAL => '=',
86
        );
87
88
        return isset($choices[$type]) ? $choices[$type] : false;
89
    }
90
}
91