Completed
Push — master ( bae860...fc1986 )
by Grégoire
11s
created

src/Filter/AbstractDateFilter.php (3 issues)

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);
0 ignored issues
show
The method applyTypeIsEqual() does not exist on Sonata\DoctrineMongoDBAd...lter\AbstractDateFilter. Did you maybe mean applyType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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);
0 ignored issues
show
The method applyTypeIsGreaterThan() does not exist on Sonata\DoctrineMongoDBAd...lter\AbstractDateFilter. Did you maybe mean applyType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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);
0 ignored issues
show
The method applyTypeIsLessEqual() does not exist on Sonata\DoctrineMongoDBAd...lter\AbstractDateFilter. Did you maybe mean applyType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
73
74
            case DateType::TYPE_NULL:
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 \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
116
     * @param string                                           $operation
117
     * @param string                                           $field
118
     * @param \DateTime                                        $datetime
119
     */
120
    protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $field, \DateTime $datetime = null): void
121
    {
122
        $queryBuilder->field($field)->$operation($datetime);
123
        $this->active = true;
124
    }
125
126
    /**
127
     * Returns if the filter type requires a value to be set.
128
     *
129
     * @param int $type
130
     *
131
     * @return bool
132
     */
133
    protected function typeRequiresValue($type)
134
    {
135
        return \in_array($type, [
136
            DateType::TYPE_NULL,
137
            DateType::TYPE_NOT_NULL,
138
        ], true);
139
    }
140
141
    /**
142
     * Resolves DataType:: constants to MongoDb operators.
143
     *
144
     * @param int $type
145
     *
146
     * @return string
147
     */
148
    protected function getOperator($type)
149
    {
150
        $choices = [
151
            DateType::TYPE_NULL => 'equals',
152
            DateType::TYPE_NOT_NULL => 'notEqual',
153
            DateType::TYPE_EQUAL => 'equals',
154
            DateType::TYPE_GREATER_EQUAL => 'gte',
155
            DateType::TYPE_GREATER_THAN => 'gt',
156
            DateType::TYPE_LESS_EQUAL => 'lte',
157
            DateType::TYPE_LESS_THAN => 'lt',
158
        ];
159
160
        return $choices[(int) $type];
161
    }
162
}
163