Test Failed
Pull Request — master (#307)
by
unknown
04:01
created

MultiRangeParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A asMultiBigInt() 0 3 1
A isAllowedType() 0 3 1
A asMultiTimestampTz() 0 3 1
A asMultiDate() 0 3 1
A asMultiNumeric() 0 3 1
A parse() 0 18 5
A asCustom() 0 3 1
A asMultiInt() 0 3 1
A withType() 0 6 1
A asMultiTimestamp() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
6
7
use InvalidArgumentException;
8
use function array_map;
9
use function preg_match_all;
10
11
final class MultiRangeParser
12
{
13
    private const RANGES = [
14
        Schema::TYPE_INT_MULTIRANGE => Schema::TYPE_INT_RANGE,
15
        Schema::TYPE_BIGINT_MULTIRANGE => Schema::TYPE_BIGINT_RANGE,
16
        Schema::TYPE_NUM_MULTIRANGE => Schema::TYPE_NUM_RANGE,
17
        Schema::TYPE_DATE_MULTIRANGE => Schema::TYPE_DATE_RANGE,
18
        Schema::TYPE_TS_MULTIRANGE => Schema::TYPE_TS_RANGE,
19
        Schema::TYPE_TS_TZ_MULTIRANGE => Schema::TYPE_TS_TZ_RANGE,
20
    ];
21
22
    private ?string $type = null;
23
24
    public function __construct(?string $type = null)
25
    {
26
        $this->type = $type;
27
    }
28
29
    public function withType(?string $type): self
30
    {
31
        $new = clone $this;
32
        $new->type = $type;
33
34
        return $new;
35
    }
36
37
    public function asMultiInt(): self
38
    {
39
        return $this->withType(Schema::TYPE_INT_MULTIRANGE);
40
    }
41
42
    public function asMultiBigInt(): self
43
    {
44
        return $this->withType(Schema::TYPE_BIGINT_MULTIRANGE);
45
    }
46
47
    public function asMultiNumeric(): self
48
    {
49
        return $this->withType(Schema::TYPE_NUM_MULTIRANGE);
50
    }
51
52
    public function asMultiDate(): self
53
    {
54
        return $this->withType(Schema::TYPE_DATE_MULTIRANGE);
55
    }
56
57
    public function asMultiTimestamp(): self
58
    {
59
        return $this->withType(Schema::TYPE_TS_MULTIRANGE);
60
    }
61
62
    public function asMultiTimestampTz(): self
63
    {
64
        return $this->withType(Schema::TYPE_TS_TZ_MULTIRANGE);
65
    }
66
67
    public function asCustom(): self
68
    {
69
        return $this->withType(null);
70
    }
71
72
    public function parse(?string $value): ?array
73
    {
74
        if ($value === null) {
75
            return null;
76
        }
77
78
        if ($value === '{}') {
79
            return [];
80
        }
81
82
        if (!preg_match_all('/(([\[\(][^,]*,[^\)\]]*[\)\]]),?)+/U', $value, $matches) || $value !== '{' . implode(',', $matches[1]) . '}') {
83
            throw new InvalidArgumentException('Unsupported range format');
84
        }
85
86
        $type = self::RANGES[$this->type] ?? $this->type;
87
        $parser = new RangeParser($type);
88
89
        return array_map([$parser, 'parse'], $matches[1]);
90
    }
91
92
    public static function isAllowedType(string $type): bool
93
    {
94
        return isset(self::RANGES[$type]);
95
    }
96
}
97