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

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