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