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 string $key         Key
20     * @param array  $coordinates Array with top left coordinate as first and bottom right coordinate as second element
21     */
22    public function __construct(string $key, array $coordinates)
23    {
24        $this->addCoordinates($key, $coordinates);
25    }
26
27    /**
28     * Add coordinates.
29     *
30     * @param string $key         Key
31     * @param array  $coordinates Array with top left coordinate as first and bottom right coordinate as second element
32     *
33     * @throws \Elastica\Exception\InvalidException If $coordinates doesn't have two elements
34     *
35     * @return $this
36     */
37    public function addCoordinates(string $key, array $coordinates): self
38    {
39        if (!isset($coordinates[0]) || !isset($coordinates[1])) {
40            throw new InvalidException('expected $coordinates to be an array with two elements');
41        }
42
43        $this->setParam($key, [
44            'top_left' => $coordinates[0],
45            'bottom_right' => $coordinates[1],
46        ]);
47
48        return $this;
49    }
50}
51