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