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
13{
14    const DEFAULT_GAP_POLICY_VALUE = 'insert_zero';
15
16    /**
17     * @param string      $name
18     * @param string|null $bucketsPath
19     */
20    public function __construct(string $name, string $bucketsPath = null)
21    {
22        parent::__construct($name);
23
24        if (null !== $bucketsPath) {
25            $this->setBucketsPath($bucketsPath);
26        }
27    }
28
29    /**
30     * Set the buckets_path for this aggregation.
31     *
32     * @param string $bucketsPath
33     *
34     * @return $this
35     */
36    public function setBucketsPath(string $bucketsPath): self
37    {
38        return $this->setParam('buckets_path', $bucketsPath);
39    }
40
41    /**
42     * Set the lag for this aggregation.
43     *
44     * @param int $lag
45     *
46     * @return $this
47     */
48    public function setLag(int $lag = 1): self
49    {
50        return $this->setParam('lag', $lag);
51    }
52
53    /**
54     * Set the gap policy for this aggregation.
55     *
56     * @param string $gapPolicy
57     *
58     * @return $this
59     */
60    public function setGapPolicy(string $gapPolicy): self
61    {
62        return $this->setParam('gap_policy', $gapPolicy);
63    }
64
65    /**
66     * Set the format for this aggregation.
67     *
68     * @param string $format
69     *
70     * @return $this
71     */
72    public function setFormat(string $format = null): self
73    {
74        return $this->setParam('format', $format);
75    }
76
77    /**
78     * @throws InvalidException If buckets path is not set
79     *
80     * @return array
81     */
82    public function toArray(): array
83    {
84        if (!$this->hasParam('buckets_path')) {
85            throw new InvalidException('Buckets path is required');
86        }
87
88        return parent::toArray();
89    }
90}
91