Completed
Push — 2.x ( 359f6e )
by Sullivan
16:25 queued 14:10
created

StringFilter::filter()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
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 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\Form\Type\Filter\ChoiceType;
15
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
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
     * @param string $type
55
     *
56
     * @return bool
57
     */
58
    private function getOperator($type)
59
    {
60
        $choices = array(
61
            ChoiceType::TYPE_CONTAINS         => 'LIKE',
62
            ChoiceType::TYPE_NOT_CONTAINS     => 'NOT LIKE',
63
            ChoiceType::TYPE_EQUAL            => '=',
64
        );
65
66
        return isset($choices[$type]) ? $choices[$type] : false;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getDefaultOptions()
73
    {
74
        return array(
75
            'format'   => '%%%s%%'
76
        );
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getRenderSettings()
83
    {
84
        return array('sonata_type_filter_choice', array(
85
            'field_type'    => $this->getFieldType(),
86
            'field_options' => $this->getFieldOptions(),
87
            'label'         => $this->getLabel()
88
        ));
89
    }
90
}
91