Elastica\Aggregation\Range::addRange PHP Method

addRange() public method

Add a range to this aggregation.
public addRange ( integer | float $fromValue = null, integer | float $toValue = null, string $key = null )
$fromValue integer | float low end of this range, exclusive (greater than or equal to)
$toValue integer | float high end of this range, exclusive (less than)
$key string customized key value
    public function addRange($fromValue = null, $toValue = null, $key = null)
    {
        if (is_null($fromValue) && is_null($toValue)) {
            throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.');
        }
        $range = [];
        if (!is_null($fromValue)) {
            $range['from'] = $fromValue;
        }
        if (!is_null($toValue)) {
            $range['to'] = $toValue;
        }
        if (!is_null($key)) {
            $range['key'] = $key;
        }
        return $this->addParam('ranges', $range);
    }

Usage Example

 /**
  * @group unit
  */
 public function testRangeAggregationWithKey()
 {
     $agg = new Range('range');
     $agg->setField('price');
     $agg->addRange(null, 50, 'cheap');
     $agg->addRange(50, 100, 'average');
     $agg->addRange(100, null, 'expensive');
     $expected = array('range' => array('field' => 'price', 'ranges' => array(array('to' => 50, 'key' => 'cheap'), array('from' => 50, 'to' => 100, 'key' => 'average'), array('from' => 100, 'key' => 'expensive'))));
     $this->assertEquals($expected, $agg->toArray());
 }
All Usage Examples Of Elastica\Aggregation\Range::addRange