Completed
Pull Request — master (#1848)
by
unknown
02:35
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
    /**
15
     * Add a range to this aggregation.
16
     *
17
     * @param float|int $fromValue low end of this range, exclusive (greater than or equal to)
18
     * @param float|int $toValue   high end of this range, exclusive (less than)
19
     * @param string    $key       customized key value
20
     *
21
     * @throws \Elastica\Exception\InvalidException
22
     *
23
     * @return $this
24
     */
25
    public function addRange($fromValue = null, $toValue = null, ?string $key = null): self
26
    {
27
        if (null === $fromValue && null === $toValue) {
28
            throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.');
29
        }
30
31
        $range = [];
32
33
        if (null !== $fromValue) {
34
            $range['from'] = $fromValue;
35
        }
36
37
        if (null !== $toValue) {
38
            $range['to'] = $toValue;
39
        }
40
41
        if (null !== $key) {
42
            $range['key'] = $key;
43
        }
44
45
        return $this->addParam('ranges', $range);
46
    }
47
48
    /**
49
     * If set to true, a unique string key will be associated with each bucket, and ranges will be returned as an associative array.
50
     *
51
     * @return $this
52
     */
53
    public function setKeyed(bool $keyed): self
54
    {
55
        return $this->setParam('keyed', $keyed);
56
    }
57
58
    /**
59
     * @return $this
60
     *
61
     * @deprecated singe version 7.1.0, use the "setKeyed()" method instead.
62
     */
63
    public function setKeyedResponse(bool $keyed = true): self
64
    {
65
        trigger_deprecation('ruflin/elastica', '7.1.0', 'The "%s()" method is deprecated, use setKeyed() instead. It will be removed in 8.0.', __METHOD__);
66
67
        return $this->setKeyed($keyed);
68
    }
69
}
70