1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Level23\Druid\Filters; |
5
|
|
|
|
6
|
|
|
use Level23\Druid\Types\SortingOrder; |
7
|
|
|
use Level23\Druid\Types\BoundOperator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class BoundFilter |
11
|
|
|
* |
12
|
|
|
* Bound filters can be used to filter on ranges of dimension values. It can be used for comparison filtering like |
13
|
|
|
* greater than, less than, greater than or equal to and less than or equal to. |
14
|
|
|
* |
15
|
|
|
* @package Level23\Druid\Filters |
16
|
|
|
*/ |
17
|
|
|
class BoundFilter implements FilterInterface |
18
|
|
|
{ |
19
|
|
|
protected string $dimension; |
20
|
|
|
|
21
|
|
|
protected BoundOperator $operator; |
22
|
|
|
|
23
|
|
|
protected string $value; |
24
|
|
|
|
25
|
|
|
protected SortingOrder $ordering; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* BoundFilter constructor. |
29
|
|
|
* |
30
|
|
|
* @param string $dimension The dimension to filter on |
31
|
|
|
* @param string|BoundOperator $operator The operator to use. Use ">", ">=", "<", or "<=" Or use the |
32
|
|
|
* BoundOperator constants. |
33
|
|
|
* @param string $value The value to compare with. This can either be a numeric or a |
34
|
|
|
* string. |
35
|
|
|
* @param string|SortingOrder|null $ordering Specifies the sorting order using when comparing values |
36
|
|
|
* against the bound. |
37
|
|
|
*/ |
38
|
49 |
|
public function __construct( |
39
|
|
|
string $dimension, |
40
|
|
|
string|BoundOperator $operator, |
41
|
|
|
string $value, |
42
|
|
|
string|SortingOrder $ordering = null |
43
|
|
|
) { |
44
|
49 |
|
if(is_string($ordering)) { |
45
|
40 |
|
$ordering = SortingOrder::from(strtolower($ordering)); |
46
|
|
|
} |
47
|
|
|
|
48
|
49 |
|
$this->dimension = $dimension; |
49
|
49 |
|
$this->operator = is_string($operator) ? BoundOperator::from($operator) : $operator; |
50
|
48 |
|
$this->value = $value; |
51
|
48 |
|
$this->ordering = $ordering ?? (is_numeric($value) ? SortingOrder::NUMERIC : SortingOrder::LEXICOGRAPHIC); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return the filter as it can be used in the druid query. |
56
|
|
|
* |
57
|
|
|
* @return array<string,string|bool|array<string,string|int|bool|array<mixed>>> |
58
|
|
|
*/ |
59
|
48 |
|
public function toArray(): array |
60
|
|
|
{ |
61
|
48 |
|
$result = [ |
62
|
48 |
|
'type' => 'bound', |
63
|
48 |
|
'dimension' => $this->dimension, |
64
|
48 |
|
'ordering' => $this->ordering->value, |
65
|
48 |
|
]; |
66
|
|
|
|
67
|
48 |
|
switch ($this->operator) { |
68
|
48 |
|
case BoundOperator::GE: |
69
|
12 |
|
$result['lower'] = $this->value; |
70
|
12 |
|
$result['lowerStrict'] = false; |
71
|
12 |
|
break; |
72
|
36 |
|
case BoundOperator::GT: |
73
|
12 |
|
$result['lower'] = $this->value; |
74
|
12 |
|
$result['lowerStrict'] = true; |
75
|
12 |
|
break; |
76
|
24 |
|
case BoundOperator::LE: |
77
|
12 |
|
$result['upper'] = $this->value; |
78
|
12 |
|
$result['upperStrict'] = false; |
79
|
12 |
|
break; |
80
|
12 |
|
case BoundOperator::LT: |
81
|
12 |
|
$result['upper'] = $this->value; |
82
|
12 |
|
$result['upperStrict'] = true; |
83
|
12 |
|
break; |
84
|
|
|
} |
85
|
|
|
|
86
|
48 |
|
return $result; |
87
|
|
|
} |
88
|
|
|
} |