GeoBoundingBox.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Geo bounding box query.
  6. *
  7. * @author Fabian Vogler <fabian@equivalence.ch>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html
  10. */
  11. class GeoBoundingBox extends AbstractQuery
  12. {
  13. /**
  14. * Construct BoundingBoxQuery.
  15. *
  16. * @param string $key Key
  17. * @param array $coordinates Array with top left coordinate as first and bottom right coordinate as second element
  18. */
  19. public function __construct($key, array $coordinates)
  20. {
  21. $this->addCoordinates($key, $coordinates);
  22. }
  23. /**
  24. * Add coordinates.
  25. *
  26. * @param string $key Key
  27. * @param array $coordinates Array with top left coordinate as first and bottom right coordinate as second element
  28. *
  29. * @throws \Elastica\Exception\InvalidException If $coordinates doesn't have two elements
  30. *
  31. * @return $this
  32. */
  33. public function addCoordinates($key, array $coordinates)
  34. {
  35. if (!isset($coordinates[0]) || !isset($coordinates[1])) {
  36. throw new InvalidException('expected $coordinates to be an array with two elements');
  37. }
  38. $this->setParam($key, [
  39. 'top_left' => $coordinates[0],
  40. 'bottom_right' => $coordinates[1],
  41. ]);
  42. return $this;
  43. }
  44. }