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