Completed
Pull Request — 3.x (#252)
by Grégoire
01:30
created

AbstractDateFilter::getRenderSettings()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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