Completed
Pull Request — master (#649)
by
unknown
02:17
created

StringFilter::filter()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
rs 4.909
cc 9
eloc 19
nc 18
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
45
        $whereCondition = sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName);
46
47
        if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
48
            $whereCondition .= sprintf(' OR %s.%s IS NULL', $alias, $field);
49
        }
50
        
51
        $this->applyWhere($queryBuilder, $whereCondition);
52
        
53
        if ($data['type'] == ChoiceType::TYPE_EQUAL) {
54
            $queryBuilder->setParameter($parameterName, $data['value']);
55
        } else {
56
            $queryBuilder->setParameter($parameterName, sprintf($this->getOption('format'), $data['value']));
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getDefaultOptions()
64
    {
65
        return array(
66
            'format' => '%%%s%%',
67
        );
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getRenderSettings()
74
    {
75
        // NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
76
        $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
77
            ? 'Sonata\AdminBundle\Form\Type\Filter\ChoiceType'
78
            : 'sonata_type_filter_choice';
79
80
        return array($type, array(
81
            'field_type' => $this->getFieldType(),
82
            'field_options' => $this->getFieldOptions(),
83
            'label' => $this->getLabel(),
84
        ));
85
    }
86
87
    /**
88
     * @param string $type
89
     *
90
     * @return bool
91
     */
92
    private function getOperator($type)
93
    {
94
        $choices = array(
95
            ChoiceType::TYPE_CONTAINS => 'LIKE',
96
            ChoiceType::TYPE_NOT_CONTAINS => 'NOT LIKE',
97
            ChoiceType::TYPE_EQUAL => '=',
98
        );
99
100
        return isset($choices[$type]) ? $choices[$type] : false;
101
    }
102
}
103