ProtobufInputFormat::toArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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 1
            'type'              => 'protobuf',
49 1
            'protoBytesDecoder' => $this->protoBytesDecoder,
50 1
        ];
51
52 1
        if ($this->flattenSpec) {
53 1
            $result['flattenSpec'] = $this->flattenSpec->toArray();
54
        }
55
56 1
        return $result;
57
    }
58
}