@@ 20-112 (lines=93) @@ | ||
17 | /** |
|
18 | * Difference values counter. |
|
19 | */ |
|
20 | class CardinalityAggregation extends AbstractAggregation |
|
21 | { |
|
22 | use MetricTrait; |
|
23 | use ScriptAwareTrait; |
|
24 | ||
25 | /** |
|
26 | * @var int |
|
27 | */ |
|
28 | private $precisionThreshold; |
|
29 | ||
30 | /** |
|
31 | * @var bool |
|
32 | */ |
|
33 | private $rehash; |
|
34 | ||
35 | /** |
|
36 | * {@inheritdoc} |
|
37 | */ |
|
38 | public function getArray() |
|
39 | { |
|
40 | $out = array_filter( |
|
41 | [ |
|
42 | 'field' => $this->getField(), |
|
43 | 'script' => $this->getScript(), |
|
44 | 'precision_threshold' => $this->getPrecisionThreshold(), |
|
45 | 'rehash' => $this->isRehash(), |
|
46 | ], |
|
47 | function ($val) { |
|
48 | return ($val || is_bool($val)); |
|
49 | } |
|
50 | ); |
|
51 | ||
52 | $this->checkRequiredFields($out); |
|
53 | ||
54 | return $out; |
|
55 | } |
|
56 | ||
57 | /** |
|
58 | * Precision threshold. |
|
59 | * |
|
60 | * @param int $precision Precision Threshold. |
|
61 | */ |
|
62 | public function setPrecisionThreshold($precision) |
|
63 | { |
|
64 | $this->precisionThreshold = $precision; |
|
65 | } |
|
66 | ||
67 | /** |
|
68 | * @return int |
|
69 | */ |
|
70 | public function getPrecisionThreshold() |
|
71 | { |
|
72 | return $this->precisionThreshold; |
|
73 | } |
|
74 | ||
75 | /** |
|
76 | * @return bool |
|
77 | */ |
|
78 | public function isRehash() |
|
79 | { |
|
80 | return $this->rehash; |
|
81 | } |
|
82 | ||
83 | /** |
|
84 | * @param bool $rehash |
|
85 | */ |
|
86 | public function setRehash($rehash) |
|
87 | { |
|
88 | $this->rehash = $rehash; |
|
89 | } |
|
90 | ||
91 | /** |
|
92 | * {@inheritdoc} |
|
93 | */ |
|
94 | public function getType() |
|
95 | { |
|
96 | return 'cardinality'; |
|
97 | } |
|
98 | ||
99 | /** |
|
100 | * Checks if required fields are set. |
|
101 | * |
|
102 | * @param array $fields |
|
103 | * |
|
104 | * @throws \LogicException |
|
105 | */ |
|
106 | private function checkRequiredFields($fields) |
|
107 | { |
|
108 | if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) { |
|
109 | throw new \LogicException('Cardinality aggregation must have field or script set.'); |
|
110 | } |
|
111 | } |
|
112 | } |
|
113 |
@@ 20-108 (lines=89) @@ | ||
17 | /** |
|
18 | * Class representing PercentilesAggregation. |
|
19 | */ |
|
20 | class PercentilesAggregation extends AbstractAggregation |
|
21 | { |
|
22 | use MetricTrait; |
|
23 | use ScriptAwareTrait; |
|
24 | ||
25 | /** |
|
26 | * @var array |
|
27 | */ |
|
28 | private $percents; |
|
29 | ||
30 | /** |
|
31 | * @var int |
|
32 | */ |
|
33 | private $compression; |
|
34 | ||
35 | /** |
|
36 | * @return array |
|
37 | */ |
|
38 | public function getPercents() |
|
39 | { |
|
40 | return $this->percents; |
|
41 | } |
|
42 | ||
43 | /** |
|
44 | * @param array $percents |
|
45 | */ |
|
46 | public function setPercents($percents) |
|
47 | { |
|
48 | $this->percents = $percents; |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @return int |
|
53 | */ |
|
54 | public function getCompression() |
|
55 | { |
|
56 | return $this->compression; |
|
57 | } |
|
58 | ||
59 | /** |
|
60 | * @param int $compression |
|
61 | */ |
|
62 | public function setCompression($compression) |
|
63 | { |
|
64 | $this->compression = $compression; |
|
65 | } |
|
66 | ||
67 | /** |
|
68 | * {@inheritdoc} |
|
69 | */ |
|
70 | public function getType() |
|
71 | { |
|
72 | return 'percentiles'; |
|
73 | } |
|
74 | ||
75 | /** |
|
76 | * {@inheritdoc} |
|
77 | */ |
|
78 | public function getArray() |
|
79 | { |
|
80 | $out = array_filter( |
|
81 | [ |
|
82 | 'compression' => $this->getCompression(), |
|
83 | 'percents' => $this->getPercents(), |
|
84 | 'field' => $this->getField(), |
|
85 | 'script' => $this->getScript(), |
|
86 | ], |
|
87 | function ($val) { |
|
88 | return ($val || is_numeric($val)); |
|
89 | } |
|
90 | ); |
|
91 | ||
92 | $this->isRequiredParametersSet($out); |
|
93 | ||
94 | return $out; |
|
95 | } |
|
96 | ||
97 | /** |
|
98 | * @param array $a |
|
99 | * |
|
100 | * @throws \LogicException |
|
101 | */ |
|
102 | private function isRequiredParametersSet($a) |
|
103 | { |
|
104 | if (!array_key_exists('field', $a) && !array_key_exists('script', $a)) { |
|
105 | throw new \LogicException('Percentiles aggregation must have field or script set.'); |
|
106 | } |
|
107 | } |
|
108 | } |
|
109 |