Completed
Push — 3.x ( 839353...0c5d0b )
by Grégoire
02:14
created

BooleanFilter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
C filter() 0 33 12
A getDefaultOptions() 0 4 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\DoctrineMongoDBAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\CoreBundle\Form\Type\BooleanType;
16
17
class BooleanFilter extends Filter
18
{
19
    /**
20
     * @param ProxyQueryInterface $queryBuilder
21
     * @param string              $alias
22
     * @param string              $field
23
     * @param mixed               $data
24
     *
25
     * @return
26
     */
27
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
28
    {
29
        if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
30
            return;
31
        }
32
33
        if (is_array($data['value'])) {
34
            $values = array();
35
            foreach ($data['value'] as $v) {
36
                if (!in_array($v, array(BooleanType::TYPE_NO, BooleanType::TYPE_YES))) {
37
                    continue;
38
                }
39
40
                $values[] = ($v == BooleanType::TYPE_YES) ? true : false;
41
            }
42
43
            if (count($values) == 0) {
44
                return;
45
            }
46
47
            $queryBuilder->field($field)->in($values);
48
            $this->active = true;
49
        } else {
50
            if (!in_array($data['value'], array(BooleanType::TYPE_NO, BooleanType::TYPE_YES))) {
51
                return;
52
            }
53
54
            $value = BooleanType::TYPE_YES == $data['value'] ? true : false;
55
56
            $queryBuilder->field($field)->equals($value);
57
            $this->active = true;
58
        }
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getDefaultOptions()
65
    {
66
        return array();
67
    }
68
69
    public function getRenderSettings()
70
    {
71
        return array('sonata_type_filter_default', array(
72
            'field_type' => $this->getFieldType(),
73
            'field_options' => $this->getFieldOptions(),
74
            'operator_type' => 'hidden',
75
            'operator_options' => array(),
76
            'label' => $this->getLabel(),
77
        ));
78
    }
79
}
80