1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation\Metric; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
15
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; |
16
|
|
|
use ONGR\ElasticsearchDSL\ScriptAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class representing Percentile Ranks Aggregation. |
20
|
|
|
* |
21
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-rank-aggregation.html |
22
|
|
|
*/ |
23
|
|
|
class PercentileRanksAggregation extends AbstractAggregation |
24
|
|
|
{ |
25
|
|
|
use MetricTrait; |
26
|
|
|
use ScriptAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $values; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $compression; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Inner aggregations container init. |
40
|
|
|
* |
41
|
|
|
* @param string $name |
42
|
|
|
* @param string $field |
43
|
|
|
* @param array $values |
44
|
|
|
* @param string $script |
45
|
|
|
* @param int $compression |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function __construct($name, $field = null, $values = null, $script = null, $compression = null) |
48
|
|
|
{ |
49
|
|
|
parent::__construct($name); |
50
|
|
|
|
51
|
|
|
$this->setField($field); |
52
|
|
|
$this->setValues($values); |
|
|
|
|
53
|
|
|
$this->setScript($script); |
54
|
|
|
$this->setCompression($compression); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getValues() |
61
|
|
|
{ |
62
|
|
|
return $this->values; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $values |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setValues($values) |
71
|
|
|
{ |
72
|
|
|
$this->values = $values; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
public function getCompression() |
81
|
|
|
{ |
82
|
|
|
return $this->compression; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $compression |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setCompression($compression) |
91
|
|
|
{ |
92
|
|
|
$this->compression = $compression; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function getType() |
101
|
|
|
{ |
102
|
|
|
return 'percentile_ranks'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function getArray() |
109
|
|
|
{ |
110
|
|
|
$out = array_filter( |
111
|
|
|
[ |
112
|
|
|
'field' => $this->getField(), |
113
|
|
|
'script' => $this->getScript(), |
114
|
|
|
'values' => $this->getValues(), |
115
|
|
|
'compression' => $this->getCompression(), |
116
|
|
|
], |
117
|
|
|
function ($val) { |
118
|
|
|
return ($val || is_numeric($val)); |
119
|
|
|
} |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$this->isRequiredParametersSet($out); |
123
|
|
|
|
124
|
|
|
return $out; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $a |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
* @throws \LogicException |
132
|
|
|
*/ |
133
|
|
|
private function isRequiredParametersSet($a) |
134
|
|
|
{ |
135
|
|
|
if (array_key_exists('field', $a) && array_key_exists('values', $a) |
136
|
|
|
|| (array_key_exists('script', $a) && array_key_exists('values', $a)) |
137
|
|
|
) { |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
throw new \LogicException('Percentile ranks aggregation must have field and values or script and values set.'); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.