Completed
Pull Request — 3.x (#979)
by Christian
04:59 queued 19s
created

EmptyFieldFilter::getRenderSettings()   A

Complexity

Conditions 1
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 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\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
        } else {
43
            throw new \UnexpectedValueException('Invalid filter value given');
44
        }
45
46
        $this->active = null !== $data['value'];
47
    }
48
49
    public function getDefaultOptions()
50
    {
51
        return [];
52
    }
53
54
    public function getFieldType()
55
    {
56
        return $this->getOption('field_type', ChoiceType::class);
57
    }
58
59
    public function getFieldOptions()
60
    {
61
        return $this->getOption('choices', [
62
            'choices' => [
63
                'label_type_empty' => static::TYPE_EMPTY,
64
                'label_type_not_empty' => static::TYPE_NOT_EMPTY,
65
            ],
66
        ]);
67
    }
68
69
    public function getRenderSettings()
70
    {
71
        return [DefaultType::class, [
72
            'field_type' => $this->getFieldType(),
73
            'field_options' => $this->getFieldOptions(),
74
            'label' => $this->getLabel(),
75
        ]];
76
    }
77
78
    public function getParentAssociationMappings()
79
    {
80
        $mappings = $this->getOption('parent_association_mappings', []);
81
82
        $fields = explode('.', $this->getFieldName());
83
        for ($i = 0; $i < \count($fields) - 1; ++$i) {
84
            $mappings[] = ['fieldName' => $fields[$i]];
85
        }
86
87
        return $mappings;
88
    }
89
90
    protected function association(ProxyQueryInterface $queryBuilder, $data): array
91
    {
92
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
93
        $part = strrchr('.'.$this->getFieldName(), '.');
94
        $fieldName = substr(false === $part ? $this->getFieldType() : $part, 1);
95
96
        return [$alias, $fieldName];
97
    }
98
}
99