1<?php
2
3namespace Elastica\Aggregation;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * Class NormalizeAggregation.
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-normalize-aggregation.html
11 */
12class NormalizeAggregation extends AbstractAggregation
13{
14    use Traits\BucketsPathTrait;
15
16    public function __construct(string $name, ?string $bucketsPath = null, ?string $method = null)
17    {
18        parent::__construct($name);
19
20        if (null !== $bucketsPath) {
21            $this->setBucketsPath($bucketsPath);
22        } elseif (\func_num_args() >= 2) {
23            \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__);
24        } else {
25            \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__);
26        }
27
28        if (null !== $method) {
29            $this->setMethod($method);
30        } elseif (\func_num_args() >= 3) {
31            \trigger_deprecation('ruflin/elastica', '7.1.3', 'Passing null as 3rd argument to "%s()" is deprecated, pass a string instead. It will be removed in 8.0.', __METHOD__);
32        } else {
33            \trigger_deprecation('ruflin/elastica', '7.1.3', 'Not passing a 3rd argument to "%s()" is deprecated, pass a string instead. It will be removed in 8.0.', __METHOD__);
34        }
35    }
36
37    /**
38     * Set the method for this aggregation.
39     *
40     * @return $this
41     */
42    public function setMethod(string $method): self
43    {
44        return $this->setParam('method', $method);
45    }
46
47    /**
48     * Set the format for this aggregation.
49     *
50     * @return $this
51     */
52    public function setFormat(string $format): self
53    {
54        return $this->setParam('format', $format);
55    }
56
57    /**
58     * @throws InvalidException If buckets path or method are not set
59     */
60    public function toArray(): array
61    {
62        if (!$this->hasParam('buckets_path')) {
63            throw new InvalidException('Buckets path is required');
64        }
65
66        if (!$this->hasParam('method')) {
67            throw new InvalidException('Method parameter is required');
68        }
69
70        return parent::toArray();
71    }
72}
73