GeoDistanceRange.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Exception\InvalidException;
  4. trigger_error('Elastica\Query\GeoDistanceRange is deprecated. Use distance aggregations or sorting instead.', E_USER_DEPRECATED);
  5. /**
  6. * Geo distance query.
  7. *
  8. * @author munkie
  9. *
  10. * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-range-query.html
  11. */
  12. class GeoDistanceRange extends AbstractGeoDistance
  13. {
  14. const RANGE_FROM = 'from';
  15. const RANGE_TO = 'to';
  16. const RANGE_LT = 'lt';
  17. const RANGE_LTE = 'lte';
  18. const RANGE_GT = 'gt';
  19. const RANGE_GTE = 'gte';
  20. const RANGE_INCLUDE_LOWER = 'include_lower';
  21. const RANGE_INCLUDE_UPPER = 'include_upper';
  22. /**
  23. * @var array
  24. */
  25. protected $_ranges = [];
  26. /**
  27. * @param string $key
  28. * @param array|string $location
  29. * @param array $ranges
  30. *
  31. * @internal param string $distance
  32. */
  33. public function __construct($key, $location, array $ranges = [])
  34. {
  35. parent::__construct($key, $location);
  36. if (!empty($ranges)) {
  37. $this->setRanges($ranges);
  38. }
  39. }
  40. /**
  41. * @param array $ranges
  42. *
  43. * @return $this
  44. */
  45. public function setRanges(array $ranges)
  46. {
  47. $this->_ranges = [];
  48. foreach ($ranges as $key => $value) {
  49. $this->setRange($key, $value);
  50. }
  51. return $this;
  52. }
  53. /**
  54. * @param string $key
  55. * @param mixed $value
  56. *
  57. * @throws \Elastica\Exception\InvalidException
  58. *
  59. * @return $this
  60. */
  61. public function setRange($key, $value)
  62. {
  63. switch ($key) {
  64. case self::RANGE_TO:
  65. case self::RANGE_FROM:
  66. case self::RANGE_GT:
  67. case self::RANGE_GTE:
  68. case self::RANGE_LT:
  69. case self::RANGE_LTE:
  70. break;
  71. case self::RANGE_INCLUDE_LOWER:
  72. case self::RANGE_INCLUDE_UPPER:
  73. $value = (boolean) $value;
  74. break;
  75. default:
  76. throw new InvalidException('Invalid range parameter given: '.$key);
  77. }
  78. $this->_ranges[$key] = $value;
  79. return $this;
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function toArray()
  85. {
  86. foreach ($this->_ranges as $key => $value) {
  87. $this->setParam($key, $value);
  88. }
  89. return parent::toArray();
  90. }
  91. }