1<?php
2
3namespace Elastica\Aggregation;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * Class WeightedAvg.
9 *
10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-weight-avg-aggregation.html
11 */
12class WeightedAvg extends AbstractAggregation
13{
14    /**
15     * Set the value for this aggregation.
16     *
17     * @param mixed $missing
18     *
19     * @return $this
20     */
21    public function setValue(string $field, $missing = null)
22    {
23        if ($this->hasParam('value') && isset($this->getParam('value')['script'])) {
24            throw new InvalidException('Weighted Average aggregation with a value mixing field and script is not possible.');
25        }
26
27        $value = ['field' => $field];
28
29        if (null !== $missing) {
30            $value['missing'] = $missing;
31        }
32
33        return $this->setParam('value', $value);
34    }
35
36    /**
37     * Set the value as a script for this aggregation.
38     *
39     * @return $this
40     */
41    public function setValueScript(string $script)
42    {
43        if ($this->hasParam('value') && isset($this->getParam('value')['field'])) {
44            throw new InvalidException('Weighted Average aggregation with a value mixing field and script is not possible.');
45        }
46
47        return $this->setParam('value', ['script' => $script]);
48    }
49
50    /**
51     * Set the weight for this aggregation.
52     *
53     * @param mixed $missing
54     *
55     * @return $this
56     */
57    public function setWeight(string $field, $missing = null)
58    {
59        if ($this->hasParam('weight') && isset($this->getParam('weight')['script'])) {
60            throw new InvalidException('Weighted Average aggregation with a weight mixing field and script is not possible.');
61        }
62
63        $weight = ['field' => $field];
64
65        if (null !== $missing) {
66            $weight['missing'] = $missing;
67        }
68
69        return $this->setParam('weight', $weight);
70    }
71
72    /**
73     * Set the weight as a script for this aggregation.
74     *
75     * @return $this
76     */
77    public function setWeightScript(string $script)
78    {
79        if ($this->hasParam('weight') && isset($this->getParam('weight')['field'])) {
80            throw new InvalidException('Weighted Average aggregation with a weight mixing field and script is not possible.');
81        }
82
83        return $this->setParam('weight', ['script' => $script]);
84    }
85
86    /**
87     * Set the format for this aggregation.
88     *
89     * @param string $format
90     *
91     * @return $this
92     */
93    public function setFormat($format)
94    {
95        return $this->setParam('format', $format);
96    }
97
98    /**
99     * Set the value_type for this aggregation.
100     *
101     * @param mixed $valueType
102     *
103     * @return $this
104     */
105    public function setValueType($valueType)
106    {
107        return $this->setParam('value_type', $valueType);
108    }
109}
110