Completed
Pull Request — master (#1876)
by romain
02:29
created

Range::setKeyed()   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
namespace Elastica\Aggregation;
4
5
use 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
 */
12
class Range extends AbstractSimpleAggregation
13
{
14
    use KeyedTrait;
15
16
    /**
17
     * Add a range to this aggregation.
18
     *
19
     * @param float|int $fromValue low end of this range, exclusive (greater than or equal to)
20
     * @param float|int $toValue   high end of this range, exclusive (less than)
21
     * @param string    $key       customized key value
22
     *
23
     * @throws \Elastica\Exception\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 singe 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