Completed
Pull Request — 3.x (#979)
by Christian
02:15 queued 45s
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 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
            $queryBuilder->andWhere(sprintf('%s.%s IS null', $alias, $field));
36
        } elseif (BooleanType::TYPE_YES === (int) $data['value']) {
37
            $queryBuilder->andWhere(sprintf('%s.%s IS NOT null', $alias, $field));
38
        }
39
40
        $this->active = null !== $data['value'];
41
    }
42
43
    public function getDefaultOptions()
44
    {
45
        return [
46
            'field_type' => BooleanType::class,
47
            'operator_type' => HiddenType::class,
48
            'operator_options' => [],
49
        ];
50
    }
51
52
    public function getRenderSettings()
53
    {
54
        return [DefaultType::class, [
55
            'field_type' => $this->getFieldType(),
56
            'field_options' => $this->getFieldOptions(),
57
            'operator_type' => $this->getOption('operator_type'),
58
            'operator_options' => $this->getOption('operator_options'),
59
            'label' => $this->getLabel(),
60
        ]];
61
    }
62
63
    public function getParentAssociationMappings()
64
    {
65
        $mappings = $this->getOption('parent_association_mappings', []);
66
67
        $fields = explode('.', $this->getFieldName());
68
        for ($i = 0; $i < \count($fields) - 1; ++$i) {
69
            $mappings[] = ['fieldName' => $fields[$i]];
70
        }
71
72
        return $mappings;
73
    }
74
75
    /**
76
     * @param mixed|null $data
77
     *
78
     * @return string[]
79
     */
80
    protected function association(ProxyQueryInterface $queryBuilder, $data): array
81
    {
82
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
83
        $part = strrchr('.'.$this->getFieldName(), '.');
84
        $fieldName = substr(false === $part ? $this->getFieldType() : $part, 1);
85
86
        return [$alias, $fieldName];
87
    }
88
}
89