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

StringFilter::filter()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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