Completed
Pull Request — 3.x (#979)
by Christian
02:11 queued 33s
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
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
33
    {
34
        if (null === $data || !\is_array($data) || !\array_key_exists('value', $data)) {
35
            return;
36
        }
37
38
        if ((int) $data['value'] === static::TYPE_EMPTY) {
39
            $queryBuilder->andWhere(sprintf('%s.%s IS null', $alias, $field));
40
        } elseif ((int) $data['value'] === static::TYPE_NOT_EMPTY) {
41
            $queryBuilder->andWhere(sprintf('%s.%s IS NOT null', $alias, $field));
42
        }
43
44
        $this->active = null !== $data['value'];
45
    }
46
47
    public function getDefaultOptions()
48
    {
49
        return [];
50
    }
51
52
    public function getFieldType()
53
    {
54
        return $this->getOption('field_type', ChoiceType::class);
55
    }
56
57
    public function getFieldOptions()
58
    {
59
        return $this->getOption('choices', [
60
            'choices' => [
61
                'label_type_empty' => static::TYPE_EMPTY,
62
                'label_type_not_empty' => static::TYPE_NOT_EMPTY,
63
            ],
64
        ]);
65
    }
66
67
    public function getRenderSettings()
68
    {
69
        return [DefaultType::class, [
70
            'field_type' => $this->getFieldType(),
71
            'field_options' => $this->getFieldOptions(),
72
            'label' => $this->getLabel(),
73
        ]];
74
    }
75
76
    public function getParentAssociationMappings()
77
    {
78
        $mappings = $this->getOption('parent_association_mappings', []);
79
80
        $fields = explode('.', $this->getFieldName());
81
        for ($i = 0; $i < \count($fields) - 1; ++$i) {
82
            $mappings[] = ['fieldName' => $fields[$i]];
83
        }
84
85
        return $mappings;
86
    }
87
88
    protected function association(ProxyQueryInterface $queryBuilder, $data): array
89
    {
90
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
91
        $part = strrchr('.'.$this->getFieldName(), '.');
92
        $fieldName = substr(false === $part ? $this->getFieldType() : $part, 1);
93
94
        return [$alias, $fieldName];
95
    }
96
}
97