Completed
Pull Request — master (#348)
by
unknown
09:42 queued 08:20
created

DateHistogramAggregation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 91
rs 10
c 0
b 0
f 0
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
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
15
16
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
17
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
18
19
/**
20
 * Class representing Histogram aggregation.
21
 *
22
 * @link https://goo.gl/hGCdDd
23
 */
24
class DateHistogramAggregation extends AbstractAggregation
25
{
26
    use BucketingTrait;
27
28
    public function __construct(
29
        private string $name,
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PRIVATE, expecting T_VARIABLE
Loading history...
30
        private ?string $field = null,
31
        private mixed $interval = null,
32
        private $format = null
33
    ) {
34
        parent::__construct($name);
35
    }
36
37
    public function getInterval(): mixed
38
    {
39
        return $this->interval;
40
    }
41
42
    public function setInterval(mixed $interval): static
43
    {
44
        $this->interval = $interval;
45
46
        return $this;
47
    }
48
49
    public function setFormat(?string $format): static
50
    {
51
        $this->format = $format;
52
53
        return $this;
54
    }
55
56
    public function getType(): string
57
    {
58
        return 'date_histogram';
59
    }
60
61
    public function getArray(): array
62
    {
63
        if (!$this->getField() || !$this->getInterval()) {
64
            throw new \LogicException('Date histogram aggregation must have field and interval set.');
65
        }
66
67
        $out = [
68
            'field' => $this->getField(),
69
            'interval' => $this->getInterval(),
70
        ];
71
72
        if (!empty($this->format)) {
73
            $out['format'] = $this->format;
74
        }
75
76
        return $out;
77
    }
78
}
79