1<?php
2
3namespace Elastica\Aggregation;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * Class Range.
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
11 */
12class Range extends AbstractSimpleAggregation
13{
14    use Traits\KeyedTrait;
15
16    /**
17     * Add a range to this aggregation.
18     *
19     * @param float|int|string|null $fromValue low end of this range, exclusive (greater than or equal to)
20     * @param float|int|string|null $toValue   high end of this range, exclusive (less than)
21     * @param string|null           $key       customized key value
22     *
23     * @throws InvalidException
24     *
25     * @return $this
26     */
27    public function addRange($fromValue = null, $toValue = null, ?string $key = null): self
28    {
29        if (null === $fromValue && null === $toValue) {
30            throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.');
31        }
32
33        $range = [];
34
35        if (null !== $fromValue) {
36            $range['from'] = $fromValue;
37        }
38
39        if (null !== $toValue) {
40            $range['to'] = $toValue;
41        }
42
43        if (null !== $key) {
44            $range['key'] = $key;
45        }
46
47        return $this->addParam('ranges', $range);
48    }
49
50    /**
51     * @return $this
52     *
53     * @deprecated since version 7.1.0, use the "setKeyed()" method instead.
54     */
55    public function setKeyedResponse(bool $keyed = true): self
56    {
57        \trigger_deprecation('ruflin/elastica', '7.1.0', 'The "%s()" method is deprecated, use "setKeyed()" instead. It will be removed in 8.0.', __METHOD__);
58
59        return $this->setKeyed($keyed);
60    }
61}
62