1<?php
2
3namespace Elastica\Aggregation;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * Class StatsBucket.
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-stats-bucket-aggregation.html
11 */
12class StatsBucket extends AbstractAggregation implements GapPolicyInterface
13{
14    use Traits\BucketsPathTrait;
15    use Traits\GapPolicyTrait;
16
17    public function __construct(string $name, ?string $bucketsPath = null)
18    {
19        parent::__construct($name);
20
21        if (null !== $bucketsPath) {
22            $this->setBucketsPath($bucketsPath);
23        } elseif (\func_num_args() >= 2) {
24            \trigger_deprecation('ruflin/elastica', '7.1.3', 'Passing null as 2nd argument to "%s()" is deprecated, pass a string instead. It will be removed in 8.0.', __METHOD__);
25        } else {
26            \trigger_deprecation('ruflin/elastica', '7.1.3', 'Not passing a 2nd argument to "%s()" is deprecated, pass a string instead. It will be removed in 8.0.', __METHOD__);
27        }
28    }
29
30    /**
31     * Set the format for this aggregation.
32     *
33     * @return $this
34     */
35    public function setFormat(string $format): self
36    {
37        return $this->setParam('format', $format);
38    }
39
40    /**
41     * @throws InvalidException If buckets path or script is not set
42     */
43    public function toArray(): array
44    {
45        if (!$this->hasParam('buckets_path')) {
46            throw new InvalidException('Buckets path is required');
47        }
48
49        return parent::toArray();
50    }
51}
52