1<?php
2
3namespace Elastica\Aggregation;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * Class SerialDiff.
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html
11 */
12class SerialDiff extends AbstractAggregation implements GapPolicyInterface
13{
14    use Traits\BucketsPathTrait;
15
16    public const DEFAULT_GAP_POLICY_VALUE = GapPolicyInterface::INSERT_ZEROS;
17
18    public function __construct(string $name, ?string $bucketsPath = null)
19    {
20        parent::__construct($name);
21
22        if (null !== $bucketsPath) {
23            $this->setBucketsPath($bucketsPath);
24        } elseif (\func_num_args() >= 2) {
25            \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__);
26        } else {
27            \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__);
28        }
29    }
30
31    /**
32     * Set the lag for this aggregation.
33     *
34     * @return $this
35     */
36    public function setLag(int $lag = 1): self
37    {
38        return $this->setParam('lag', $lag);
39    }
40
41    /**
42     * Set the gap policy for this aggregation.
43     *
44     * @return $this
45     */
46    public function setGapPolicy(string $gapPolicy = self::DEFAULT_GAP_POLICY_VALUE): self
47    {
48        return $this->setParam('gap_policy', $gapPolicy);
49    }
50
51    /**
52     * Set the format for this aggregation.
53     *
54     * @return $this
55     */
56    public function setFormat(?string $format = null): self
57    {
58        return $this->setParam('format', $format);
59    }
60
61    /**
62     * @throws InvalidException If buckets path is not set
63     */
64    public function toArray(): array
65    {
66        if (!$this->hasParam('buckets_path')) {
67            throw new InvalidException('Buckets path is required');
68        }
69
70        return parent::toArray();
71    }
72}
73