Completed
Push — 3.x-dev-kit ( 4fe8f1 )
by
unknown
11:30 queued 08:33
created

BooleanFilter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 14
c 3
b 2
f 0
lcom 1
cbo 1
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C filter() 0 31 12
A getDefaultOptions() 0 6 1
A getRenderSettings() 0 10 1
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\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 = array();
30
            foreach ($data['value'] as $v) {
31
                if (!in_array($v, array(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'], array(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 array(
60
            'field_type' => 'sonata_type_boolean',
61
        );
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getRenderSettings()
68
    {
69
        return array('sonata_type_filter_default', array(
70
            'field_type' => $this->getFieldType(),
71
            'field_options' => $this->getFieldOptions(),
72
            'operator_type' => 'hidden',
73
            'operator_options' => array(),
74
            'label' => $this->getLabel(),
75
        ));
76
    }
77
}
78