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
|
|
|
|