BooleanFilter::getRenderSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\DoctrineORMAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
18
use Sonata\Form\Type\BooleanType;
19
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
20
21
class BooleanFilter extends Filter
22
{
23
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
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'])) {
30
            $values = [];
31
            foreach ($data['value'] as $v) {
32
                if (!\in_array($v, [BooleanType::TYPE_NO, BooleanType::TYPE_YES], true)) {
33
                    continue;
34
                }
35
36
                $values[] = (BooleanType::TYPE_YES === $v) ? 1 : 0;
37
            }
38
39
            if (0 === \count($values)) {
40
                return;
41
            }
42
43
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field), $values));
44
        } else {
45
            if (!\in_array($data['value'], [BooleanType::TYPE_NO, BooleanType::TYPE_YES], true)) {
46
                return;
47
            }
48
49
            $parameterName = $this->getNewParameterName($queryBuilder);
50
            $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName));
51
            $queryBuilder->setParameter($parameterName, (BooleanType::TYPE_YES === $data['value']) ? 1 : 0);
52
        }
53
    }
54
55
    public function getDefaultOptions()
56
    {
57
        return [
58
            'field_type' => BooleanType::class,
59
            'operator_type' => HiddenType::class,
60
            'operator_options' => [],
61
        ];
62
    }
63
64
    public function getRenderSettings()
65
    {
66
        return [DefaultType::class, [
67
            'field_type' => $this->getFieldType(),
68
            'field_options' => $this->getFieldOptions(),
69
            'operator_type' => $this->getOption('operator_type'),
70
            'operator_options' => $this->getOption('operator_options'),
71
            'label' => $this->getLabel(),
72
        ]];
73
    }
74
}
75