Completed
Pull Request — master (#348)
by
unknown
10:14
created

DateRangeAggregation::__construct()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 9
nop 5
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 date range aggregation.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html
23
 */
24
class DateRangeAggregation extends AbstractAggregation
25
{
26
    use BucketingTrait;
27
28
    private array $ranges = [];
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_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
29
30
    private bool $keyed = false;
31
32
    public function __construct(
33
        private string $name,
34
        private ?string $field = null,
35
        private ?string $format = null,
36
        array $ranges = [],
37
        bool $keyed = false
38
    ) {
39
        parent::__construct($name);
40
41
        $this->setField($field);
42
        $this->setFormat($format);
43
        $this->setKeyed($keyed);
44
        foreach ($ranges as $range) {
45
            $from = isset($range['from']) ? $range['from'] : null;
46
            $to = isset($range['to']) ? $range['to'] : null;
47
            $key = isset($range['key']) ? $range['key'] : null;
48
            $this->addRange($from, $to, $key);
49
        }
50
    }
51
52
    public function setKeyed($keyed): static
53
    {
54
        $this->keyed = $keyed;
55
56
        return $this;
57
    }
58
59
    public function getFormat(): string
60
    {
61
        return $this->format;
62
    }
63
64
    public function setFormat($format): static
65
    {
66
        $this->format = $format;
67
68
        return $this;
69
    }
70
71
    public function addRange(?string $from = null, ?string $to = null, ?string $key = null): static
72
    {
73
        $range = array_filter(
74
            [
75
                'from' => $from,
76
                'to' => $to,
77
                'key' => $key,
78
            ],
79
            fn(mixed $v): bool => !is_null($v)
80
        );
81
82
        if (empty($range)) {
83
            throw new \LogicException('Either from or to must be set. Both cannot be null.');
84
        }
85
86
        $this->ranges[] = $range;
87
88
        return $this;
89
    }
90
91
    public function getArray(): array
92
    {
93
        if (!$this->getField() || !$this->getFormat() || empty($this->ranges)) {
94
            throw new \LogicException('Date range aggregation must have field, format set and range added.');
95
        }
96
97
        return [
98
            'format' => $this->getFormat(),
99
            'field' => $this->getField(),
100
            'ranges' => $this->ranges,
101
            'keyed' => $this->keyed,
102
        ];
103
    }
104
105
    public function getType(): string
106
    {
107
        return 'date_range';
108
    }
109
}
110