Completed
Push — master ( 38a8eb...1641b0 )
by
unknown
01:33
created

AbstractDateFilter::getRenderSettings()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 12
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 Kévin Dunglas
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 Kévin Dunglas
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 Kévin Dunglas
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 = DateType::class;
96
97
        if ($this->time && $this->range) {
98
            $name = DateTimeRangeType::class;
99
        } elseif ($this->time) {
100
            $name = DateTimeType::class;
101
        } elseif ($this->range) {
102
            $name = DateRangeType::class;
103
        }
104
105
        return [$name, [
106
            'field_type' => $this->getFieldType(),
107
            'field_options' => $this->getFieldOptions(),
108
            'label' => $this->getLabel(),
109
        ]];
110
    }
111
112
    /**
113
     * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
114
     * @param string                                           $operation
115
     * @param string                                           $field
116
     * @param \DateTime                                        $datetime
117
     */
118
    protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $field, \DateTime $datetime = null)
119
    {
120
        $queryBuilder->field($field)->$operation($datetime);
121
        $this->active = true;
122
    }
123
124
    /**
125
     * Returns if the filter type requires a value to be set.
126
     *
127
     * @param int $type
128
     *
129
     * @return bool
130
     */
131
    protected function typeRequiresValue($type)
132
    {
133
        return in_array($type, [
134
                    DateType::TYPE_NULL,
135
                    DateType::TYPE_NOT_NULL, ]
136
        );
137
    }
138
139
    /**
140
     * Resolves DataType:: constants to MongoDb operators.
141
     *
142
     * @param int $type
143
     *
144
     * @return string
145
     */
146
    protected function getOperator($type)
147
    {
148
        $choices = [
149
            DateType::TYPE_NULL => 'equals',
150
            DateType::TYPE_NOT_NULL => 'notEqual',
151
            DateType::TYPE_EQUAL => 'equals',
152
            DateType::TYPE_GREATER_EQUAL => 'gte',
153
            DateType::TYPE_GREATER_THAN => 'gt',
154
            DateType::TYPE_LESS_EQUAL => 'lte',
155
            DateType::TYPE_LESS_THAN => 'lt',
156
        ];
157
158
        return $choices[intval($type)];
159
    }
160
}
161