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

MultiRangeParser::withType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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 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 8
    public function __construct(?string $type = null)
25
    {
26 8
        $this->type = $type;
27
    }
28
29 7
    public function withType(?string $type): self
30
    {
31 7
        $new = clone $this;
32 7
        $new->type = $type;
33
34 7
        return $new;
35
    }
36
37 3
    public function asMultiInt(): self
38
    {
39 3
        return $this->withType(Schema::TYPE_INT_MULTIRANGE);
40
    }
41
42 1
    public function asMultiBigInt(): self
43
    {
44 1
        return $this->withType(Schema::TYPE_BIGINT_MULTIRANGE);
45
    }
46
47 1
    public function asMultiNumeric(): self
48
    {
49 1
        return $this->withType(Schema::TYPE_NUM_MULTIRANGE);
50
    }
51
52 1
    public function asMultiDate(): self
53
    {
54 1
        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 1
    public function asCustom(): self
68
    {
69 1
        return $this->withType(null);
70
    }
71
72 8
    public function parse(?string $value): ?array
73
    {
74 8
        if ($value === null) {
75
            return null;
76
        }
77
78 8
        if ($value === '{}') {
79 1
            return [];
80
        }
81
82 7
        if (!preg_match_all('/(([\[\(][^,]*,[^\)\]]*[\)\]]),?)+/U', $value, $matches) || $value !== '{' . implode(',', $matches[1]) . '}') {
83
            throw new InvalidArgumentException('Unsupported range format');
84
        }
85
86 7
        $type = self::RANGES[$this->type] ?? $this->type;
87 7
        $parser = new RangeParser($type);
88
89 7
        return array_map([$parser, 'parse'], $matches[1]);
90
    }
91
92 96
    public static function isAllowedType(string $type): bool
93
    {
94 96
        return isset(self::RANGES[$type]);
95
    }
96
}
97