Completed
Pull Request — master (#1874)
by romain
02:48
created

DateHistogram::setCalendarInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Elastica\Aggregation;
4
5
/**
6
 * Class DateHistogram.
7
 *
8
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
9
 */
10
class DateHistogram extends AbstractSimpleAggregation
11
{
12
    use Traits\KeyedTrait;
13
    use Traits\MissingTrait;
14
15
    public const DEFAULT_TIMEZONE_VALUE = 'UTC';
16
17
    /**
18
     * @param string     $name     the name of this aggregation
19
     * @param string     $field    the name of the field on which to perform the aggregation
20
     * @param int|string $interval the interval by which documents will be bucketed
21
     */
22
    public function __construct(string $name, string $field, $interval = null)
23
    {
24
        parent::__construct($name, $field);
0 ignored issues
show
Unused Code introduced by
The call to AbstractSimpleAggregation::__construct() has too many arguments starting with $field.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
25
        $this->setField($field);
26
27
        if (null !== $interval) {
28
            trigger_deprecation('ruflin/elastica', '7.1.0', 'Argument 3 passed to "%s()" is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.', __METHOD__);
29
30
            $this->setParam('interval', $interval);
31
        }
32
    }
33
34
    /**
35
     * Set the interval by which documents will be bucketed.
36
     *
37
     * @deprecated Deprecated since 7.1.0
38
     *
39
     * @param int|string $interval
40
     *
41
     * @return $this
42
     */
43
    public function setInterval($interval): self
44
    {
45
        trigger_deprecation('ruflin/elastica', '7.1.0', 'The "%s()" method is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.', __METHOD__);
46
47
        return $this->setParam('interval', $interval);
48
    }
49
50
    /**
51
     * Set the fixed interval by which documents will be bucketed.
52
     *
53
     * @param int|string $interval
54
     *
55
     * @return $this
56
     */
57
    public function setFixedInterval($interval): self
58
    {
59
        return $this->setParam('fixed_interval', $interval);
60
    }
61
62
    /**
63
     * Set the calendar interval by which documents will be bucketed.
64
     *
65
     * @param int|string $interval
66
     *
67
     * @return $this
68
     */
69
    public function setCalendarInterval($interval): self
70
    {
71
        return $this->setParam('calendar_interval', $interval);
72
    }
73
74
    /**
75
     * Set time_zone option.
76
     *
77
     * @return $this
78
     */
79
    public function setTimezone(string $timezone): self
80
    {
81
        return $this->setParam('time_zone', $timezone);
82
    }
83
84
    /**
85
     * Adjust for granularity of date data.
86
     *
87
     * @param int $factor set to 1000 if date is stored in seconds rather than milliseconds
88
     *
89
     * @return $this
90
     */
91
    public function setFactor(int $factor): self
92
    {
93
        return $this->setParam('factor', $factor);
94
    }
95
96
    /**
97
     * Set offset option.
98
     *
99
     * @return $this
100
     */
101
    public function setOffset(string $offset): self
102
    {
103
        return $this->setParam('offset', $offset);
104
    }
105
106
    /**
107
     * Set the format for returned bucket key_as_string values.
108
     *
109
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html#date-format-pattern
110
     *
111
     * @param string $format see link for formatting options
112
     *
113
     * @return $this
114
     */
115
    public function setFormat(string $format): self
116
    {
117
        return $this->setParam('format', $format);
118
    }
119
120
    /**
121
     * Set extended bounds option.
122
     *
123
     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html#search-aggregations-bucket-histogram-aggregation-extended-bounds
124
     *
125
     * @param string $min see link for formatting options
126
     * @param string $max see link for formatting options
127
     *
128
     * @return $this
129
     */
130
    public function setExtendedBounds(string $min = '', string $max = ''): self
131
    {
132
        $bounds = [];
133
        $bounds['min'] = $min;
134
        $bounds['max'] = $max;
135
        // switch if min is higher then max
136
        if (\strtotime($min) > \strtotime($max)) {
137
            $bounds['min'] = $max;
138
            $bounds['max'] = $min;
139
        }
140
141
        return $this->setParam('extended_bounds', $bounds);
142
    }
143
144
    /**
145
     * Set the bucket sort order.
146
     *
147
     * @param string $order     "_count", "_term", or the name of a sub-aggregation or sub-aggregation response field
148
     * @param string $direction "asc" or "desc"
149
     *
150
     * @return $this
151
     */
152
    public function setOrder(string $order, string $direction): self
153
    {
154
        return $this->setParam('order', [$order => $direction]);
155
    }
156
157
    /**
158
     * Set the minimum number of documents which must fall into a bucket in order for the bucket to be returned.
159
     *
160
     * @param int $count set to 0 to include empty buckets
161
     *
162
     * @return $this
163
     */
164
    public function setMinimumDocumentCount(int $count): self
165
    {
166
        return $this->setParam('min_doc_count', $count);
167
    }
168
}
169