VirtualColumn::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\VirtualColumns;
5
6
use Level23\Druid\Types\DataType;
7
8
class VirtualColumn implements VirtualColumnInterface
9
{
10
    protected string $name;
11
12
    protected string $expression;
13
14
    protected DataType $outputType;
15
16
    /**
17
     * VirtualColumn constructor.
18
     *
19
     * @param string $expression An druid expression
20
     * @param string $as
21
     * @param string|DataType $outputType
22
     *
23
     * @see https://druid.apache.org/docs/latest/misc/math-expr.html
24
     */
25 23
    public function __construct(string $expression, string $as, string|DataType $outputType = DataType::FLOAT)
26
    {
27 23
        $this->name       = $as;
28 23
        $this->expression = $expression;
29 23
        $this->outputType = is_string($outputType) ? DataType::from(strtolower($outputType)) : $outputType;
30
    }
31
32
    /**
33
     * Return the virtual column as it can be used in a druid query.
34
     *
35
     * @return array<string,string>
36
     */
37 8
    public function toArray(): array
38
    {
39 8
        return [
40 8
            'type'       => 'expression',
41 8
            'name'       => $this->name,
42 8
            'expression' => $this->expression,
43 8
            'outputType' => $this->outputType->value,
44 8
        ];
45
    }
46
}
47