1<?php
2
3namespace Elastica\Aggregation;
4
5/**
6 * Class GeohashGrid.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geohashgrid-aggregation.html
9 */
10class GeohashGrid extends AbstractAggregation
11{
12    use Traits\ShardSizeTrait;
13
14    public const DEFAULT_PRECISION_VALUE = 5;
15    public const DEFAULT_SIZE_VALUE = 10000;
16
17    /**
18     * @param string $name  the name of this aggregation
19     * @param string $field the field on which to perform this aggregation
20     */
21    public function __construct(string $name, string $field)
22    {
23        parent::__construct($name);
24        $this->setField($field);
25    }
26
27    /**
28     * Set the field for this aggregation.
29     *
30     * @param string $field the name of the document field on which to perform this aggregation
31     *
32     * @return $this
33     */
34    public function setField(string $field): self
35    {
36        return $this->setParam('field', $field);
37    }
38
39    /**
40     * Set the precision for this aggregation.
41     *
42     * @param int|string $precision an integer between 1 and 12, inclusive. Defaults to 5 or distance like 1km, 10m
43     *
44     * @return $this
45     */
46    public function setPrecision($precision): self
47    {
48        if (!\is_int($precision) && !\is_string($precision)) {
49            throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be of type int|string, %s given.', __METHOD__, \is_object($precision) ? \get_class($precision) : \gettype($precision)));
50        }
51
52        return $this->setParam('precision', $precision);
53    }
54
55    /**
56     * Set the maximum number of buckets to return.
57     *
58     * @param int $size defaults to 10,000
59     *
60     * @return $this
61     */
62    public function setSize(int $size): self
63    {
64        return $this->setParam('size', $size);
65    }
66}
67