ChoiceFilter::getDefaultOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
18
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
19
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType;
20
21
class ChoiceFilter extends Filter
22
{
23
    /**
24
     * @param string $alias
25
     * @param string $field
26
     * @param mixed  $data
27
     */
28
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
29
    {
30
        if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) {
31
            return;
32
        }
33
34
        if (\is_array($data['value'])) {
35
            if (0 === \count($data['value'])) {
36
                return;
37
            }
38
39
            if (ContainsOperatorType::TYPE_NOT_CONTAINS === $data['type']) {
40
                $queryBuilder->field($field)->notIn($data['value']);
41
            } else {
42
                $queryBuilder->field($field)->in($data['value']);
43
            }
44
45
            $this->active = true;
46
        } else {
47
            if ('' === $data['value'] || null === $data['value'] || false === $data['value']) {
48
                return;
49
            }
50
51
            if (ContainsOperatorType::TYPE_NOT_CONTAINS === $data['type']) {
52
                $queryBuilder->field($field)->notEqual($data['value']);
53
            } else {
54
                $queryBuilder->field($field)->equals($data['value']);
55
            }
56
57
            $this->active = true;
58
        }
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getDefaultOptions()
65
    {
66
        return [];
67
    }
68
69
    public function getRenderSettings()
70
    {
71
        return [DefaultType::class, [
72
            'operator_type' => ChoiceType::class,
73
            'field_type' => $this->getFieldType(),
74
            'field_options' => $this->getFieldOptions(),
75
            'label' => $this->getLabel(),
76
        ]];
77
    }
78
}
79