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\DoctrinePHPCRAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\DoctrinePHPCRAdminBundle\Filter\Filter as BaseFilter;
18
19
class CallbackFilter extends BaseFilter
20
{
21
    /**
22
     * {@inheritdoc}
23
     *
24
     * @throws \InvalidArgumentException if the filter is not configured with a
25
     *                                   callable in the 'callback' option field
26
     */
27
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data): void
28
    {
29
        if (!\is_callable($this->getOption('callback'))) {
30
            throw new \RuntimeException(sprintf('Please provide a valid callback for option "callback" and field "%s"', $this->getName()));
31
        }
32
33
        $this->active = true === \call_user_func($this->getOption('callback'), $proxyQuery, $alias, $field, $data);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getDefaultOptions()
40
    {
41
        return [
42
            'callback' => null,
43
            'field_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
44
            'operator_type' => 'hidden',
45
            'operator_options' => [],
46
        ];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getRenderSettings()
53
    {
54
        return ['sonata_type_filter_default', [
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