Completed
Pull Request — master (#1876)
by romain
02:52
created

PercentilesBucket::setKeyed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Elastica\Aggregation;
4
5
use Elastica\Exception\InvalidException;
6
7
/**
8
 * Class PercentilesBucket.
9
 *
10
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-percentiles-bucket-aggregation.html
11
 */
12
class PercentilesBucket extends AbstractAggregation
13
{
14
    use KeyedTrait;
15
16
    /**
17
     * @param string      $name        the name of this aggregation
18
     * @param string|null $bucketsPath the field on which to perform this aggregation
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
     * @throws InvalidException If buckets path or script is not set
31
     */
32
    public function toArray(): array
33
    {
34
        if (!$this->hasParam('buckets_path')) {
35
            throw new InvalidException('Buckets path is required');
36
        }
37
38
        return parent::toArray();
39
    }
40
41
    /**
42
     * Set the buckets_path for this aggregation.
43
     */
44
    public function setBucketsPath(string $bucketsPath): self
45
    {
46
        return $this->setParam('buckets_path', $bucketsPath);
47
    }
48
49
    /**
50
     * Set the gap policy for this aggregation.
51
     */
52
    public function setGapPolicy(string $gapPolicy): self
53
    {
54
        return $this->setParam('gap_policy', $gapPolicy);
55
    }
56
57
    /**
58
     * Set the format for this aggregation.
59
     */
60
    public function setFormat(string $format): self
61
    {
62
        return $this->setParam('format', $format);
63
    }
64
65
    /**
66
     * Set which percents must be returned.
67
     *
68
     * @param float[] $percents
69
     */
70
    public function setPercents(array $percents): self
71
    {
72
        return $this->setParam('percents', $percents);
73
    }
74
}
75