1<?php
2
3namespace 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 */
10class DateHistogram extends Histogram
11{
12    const DEFAULT_TIMEZONE_VALUE = 'UTC';
13
14    /**
15     * Set time_zone option.
16     *
17     * @param string $timezone
18     *
19     * @return $this
20     */
21    public function setTimezone(string $timezone): self
22    {
23        return $this->setParam('time_zone', $timezone);
24    }
25
26    /**
27     * Adjust for granularity of date data.
28     *
29     * @param int $factor set to 1000 if date is stored in seconds rather than milliseconds
30     *
31     * @return $this
32     */
33    public function setFactor(int $factor): self
34    {
35        return $this->setParam('factor', $factor);
36    }
37
38    /**
39     * Set offset option.
40     *
41     * @param string $offset
42     *
43     * @return $this
44     */
45    public function setOffset(string $offset): self
46    {
47        return $this->setParam('offset', $offset);
48    }
49
50    /**
51     * Set the format for returned bucket key_as_string values.
52     *
53     * @see https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-bucket-daterange-aggregation.html#date-format-pattern
54     *
55     * @param string $format see link for formatting options
56     *
57     * @return $this
58     */
59    public function setFormat(string $format): self
60    {
61        return $this->setParam('format', $format);
62    }
63
64    /**
65     * Set extended bounds option.
66     *
67     * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html#search-aggregations-bucket-histogram-aggregation-extended-bounds
68     *
69     * @param string $min see link for formatting options
70     * @param string $max see link for formatting options
71     *
72     * @return $this
73     */
74    public function setExtendedBounds(string $min = '', string $max = ''): self
75    {
76        $bounds = [];
77        $bounds['min'] = $min;
78        $bounds['max'] = $max;
79        // switch if min is higher then max
80        if (\strtotime($min) > \strtotime($max)) {
81            $bounds['min'] = $max;
82            $bounds['max'] = $min;
83        }
84
85        return $this->setParam('extended_bounds', $bounds);
86    }
87}
88