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

ClassFilter::filter()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 8.2222
cc 7
eloc 10
nc 6
nop 4
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\CoreBundle\Form\Type\EqualType;
15
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
18
class ClassFilter extends Filter
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
24
    {
25
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
26
            return;
27
        }
28
29
        if (strlen($data['value']) == 0) {
30
            return;
31
        }
32
33
        $data['type'] = !isset($data['type']) ?  EqualType::TYPE_IS_EQUAL : $data['type'];
34
35
        $operator = $this->getOperator((int) $data['type']);
36
37
        if (!$operator) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $operator of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
38
            $operator = 'INSTANCE OF';
39
        }
40
41
        $this->applyWhere($queryBuilder, sprintf('%s %s %s', $alias, $operator, $data['value']));
42
    }
43
44
    /**
45
     * @param int $type
46
     *
47
     * @return mixed
48
     */
49
    private function getOperator($type)
50
    {
51
        $choices = array(
52
            EqualType::TYPE_IS_EQUAL     => 'INSTANCE OF',
53
            EqualType::TYPE_IS_NOT_EQUAL => 'NOT INSTANCE OF',
54
        );
55
56
        return isset($choices[$type]) ? $choices[$type] : false;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getDefaultOptions()
63
    {
64
        return array();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getFieldType()
71
    {
72
        return $this->getOption('field_type', 'choice');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getFieldOptions()
79
    {
80
        return $this->getOption('choices', array(
81
            'required' => false,
82
            'choice_list'  => new ChoiceList(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Form\E...e\ChoiceList\ChoiceList has been deprecated with message: since version 2.7, to be removed in 3.0. Use {@link \Symfony\Component\Form\ChoiceList\ArrayChoiceList} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
83
                \array_values($this->getOption('sub_classes')),
84
                \array_keys($this->getOption('sub_classes'))
85
            ),
86
        ));
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getRenderSettings()
93
    {
94
        return array('sonata_type_filter_default', array(
95
            'operator_type' => 'sonata_type_equal',
96
            'field_type'    => $this->getFieldType(),
97
            'field_options' => $this->getFieldOptions(),
98
            'label'         => $this->getLabel()
99
        ));
100
    }
101
}
102