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

src/Filter/BooleanFilter.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
/*
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\CoreBundle\Form\Type\BooleanType;
16
use Sonata\DoctrinePHPCRAdminBundle\Filter\Filter as BaseFilter;
17
18
class BooleanFilter extends BaseFilter
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
24
    {
25
        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...
26
            return;
27
        }
28
29
        if (is_array($data['value']) || !in_array($data['value'], [BooleanType::TYPE_NO, BooleanType::TYPE_YES])) {
30
            return;
31
        }
32
33
        $where = $this->getWhere($proxyQuery);
34
        $where->eq()->field('a.'.$field)->literal(BooleanType::TYPE_YES == $data['value'] ? true : false);
35
36
        // filter is active as we have now modified the query
37
        $this->active = true;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getDefaultOptions()
44
    {
45
        return [];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getRenderSettings()
52
    {
53
        return ['sonata_type_filter_default', [
54
            'field_type' => $this->getFieldType(),
55
            'field_options' => $this->getFieldOptions(),
56
            'operator_type' => 'hidden',
57
            'operator_options' => [],
58
            'label' => $this->getLabel(),
59
        ]];
60
    }
61
}
62