Completed
Pull Request — 3.x (#979)
by Christian
01:50
created

EmptyFieldFilter::filter()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 4
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\DoctrineORMAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
18
use Sonata\Form\Type\BooleanType;
19
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
20
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
21
22
final class EmptyFieldFilter extends Filter
23
{
24
    /**
25
     * @param string       $alias
26
     * @param string       $field
27
     * @param mixed[]|null $data
28
     */
29
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
30
    {
31
        if (null === $data || !\is_array($data) || !\array_key_exists('value', $data)) {
32
            return;
33
        }
34
35
        if (BooleanType::TYPE_NO === (int) $data['value']) {
36
            $queryBuilder->andWhere(sprintf('%s.%s IS null', $alias, $field));
37
        } elseif (BooleanType::TYPE_YES === (int) $data['value']) {
38
            $queryBuilder->andWhere(sprintf('%s.%s IS NOT null', $alias, $field));
39
        }
40
41
        $this->active = null !== $data['value'];
42
    }
43
44
    public function getDefaultOptions()
45
    {
46
        return [
47
            'field_type' => BooleanType::class,
48
            'operator_type' => HiddenType::class,
49
            'operator_options' => [],
50
        ];
51
    }
52
53
    public function getFieldType()
54
    {
55
        return $this->getOption('field_type', ChoiceType::class);
56
    }
57
58
    public function getRenderSettings()
59
    {
60
        return [DefaultType::class, [
61
            'field_type' => $this->getFieldType(),
62
            'field_options' => $this->getFieldOptions(),
63
            'operator_type' => $this->getOption('operator_type'),
64
            'operator_options' => $this->getOption('operator_options'),
65
            'label' => $this->getLabel(),
66
        ]];
67
    }
68
69
    public function getParentAssociationMappings()
70
    {
71
        $mappings = $this->getOption('parent_association_mappings', []);
72
73
        $fields = explode('.', $this->getFieldName());
74
        for ($i = 0; $i < \count($fields) - 1; ++$i) {
75
            $mappings[] = ['fieldName' => $fields[$i]];
76
        }
77
78
        return $mappings;
79
    }
80
81
    /**
82
     * @param mixed|null $data
83
     *
84
     * @return string[]
85
     */
86
    protected function association(ProxyQueryInterface $queryBuilder, $data): array
87
    {
88
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
89
        $part = strrchr('.'.$this->getFieldName(), '.');
90
        $fieldName = substr(false === $part ? $this->getFieldType() : $part, 1);
91
92
        return [$alias, $fieldName];
93
    }
94
}
95