Passed
Pull Request — master (#38)
by Teye
05:43
created

ProtobufInputFormat::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\InputFormats;
5
6
class ProtobufInputFormat implements InputFormatInterface
7
{
8
    /**
9
     * @var array<string,string>
10
     */
11
    protected array $protoBytesDecoder;
12
13
    protected ?FlattenSpec $flattenSpec;
14
15
    /**
16
     *
17
     * @param array<string,string> $protoBytesDecoder Specifies how to decode bytes to Protobuf record. See below for
18
     *                                                an example.
19
     * @param FlattenSpec|null     $flattenSpec       Define a flattenSpec to extract nested values from a Parquet
20
     *                                                file. Note that only 'path' expression are supported ('jq' is
21
     *                                                unavailable).
22
     *
23
     * Example $protoBytesDecoder value:
24
     * ```
25
     * [
26
     *     "type" => "file",
27
     *     "descriptor" => "file:///tmp/metrics.desc",
28
     *     "protoMessageType" => "Metrics"
29
     * ]
30
     * ```
31
     *
32
     * @see https://druid.apache.org/docs/latest/ingestion/data-formats.html#protobuf
33
     */
34 1
    public function __construct(array $protoBytesDecoder, FlattenSpec $flattenSpec = null)
35
    {
36 1
        $this->protoBytesDecoder = $protoBytesDecoder;
37 1
        $this->flattenSpec       = $flattenSpec;
38
    }
39
40
    /**
41
     * Return the ProtobufInputFormat so that it can be used in a druid query.
42
     *
43
     * @return array<string,string|array<string,bool|array<array<string,string>>>|string[]>
44
     */
45 1
    public function toArray(): array
46
    {
47 1
        $result = [
48
            'type'              => 'protobuf',
49 1
            'protoBytesDecoder' => $this->protoBytesDecoder,
50
        ];
51
52 1
        if ($this->flattenSpec) {
53 1
            $result['flattenSpec'] = $this->flattenSpec->toArray();
54
        }
55
56 1
        return $result;
57
    }
58
}