Issues (52)

src/Dimensions/LookupDimension.php (1 issue)

Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Dimensions;
5
6
class LookupDimension implements DimensionInterface
7
{
8
    protected string $dimension;
9
10
    protected string $outputName;
11
12
    /**
13
     * @var string|array<int|string,string>
14
     */
15
    protected string|array $registeredLookupFunctionOrMap;
16
17
    /**
18
     * @var bool|string
19
     */
20
    protected string|bool $keepMissingValue;
21
22
    protected bool $isOneToOne;
23
24
    /**
25
     * DefaultDimension constructor.
26
     *
27
     * A property of retainMissingValue and replaceMissingValueWith can be specified at query
28
     * time to hint how to handle missing values. Setting replaceMissingValueWith to "" has the
29
     * same effect as setting it to null or omitting the property. Setting replaceMissingValue to
30
     * true will use the dimension's original value if it is not found in the lookup. If you set
31
     * it to a string it wil replace the missing value with that string. It defaults to false
32
     * which will ignore the value.
33
     *
34
     * @param string                          $dimension
35
     * @param string|array<int|string,string> $registeredLookupFunctionOrMap
36
     * @param string                          $outputName
37
     * @param bool|string                     $keepMissingValue
38
     * @param bool                            $isOneToOne
39
     */
40 11
    public function __construct(
41
        string $dimension,
42
        array|string $registeredLookupFunctionOrMap,
43
        string $outputName = '',
44
        bool|string $keepMissingValue = false,
45
        bool $isOneToOne = false
46
    ) {
47 11
        $this->dimension                     = $dimension;
48 11
        $this->outputName                    = $outputName ?: $dimension;
49 11
        $this->registeredLookupFunctionOrMap = $registeredLookupFunctionOrMap;
50 11
        $this->keepMissingValue              = $keepMissingValue;
51 11
        $this->isOneToOne                    = $isOneToOne;
52
    }
53
54
    /**
55
     * Return the dimension as it should be used in a druid query.
56
     *
57
     * @return array<string,string|bool|array<string,string|bool|string[]>>
58
     */
59 6
    public function toArray(): array
60
    {
61 6
        $result = [
62 6
            'type'       => 'lookup',
63 6
            'dimension'  => $this->dimension,
64 6
            'outputName' => $this->outputName,
65 6
        ];
66
67 6
        if (is_string($this->registeredLookupFunctionOrMap)) {
68 3
            $result['name'] = $this->registeredLookupFunctionOrMap;
69 3
        } elseif (is_array($this->registeredLookupFunctionOrMap)) {
0 ignored issues
show
The condition is_array($this->registeredLookupFunctionOrMap) is always true.
Loading history...
70 3
            $result['lookup'] = [
71 3
                'type'       => 'map',
72 3
                'map'        => $this->registeredLookupFunctionOrMap,
73 3
                'isOneToOne' => $this->isOneToOne,
74 3
            ];
75
        }
76
77 6
        if ($this->keepMissingValue === true) {
78 2
            $result['retainMissingValue'] = true;
79 4
        } elseif (is_string($this->keepMissingValue)) {
80 2
            $result['replaceMissingValueWith'] = $this->keepMissingValue;
81
        }
82
83 6
        return $result;
84
    }
85
86
    /**
87
     * Return the name of the dimension which is selected.
88
     *
89
     * @return string
90
     */
91 4
    public function getDimension(): string
92
    {
93 4
        return $this->dimension;
94
    }
95
96
    /**
97
     * Return the output name of this dimension
98
     *
99
     * @return string
100
     */
101 4
    public function getOutputName(): string
102
    {
103 4
        return $this->outputName;
104
    }
105
}