Passed
Push — master ( dcfc52...d45e42 )
by Teye
05:06
created

ArrayContainsFilter::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
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
class ArrayContainsFilter implements FilterInterface
9
{
10
    protected string $column;
11
12
    protected string|int|float|null $value;
13
14
    /**
15
     * @var \Level23\Druid\Types\DataType
16
     */
17
    protected DataType $matchValueType;
18
19
    /**
20
     * Equality Filter constructor.
21
     *
22
     * @param string                             $column         Input column or virtual column name to filter.
23
     * @param string|int|float|null              $value          Array element value to match. This value can be null.
24
     * @param \Level23\Druid\Types\DataType|null $matchValueType The type of value to match. When not given, we will
25
     *                                                           auto-detect the value based on the given value.
26
     */
27 3
    public function __construct(
28
        string $column,
29
        string|int|float|null $value,
30
        DataType $matchValueType = null
31
    ) {
32 3
        if (is_null($matchValueType)) {
33
34 3
            if (is_int($value)) {
35 1
                $matchValueType = DataType::LONG;
36 2
            } elseif (is_float($value)) {
37 1
                $matchValueType = DataType::DOUBLE;
38
            } else {
39 1
                $matchValueType = DataType::STRING;
40
            }
41
        }
42
43 3
        $this->value          = $value;
44 3
        $this->matchValueType = $matchValueType;
45 3
        $this->column         = $column;
46
    }
47
48
    /**
49
     * Return the filter as it can be used in the druid query.
50
     *
51
     * @return array<string, float|int|string|null>
52
     */
53 3
    public function toArray(): array
54
    {
55 3
        return [
56 3
            'type'                  => 'arrayContainsElement',
57 3
            'column'                => $this->column,
58 3
            'elementMatchValueType' => $this->matchValueType->value,
59 3
            'elementMatchValue'     => $this->value,
60 3
        ];
61
    }
62
}