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