Completed
Push — master ( 9552d6...4b2b1b )
by Simonas
08:12
created

DateHistogramAggregation::setFormat()   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
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\ElasticsearchDSL\Aggregation\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
17
/**
18
 * Class representing Histogram aggregation.
19
 *
20
 * @link https://goo.gl/hGCdDd
21
 */
22
class DateHistogramAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    /**
27
     * @var string
28
     */
29
    protected $interval;
30
31
    /**
32
     * @var string
33
     */
34
    protected $format;
35
36
    /**
37
     * Inner aggregations container init.
38
     *
39
     * @param string $name
40
     * @param string $field
41
     * @param string $interval
42
     */
43
    public function __construct($name, $field = null, $interval = null, $format = null)
44
    {
45
        parent::__construct($name);
46
47
        $this->setField($field);
48
        $this->setInterval($interval);
49
        $this->setFormat($format);
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function getInterval()
56
    {
57
        return $this->interval;
58
    }
59
60
    /**
61
     * @param string $interval
62
     */
63
    public function setInterval($interval)
64
    {
65
        $this->interval = $interval;
66
    }
67
68
    /**
69
     * @param string $format
70
     */
71
    public function setFormat($format)
72
    {
73
        $this->format = $format;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getType()
80
    {
81
        return 'date_histogram';
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getArray()
88
    {
89
        if (!$this->getField() || !$this->getInterval()) {
90
            throw new \LogicException('Date histogram aggregation must have field and interval set.');
91
        }
92
93
        $out = [
94
            'field' => $this->getField(),
95
            'interval' => $this->getInterval(),
96
        ];
97
98
        if (!empty($this->format)) {
99
            $out['format'] = $this->format;
100
        }
101
102
        return $out;
103
    }
104
}
105