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

EmptyFieldFilter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 66
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 12 6
A getDefaultOptions() 0 8 1
A getRenderSettings() 0 10 1
A getParentAssociationMappings() 0 11 2
A association() 0 8 2
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\HiddenType;
20
21
final class EmptyFieldFilter extends Filter
22
{
23
    /**
24
     * @param string       $alias
25
     * @param string       $field
26
     * @param mixed[]|null $data
27
     */
28
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
29
    {
30
        if (null === $data || !\is_array($data) || !\array_key_exists('value', $data)) {
31
            return;
32
        }
33
34
        if (BooleanType::TYPE_NO === (int) $data['value']) {
35
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s IS NULL', $alias, $field)));
36
        } elseif (BooleanType::TYPE_YES === (int) $data['value']) {
37
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in(sprintf('%s.%s IS NOT NULL', $alias, $field)));
38
        }
39
    }
40
41
    public function getDefaultOptions()
42
    {
43
        return [
44
            'field_type' => BooleanType::class,
45
            'operator_type' => HiddenType::class,
46
            'operator_options' => [],
47
        ];
48
    }
49
50
    public function getRenderSettings()
51
    {
52
        return [DefaultType::class, [
53
            'field_type' => $this->getFieldType(),
54
            'field_options' => $this->getFieldOptions(),
55
            'operator_type' => $this->getOption('operator_type'),
56
            'operator_options' => $this->getOption('operator_options'),
57
            'label' => $this->getLabel(),
58
        ]];
59
    }
60
61
    public function getParentAssociationMappings()
62
    {
63
        $mappings = $this->getOption('parent_association_mappings', []);
64
65
        $fields = explode('.', $this->getFieldName());
66
        for ($i = 0; $i < \count($fields) - 1; ++$i) {
67
            $mappings[] = ['fieldName' => $fields[$i]];
68
        }
69
70
        return $mappings;
71
    }
72
73
    /**
74
     * @param mixed|null $data
75
     *
76
     * @return string[]
77
     */
78
    protected function association(ProxyQueryInterface $queryBuilder, $data): array
79
    {
80
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
81
        $part = strrchr('.'.$this->getFieldName(), '.');
82
        $fieldName = substr(false === $part ? $this->getFieldType() : $part, 1);
83
84
        return [$alias, $fieldName];
85
    }
86
}
87