Completed
Pull Request — master (#738)
by Grégoire
13:17
created

BooleanFilter::getRenderSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 17.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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\DoctrineORMAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\CoreBundle\Form\Type\BooleanType;
16
17
class BooleanFilter extends Filter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
23
    {
24
        if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
25
            return;
26
        }
27
28
        if (is_array($data['value'])) {
29
            $values = [];
30
            foreach ($data['value'] as $v) {
31
                if (!in_array($v, [BooleanType::TYPE_NO, BooleanType::TYPE_YES])) {
32
                    continue;
33
                }
34
35
                $values[] = ($v == BooleanType::TYPE_YES) ? 1 : 0;
36
            }
37
38
            if (count($values) == 0) {
39
                return;
40
            }
41
42
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field), $values));
43
        } else {
44
            if (!in_array($data['value'], [BooleanType::TYPE_NO, BooleanType::TYPE_YES])) {
45
                return;
46
            }
47
48
            $parameterName = $this->getNewParameterName($queryBuilder);
49
            $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName));
50
            $queryBuilder->setParameter($parameterName, ($data['value'] == BooleanType::TYPE_YES) ? 1 : 0);
51
        }
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getDefaultOptions()
58
    {
59
        return ['field_type' => 'Sonata\CoreBundle\Form\Type\BooleanType'];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getRenderSettings()
66
    {
67
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL
Loading history...
68
        return ['Sonata\AdminBundle\Form\Type\Filter\DefaultType', [
69
            'field_type' => $this->getFieldType(),
70
            'field_options' => $this->getFieldOptions(),
71
            'operator_type' => 'Symfony\Component\Form\Extension\Core\Type\HiddenType',
72
=======
73
        // NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
74
        $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
75
            ? 'Sonata\AdminBundle\Form\Type\Filter\DefaultType'
76
            : 'sonata_type_filter_default';
77
78
        return [$type, [
79
            'field_type' => $this->getFieldType(),
80
            'field_options' => $this->getFieldOptions(),
81
            'operator_type' => method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
82
                ? 'Symfony\Component\Form\Extension\Core\Type\HiddenType'
83
                : 'hidden', // NEXT_MAJOR: Remove ternary (when requirement of Symfony is >= 2.8)
84
>>>>>>> origin/3.x
85
            'operator_options' => [],
86
            'label' => $this->getLabel(),
87
        ]];
88
    }
89
}
90