1<?php
2
3namespace Elastica\Aggregation;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * Class AvgBucket.
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html
11 */
12class AvgBucket extends AbstractAggregation implements GapPolicyInterface
13{
14    use Traits\BucketsPathTrait;
15    use Traits\GapPolicyTrait;
16
17    public const DEFAULT_FORMAT_VALUE = null;
18
19    public function __construct(string $name, ?string $bucketsPath = null)
20    {
21        parent::__construct($name);
22
23        if (null !== $bucketsPath) {
24            $this->setBucketsPath($bucketsPath);
25        } elseif (\func_num_args() >= 2) {
26            \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__);
27        } else {
28            \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__);
29        }
30    }
31
32    /**
33     * Set the format for this aggregation.
34     *
35     * @return $this
36     */
37    public function setFormat(?string $format = null): self
38    {
39        return $this->setParam('format', $format);
40    }
41
42    /**
43     * @throws InvalidException If buckets path or script is not set
44     */
45    public function toArray(): array
46    {
47        if (!$this->hasParam('buckets_path')) {
48            throw new InvalidException('Buckets path is required');
49        }
50
51        return parent::toArray();
52    }
53}
54