Completed
Push — master ( f8a88d...dd8807 )
by Nicolas
02:45
created

NormalizeAggregation::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 3
1
<?php
2
3
namespace Elastica\Aggregation;
4
5
use 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
 */
12
class NormalizeAggregation extends AbstractAggregation
13
{
14
    public function __construct(string $name, ?string $bucketsPath = null, ?string $method = null)
15
    {
16
        parent::__construct($name);
17
18
        if (null !== $bucketsPath) {
19
            $this->setBucketsPath($bucketsPath);
20
        }
21
22
        if (null !== $method) {
23
            $this->setMethod($method);
24
        }
25
    }
26
27
    /**
28
     * Set the buckets_path for this aggregation.
29
     *
30
     * @return $this
31
     */
32
    public function setBucketsPath(string $bucketsPath): self
33
    {
34
        return $this->setParam('buckets_path', $bucketsPath);
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