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

EmptyFieldFilter::association()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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