Completed
Pull Request — 2.x (#556)
by Grégoire
03:14 queued 01:40
created

src/Filter/ChoiceFilter.php (1 issue)

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
18
19
class ChoiceFilter extends Filter
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
25
    {
26
        if (!$data || !\is_array($data) || !\array_key_exists('type', $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...
27
            return;
28
        }
29
30
        $values = (array) $data['value'];
31
        $type = $data['type'];
32
33
        // clean values
34
        foreach ($values as $key => $value) {
35
            $value = trim((string) $value);
36
            if (!$value) {
37
                unset($values[$key]);
38
            } else {
39
                $values[$key] = $value;
40
            }
41
        }
42
43
        // if values not set or "all" specified, do not do this filter
44
        if (!$values || \in_array('all', $values, true)) {
45
            return;
46
        }
47
48
        $andX = $this->getWhere($proxyQuery)->andX();
49
50
        foreach ($values as $value) {
51
            if (ChoiceType::TYPE_NOT_CONTAINS === $type) {
52
                $andX->not()->like()->field('a.'.$field)->literal('%'.$value.'%');
53
            } elseif (ChoiceType::TYPE_CONTAINS === $type) {
54
                $andX->like()->field('a.'.$field)->literal('%'.$value.'%');
55
            } elseif (ChoiceType::TYPE_EQUAL === $type) {
56
                $andX->like()->field('a.'.$field)->literal($value);
57
            }
58
        }
59
60
        // filter is active as we have now modified the query
61
        $this->active = true;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getDefaultOptions()
68
    {
69
        return [];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getRenderSettings()
76
    {
77
        return ['sonata_type_filter_default', [
78
            'operator_type' => 'sonata_type_equal',
79
            'field_type' => $this->getFieldType(),
80
            'field_options' => $this->getFieldOptions(),
81
            'label' => $this->getLabel(),
82
        ]];
83
    }
84
}
85