Completed
Push — master ( a33308...36ba79 )
by Maximilian
01:58
created

src/Filter/DateFilter.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project 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\DoctrinePHPCRAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\AdminBundle\Form\Type\Filter\DateType;
16
17
class DateFilter extends Filter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
23
    {
24
        if (!$data || !is_array($data) || !isset($data['value'])) {
25
            return;
26
        }
27
28
        $data['type'] = isset($data['type']) ? $data['type'] : DateType::TYPE_EQUAL;
29
30
        $where = $this->getWhere($proxyQuery);
0 ignored issues
show
$proxyQuery of type object<Sonata\AdminBundl...id\ProxyQueryInterface> is not a sub-type of object<Sonata\DoctrinePH...le\Datagrid\ProxyQuery>. It seems like you assume a concrete implementation of the interface Sonata\AdminBundle\Datagrid\ProxyQueryInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
31
32
        $from = $data['value'];
33
        $to = new \DateTime($from->format('Y-m-d').' +86399 seconds'); // 23 hours 59 minutes 59 seconds
34
35
        switch ($data['type']) {
36
            case DateType::TYPE_GREATER_EQUAL:
37
                $where->gte()->field('a.'.$field)->literal($from);
38
                break;
39
            case DateType::TYPE_GREATER_THAN:
40
                $where->gt()->field('a.'.$field)->literal($from);
41
                break;
42
            case DateType::TYPE_LESS_EQUAL:
43
                $where->lte()->field('a.'.$field)->literal($from);
44
                break;
45
            case DateType::TYPE_LESS_THAN:
46
                $where->lt()->field('a.'.$field)->literal($from);
47
                break;
48
            case DateType::TYPE_NULL:
49
                $where->eq()->field('a.'.$field)->literal(null);
50
                break;
51
            case DateType::TYPE_NOT_NULL:
52
                $where->neq()->field('a.'.$field)->literal(null);
53
                break;
54
            case DateType::TYPE_EQUAL:
55
            default:
56
                $where->andX()
57
                    ->gte()->field('a.'.$field)->literal($from)->end()
58
                    ->lte()->field('a.'.$field)->literal($to)->end();
59
        }
60
61
        // filter is active as we have now modified the query
62
        $this->active = true;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getDefaultOptions()
69
    {
70
        return array(
71
            'date_format' => 'yyyy-MM-dd',
72
        );
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getRenderSettings()
79
    {
80
        return array('sonata_type_filter_date', array(
81
            'field_type' => $this->getFieldType(),
82
            'field_options' => $this->getFieldOptions(),
83
            'label' => $this->getLabel(),
84
        ));
85
    }
86
}
87