summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Aggregation/Range.php
blob: ab70c5e42176b1de8289b94ed8acafe590fdfe12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace Elastica\Aggregation;


use Elastica\Exception\InvalidException;

/**
 * Class Range
 * @package Elastica\Aggregation
 * @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-aggregations-bucket-range-aggregation.html
 */
class Range extends AbstractSimpleAggregation
{
    /**
     * Add a range to this aggregation
     * @param int|float $fromValue low end of this range, exclusive (greater than)
     * @param int|float $toValue high end of this range, exclusive (less than)
     * @return Range
     * @throws \Elastica\Exception\InvalidException
     */
    public function addRange($fromValue = null, $toValue = null)
    {
        if (is_null($fromValue) && is_null($toValue)) {
            throw new InvalidException("Either fromValue or toValue must be set. Both cannot be null.");
        }
        $range = array();
        if (!is_null($fromValue)) {
            $range['from'] = $fromValue;
        }
        if (!is_null($toValue)) {
            $range['to'] = $toValue;
        }
        return $this->addParam('ranges', $range);
    }

    /**
     * If set to true, a unique string key will be associated with each bucket, and ranges will be returned as an associative array
     * @param bool $keyed
     * @return Range
     */
    public function setKeyedResponse($keyed = true)
    {
        return $this->setParam('keyed', (bool)$keyed);
    }
}