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

FlattenSpec::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\InputFormats;
5
6
use InvalidArgumentException;
7
use Level23\Druid\Types\FlattenFieldType;
8
9
class FlattenSpec
10
{
11
    protected bool $useFieldDiscovery = true;
12
13
    /**
14
     * @var array<array<string,string>>
15
     */
16
    protected array $fields = [];
17
18 14
    public function __construct(bool $useFieldDiscovery = true)
19
    {
20 14
        $this->useFieldDiscovery = $useFieldDiscovery;
21
    }
22
23
    /**
24
     * @param string      $flattenFieldType One of the FlattenFieldType::* constant.s
25
     *                                      Valid options are:
26
     *                                      root, referring to a field at the root level of the record. Only really
27
     *                                      useful if useFieldDiscovery is false. path, referring to a field using
28
     *                                      JsonPath notation. Supported by most data formats that offer nesting,
29
     *                                      including avro, json, orc, and parquet. jq, referring to a field using
30
     *                                      jackson-jq notation. Only supported for the json format.
31
     * @param string      $name             Name of the field after flattening. This name can be referred to by the
32
     *                                      timestampSpec, transformSpec, dimensionsSpec, and metricsSpec.
33
     * @param string|null $expr             Expression for accessing the field while flattening. For type path, this
34
     *                                      should be JsonPath. For type jq, this should be jackson-jq notation. For
35
     *                                      other types, this parameter is ignored.
36
     *
37
     * @return $this
38
     */
39 14
    public function field(string $flattenFieldType, string $name, string $expr = null): self
40
    {
41 14
        $type = FlattenFieldType::validate($flattenFieldType);
42
43 13
        if (($type == FlattenFieldType::JQ || $type == FlattenFieldType::PATH) && empty($expr)) {
44 2
            throw new InvalidArgumentException('For type JQ or PATH, you need to specify the expression!');
45
        }
46 11
        $field = [
47
            'type' => $type,
48
            'name' => $name,
49
        ];
50
51 11
        if (!empty($expr)) {
52 7
            $field['expr'] = $expr;
53
        }
54
55 11
        $this->fields[] = $field;
56
57 11
        return $this;
58
    }
59
60
    /**
61
     * Return the FlattenSpec so that it can be used in a druid query.
62
     *
63
     * @return array<string,bool|array<array<string,string>>>
64
     */
65 7
    public function toArray(): array
66
    {
67
        return [
68 7
            'useFieldDiscovery' => $this->useFieldDiscovery,
69 7
            'fields'            => $this->fields,
70
        ];
71
    }
72
}