Passed
Pull Request — master (#307)
by
unknown
03:26
created

MultiRangeParser::asMultiBigInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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 ltrim;
10
use function preg_match_all;
11
use function rtrim;
12
13
final class MultiRangeParser
14
{
15
    private const RANGES = [
16
        Schema::TYPE_INT_MULTIRANGE => Schema::TYPE_INT_RANGE,
17
        Schema::TYPE_BIGINT_MULTIRANGE => Schema::TYPE_BIGINT_RANGE,
18
        Schema::TYPE_NUM_MULTIRANGE => Schema::TYPE_NUM_RANGE,
19
        Schema::TYPE_DATE_MULTIRANGE => Schema::TYPE_DATE_RANGE,
20
        Schema::TYPE_TS_MULTIRANGE => Schema::TYPE_TS_RANGE,
21
        Schema::TYPE_TS_TZ_MULTIRANGE => Schema::TYPE_TS_TZ_RANGE,
22
    ];
23
24
    private string $type;
25
26 5
    public function __construct(string $type)
27
    {
28 5
        if (self::isAllowedType($type)) {
29 5
            $this->type = $type;
30
        } else {
31
            throw new InvalidArgumentException('Unsupported range type "' . $type . '"');
32
        }
33
    }
34
35
    public function withType(string $type): self
36
    {
37
        if (!self::isAllowedType($type)) {
38
            throw new InvalidArgumentException('Unsupported range type "' . $type . '"');
39
        }
40
41
        $new = clone $this;
42
        $new->type = $type;
43
44
        return $new;
45
    }
46
47
    public function asMultiInt(): self
48
    {
49
        return $this->withType(Schema::TYPE_INT_MULTIRANGE);
50
    }
51
52
    public function asMultiBigInt(): self
53
    {
54
        return $this->withType(Schema::TYPE_BIGINT_MULTIRANGE);
55
    }
56
57
    public function asMultiNumeric(): self
58
    {
59
        return $this->withType(Schema::TYPE_NUM_MULTIRANGE);
60
    }
61
62
    public function asMultiDate(): self
63
    {
64
        return $this->withType(Schema::TYPE_DATE_MULTIRANGE);
65
    }
66
67
    public function asMultiTimestamp(): self
68
    {
69
        return $this->withType(Schema::TYPE_TS_MULTIRANGE);
70
    }
71
72
    public function asMultiTimestampTz(): self
73
    {
74
        return $this->withType(Schema::TYPE_TS_TZ_MULTIRANGE);
75
    }
76
77 5
    public function parse(?string $value): ?array
78
    {
79 5
        if ($value === null) {
80
            return null;
81
        }
82
83 5
        $trim = ltrim(rtrim($value, '}'), '{');
84
85 5
        if ($trim === '') {
86 1
            return [];
87
        }
88
89 4
        if (!preg_match_all('/([\[\(][^,]*,[^\)\]]*[\)\]])/', $value, $matches)) {
90
            throw new InvalidArgumentException('Unsupported range format');
91
        }
92
93 4
        $parser = new RangeParser(self::RANGES[$this->type]);
94
95 4
        return array_map([$parser, 'parse'], $matches[0]);
96
    }
97
98 100
    public static function isAllowedType(string $type): bool
99
    {
100 100
        return isset(self::RANGES[$type]);
101
    }
102
}
103