BetweenFilter::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Filters;
5
6
use Level23\Druid\Types\DataType;
7
8
/**
9
 * Class BetweenFilter
10
 *
11
 * This filter will create a filter where the given dimension is greater than or equal to the given minValue, and
12
 * less than or equal to the given $maxValue.
13
 *
14
 * So in SQL syntax, this would be:
15
 * ```
16
 * WHERE dimension => $minValue AND dimension <= $maxValue
17
 * ```
18
 *
19
 * @package Level23\Druid\Filters
20
 */
21
class BetweenFilter implements FilterInterface
22
{
23
    protected string $column;
24
25
    protected int|string|float $minValue;
26
27
    protected int|string|float $maxValue;
28
29
    protected DataType $valueType;
30
31
    /**
32
     * BetweenFilter constructor.
33
     *
34
     * @param string           $column    The dimension to filter on
35
     * @param int|string|float $minValue
36
     * @param int|string|float $maxValue
37
     * @param DataType|null    $valueType String specifying the type of bounds to match. The valueType determines how
38
     *                                    Druid interprets the matchValue to assist in converting to the type of the
39
     *                                    matched column and also defines the type of comparison used when matching
40
     *                                    values.
41
     */
42 7
    public function __construct(
43
        string $column,
44
        int|float|string $minValue,
45
        int|float|string $maxValue,
46
        ?DataType $valueType = null
47
    ) {
48 7
        if (is_null($valueType)) {
49
50 3
            if (is_int($minValue)) {
51 1
                $valueType = DataType::LONG;
52 2
            } elseif (is_float($minValue)) {
53 1
                $valueType = DataType::DOUBLE;
54
            } else {
55 1
                $valueType = DataType::STRING;
56
            }
57
        }
58
59 7
        $this->column    = $column;
60 7
        $this->valueType = $valueType;
61 7
        $this->minValue  = $minValue;
62 7
        $this->maxValue  = $maxValue;
63
    }
64
65
    /**
66
     * Return the filter as it can be used in the druid query.
67
     *
68
     * @return array<string,string|bool|float>
69
     */
70 7
    public function toArray(): array
71
    {
72 7
        return [
73 7
            'type'           => 'range',
74 7
            'column'         => $this->column,
75 7
            'matchValueType' => $this->valueType->value,
76 7
            'lower'          => $this->minValue,
77 7
            'lowerOpen'      => false, // >=
78 7
            'upper'          => $this->maxValue,
79 7
            'upperOpen'      => true, // <
80 7
        ];
81
    }
82
}