Completed
Push — 3.x ( 3a82e5...0238da )
by Grégoire
01:26
created

src/Filter/AbstractDateFilter.php (1 issue)

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
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\DoctrineMongoDBAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DateRangeType;
18
use Sonata\AdminBundle\Form\Type\Filter\DateTimeRangeType;
19
use Sonata\AdminBundle\Form\Type\Filter\DateTimeType;
20
use Sonata\AdminBundle\Form\Type\Filter\DateType;
21
22
abstract class AbstractDateFilter extends Filter
23
{
24
    /**
25
     * Flag indicating that filter will have range.
26
     *
27
     * @var bool
28
     */
29
    protected $range = false;
30
31
    /**
32
     * Flag indicating that filter will filter by datetime instead by date.
33
     *
34
     * @var bool
35
     */
36
    protected $time = false;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
42
    {
43
        //check data sanity
44
        if (true !== \is_array($data)) {
45
            return;
46
        }
47
48
        //default type for simple filter
49
        $data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateType::TYPE_EQUAL : $data['type'];
50
51
        // Some types do not require a value to be set (NULL, NOT NULL).
52
        if (!$this->typeRequiresValue($data['type']) && !$data['value']) {
53
            return;
54
        }
55
56
        switch ($data['type']) {
57
            case DateType::TYPE_EQUAL:
58
                return $this->applyTypeIsEqual($queryBuilder, $field, $data);
59
60
            case DateType::TYPE_GREATER_THAN:
61
                if (!\array_key_exists('value', $data) || !$data['value']) {
62
                    return;
63
                }
64
65
                return $this->applyTypeIsGreaterThan($queryBuilder, $field, $data);
66
67
            case DateType::TYPE_LESS_EQUAL:
68
                if (!\array_key_exists('value', $data) || !$data['value']) {
69
                    return;
70
                }
71
72
                return $this->applyTypeIsLessEqual($queryBuilder, $field, $data);
73
74
            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...
75
            case DateType::TYPE_NOT_NULL:
76
                return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);
77
78
            case DateType::TYPE_GREATER_EQUAL:
79
            case DateType::TYPE_LESS_THAN:
80
                return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getDefaultOptions()
88
    {
89
        return ['input_type' => 'datetime'];
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getRenderSettings()
96
    {
97
        $name = DateType::class;
98
99
        if ($this->time && $this->range) {
100
            $name = DateTimeRangeType::class;
101
        } elseif ($this->time) {
102
            $name = DateTimeType::class;
103
        } elseif ($this->range) {
104
            $name = DateRangeType::class;
105
        }
106
107
        return [$name, [
108
            'field_type' => $this->getFieldType(),
109
            'field_options' => $this->getFieldOptions(),
110
            'label' => $this->getLabel(),
111
        ]];
112
    }
113
114
    /**
115
     * @param string    $operation
116
     * @param string    $field
117
     * @param \DateTime $datetime
118
     */
119
    protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $field, \DateTime $datetime = null)
120
    {
121
        $queryBuilder->field($field)->$operation($datetime);
122
        $this->active = true;
123
    }
124
125
    /**
126
     * Returns if the filter type requires a value to be set.
127
     *
128
     * @param int $type
129
     *
130
     * @return bool
131
     */
132
    protected function typeRequiresValue($type)
133
    {
134
        return \in_array($type, [
135
            DateType::TYPE_NULL,
136
            DateType::TYPE_NOT_NULL,
137
        ], true);
138
    }
139
140
    /**
141
     * Resolves DataType:: constants to MongoDb operators.
142
     *
143
     * @param int $type
144
     *
145
     * @return string
146
     */
147
    protected function getOperator($type)
148
    {
149
        $choices = [
150
            DateType::TYPE_NULL => 'equals',
151
            DateType::TYPE_NOT_NULL => 'notEqual',
152
            DateType::TYPE_EQUAL => 'equals',
153
            DateType::TYPE_GREATER_EQUAL => 'gte',
154
            DateType::TYPE_GREATER_THAN => 'gt',
155
            DateType::TYPE_LESS_EQUAL => 'lte',
156
            DateType::TYPE_LESS_THAN => 'lt',
157
        ];
158
159
        return $choices[(int) $type];
160
    }
161
}
162