Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

ChoiceFilter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C filter() 0 38 14
A getDefaultOptions() 0 4 1
A getRenderSettings() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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\Form\Type\Filter\ChoiceType;
15
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
16
17
class ChoiceFilter 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
            if (count($data['value']) == 0) {
30
                return;
31
            }
32
33
            if (in_array('all', $data['value'], true)) {
34
                return;
35
            }
36
37
            if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
38
                $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn(sprintf('%s.%s', $alias, $field ), $data['value']));
39
            } else {
40
                $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field ), $data['value']));
41
            }
42
43
        } else {
44
45
            if ($data['value'] === '' || $data['value'] === null || $data['value'] === false || $data['value'] === 'all') {
46
                return;
47
            }
48
49
            $parameterName = $this->getNewParameterName($queryBuilder);
50
51
            if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
52
                $this->applyWhere($queryBuilder, sprintf('%s.%s <> :%s', $alias, $field, $parameterName));
53
            } else {
54
                $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName));
55
            }
56
57
            $queryBuilder->setParameter($parameterName, $data['value']);
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getDefaultOptions()
65
    {
66
        return array();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getRenderSettings()
73
    {
74
        return array('sonata_type_filter_default', array(
75
            'operator_type' => 'sonata_type_equal',
76
            'field_type'    => $this->getFieldType(),
77
            'field_options' => $this->getFieldOptions(),
78
            'label'         => $this->getLabel()
79
        ));
80
    }
81
}
82