CallbackFilter::getRenderSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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\HiddenType;
19
use Symfony\Component\Form\Extension\Core\Type\TextType;
20
21
class CallbackFilter extends Filter
22
{
23
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
24
    {
25
        if (!\is_callable($this->getOption('callback'))) {
26
            throw new \RuntimeException(sprintf('Please provide a valid callback option "filter" for field "%s"', $this->getName()));
27
        }
28
29
        $this->active = \call_user_func($this->getOption('callback'), $queryBuilder, $alias, $field, $data);
30
    }
31
32
    public function getDefaultOptions()
33
    {
34
        return [
35
            'callback' => null,
36
            'field_type' => TextType::class,
37
            'operator_type' => HiddenType::class,
38
            'operator_options' => [],
39
        ];
40
    }
41
42
    public function getRenderSettings()
43
    {
44
        return [DefaultType::class, [
45
            'field_type' => $this->getFieldType(),
46
            'field_options' => $this->getFieldOptions(),
47
            'operator_type' => $this->getOption('operator_type'),
48
            'operator_options' => $this->getOption('operator_options'),
49
            'label' => $this->getLabel(),
50
        ]];
51
    }
52
53
    protected function association(ProxyQueryInterface $queryBuilder, $data)
54
    {
55
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
56
57
        return [$this->getOption('alias', $alias), $this->getFieldName()];
58
    }
59
}
60