DateFilter::getRenderSettings()   A
last analyzed

Complexity

Conditions 1
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 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\AdminBundle\Form\Type\Filter\DateType;
18
19
class DateFilter extends Filter
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data): void
25
    {
26
        if (!$data || !\is_array($data) || !isset($data['value'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
27
            return;
28
        }
29
30
        $data['type'] = $data['type'] ?? DateType::TYPE_EQUAL;
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...er\DateType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
31
32
        $where = $this->getWhere($proxyQuery);
0 ignored issues
show
Compatibility introduced by
$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...
33
34
        $from = $data['value'];
35
        $to = new \DateTime($from->format('Y-m-d').' +86399 seconds'); // 23 hours 59 minutes 59 seconds
36
37
        switch ($data['type']) {
38
            case DateType::TYPE_GREATER_EQUAL:
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...ype::TYPE_GREATER_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_GREATER_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
39
                $where->gte()->field('a.'.$field)->literal($from);
40
41
                break;
42
            case DateType::TYPE_GREATER_THAN:
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...Type::TYPE_GREATER_THAN has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_GREATER_THAN instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
43
                $where->gt()->field('a.'.$field)->literal($from);
44
45
                break;
46
            case DateType::TYPE_LESS_EQUAL:
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...teType::TYPE_LESS_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_LESS_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
47
                $where->lte()->field('a.'.$field)->literal($from);
48
49
                break;
50
            case DateType::TYPE_LESS_THAN:
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...ateType::TYPE_LESS_THAN has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_LESS_THAN instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
51
                $where->lt()->field('a.'.$field)->literal($from);
52
53
                break;
54
            case DateType::TYPE_NULL:
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...ter\DateType::TYPE_NULL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_NULL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
55
                $where->eq()->field('a.'.$field)->literal(null);
56
57
                break;
58
            case DateType::TYPE_NOT_NULL:
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...DateType::TYPE_NOT_NULL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_NOT_NULL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
59
                $where->neq()->field('a.'.$field)->literal(null);
60
61
                break;
62
            case DateType::TYPE_EQUAL:
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...er\DateType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use DateOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
63
            default:
64
                $where->andX()
65
                    ->gte()->field('a.'.$field)->literal($from)->end()
66
                    ->lte()->field('a.'.$field)->literal($to)->end();
67
        }
68
69
        // filter is active as we have now modified the query
70
        $this->active = true;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getDefaultOptions()
77
    {
78
        return [
79
            'date_format' => 'yyyy-MM-dd',
80
        ];
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getRenderSettings()
87
    {
88
        return ['sonata_type_filter_date', [
89
            'field_type' => $this->getFieldType(),
90
            'field_options' => $this->getFieldOptions(),
91
            'label' => $this->getLabel(),
92
        ]];
93
    }
94
}
95