Completed
Push — master ( 6734fa...551366 )
by
unknown
07:45
created

src/Filter/StringFilter.php (1 issue)

Check for implicit conversion of array to boolean.

Best Practice Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DoctrinePHPCRAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
16
17
class StringFilter extends Filter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
23
    {
24
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
25
            return;
26
        }
27
28
        $value = trim($data['value']);
29
        $data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
30
31
        if (0 == strlen($value)) {
32
            return;
33
        }
34
35
        $where = $this->getWhere($proxyQuery);
36
        $isComparisonLowerCase = $this->getOption('compare_case_insensitive');
37
        $value = $isComparisonLowerCase ? strtolower($value) : $value;
38
        switch ($data['type']) {
39
            case ChoiceType::TYPE_EQUAL:
40
                if ($isComparisonLowerCase) {
41
                    $where->eq()->lowerCase()->field('a.'.$field)->end()->literal($value);
42
                } else {
43
                    $where->eq()->field('a.'.$field)->literal($value);
44
                }
45
46
                break;
47
            case ChoiceType::TYPE_NOT_CONTAINS:
48
                $where->fullTextSearch('a.'.$field, '* -'.$value);
49
50
                break;
51
            case ChoiceType::TYPE_CONTAINS:
52
                if ($isComparisonLowerCase) {
53
                    $where->like()->lowerCase()->field('a.'.$field)->end()->literal('%'.$value.'%');
54
                } else {
55
                    $where->like()->field('a.'.$field)->literal('%'.$value.'%');
56
                }
57
58
                break;
59
            case ChoiceType::TYPE_CONTAINS_WORDS:
60
            default:
61
                $where->fullTextSearch('a.'.$field, $value);
62
        }
63
64
        // filter is active as we have now modified the query
65
        $this->active = true;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getDefaultOptions()
72
    {
73
        return [
74
            'format' => '%%%s%%',
75
            'compare_lower_case' => false,
76
        ];
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getRenderSettings()
83
    {
84
        return ['Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType', [
85
            'field_type' => $this->getFieldType(),
86
            'field_options' => $this->getFieldOptions(),
87
            'label' => $this->getLabel(),
88
        ]];
89
    }
90
}
91