Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

CallbackFilter::getRenderSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
16
class CallbackFilter extends Filter
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected function association(ProxyQueryInterface $queryBuilder, $data)
22
    {
23
        return array($this->getOption('alias', $queryBuilder->getRootAlias()), false);
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
30
    {
31
        if (!is_callable($this->getOption('callback'))) {
32
            throw new \RuntimeException(sprintf('Please provide a valid callback option "filter" for field "%s"', $this->getName()));
33
        }
34
35
        $this->active = call_user_func($this->getOption('callback'), $queryBuilder, $alias, $field, $data);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getDefaultOptions()
42
    {
43
        return array(
44
            'callback'    => null,
45
            'field_type'  => 'text',
46
            'operator_type' => 'hidden',
47
            'operator_options' => array()
48
        );
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getRenderSettings()
55
    {
56
        return array('sonata_type_filter_default', array(
57
            'field_type'    => $this->getFieldType(),
58
            'field_options' => $this->getFieldOptions(),
59
            'operator_type' => $this->getOption('operator_type'),
60
            'operator_options' => $this->getOption('operator_options'),
61
            'label'         => $this->getLabel()
62
        ));
63
    }
64
}
65