PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Elastica/Aggregation/Range.php

http://github.com/ruflin/Elastica
PHP | 58 lines | 27 code | 8 blank | 23 comment | 5 complexity | de7f25ff3f59842147a6055d226b10ee MD5 | raw file
  1. <?php
  2. namespace Elastica\Aggregation;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Class Range.
  6. *
  7. * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
  8. */
  9. class Range extends AbstractSimpleAggregation
  10. {
  11. /**
  12. * Add a range to this aggregation.
  13. *
  14. * @param int|float $fromValue low end of this range, exclusive (greater than or equal to)
  15. * @param int|float $toValue high end of this range, exclusive (less than)
  16. * @param string $key customized key value
  17. *
  18. * @throws \Elastica\Exception\InvalidException
  19. *
  20. * @return $this
  21. */
  22. public function addRange($fromValue = null, $toValue = null, $key = null)
  23. {
  24. if (is_null($fromValue) && is_null($toValue)) {
  25. throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.');
  26. }
  27. $range = [];
  28. if (!is_null($fromValue)) {
  29. $range['from'] = $fromValue;
  30. }
  31. if (!is_null($toValue)) {
  32. $range['to'] = $toValue;
  33. }
  34. if (!is_null($key)) {
  35. $range['key'] = $key;
  36. }
  37. return $this->addParam('ranges', $range);
  38. }
  39. /**
  40. * If set to true, a unique string key will be associated with each bucket, and ranges will be returned as an associative array.
  41. *
  42. * @param bool $keyed
  43. *
  44. * @return $this
  45. */
  46. public function setKeyedResponse($keyed = true)
  47. {
  48. return $this->setParam('keyed', (bool) $keyed);
  49. }
  50. }