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 = null; |
25
|
|
|
|
26
|
|
|
public function __construct(?string $type = null) |
27
|
|
|
{ |
28
|
|
|
$this->type = $type; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function withType(?string $type): self |
32
|
|
|
{ |
33
|
|
|
$new = clone $this; |
34
|
|
|
$new->type = $type; |
35
|
|
|
|
36
|
|
|
return $new; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function asMultiInt(): self |
40
|
|
|
{ |
41
|
|
|
return $this->withType(Schema::TYPE_INT_MULTIRANGE); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function asMultiBigInt(): self |
45
|
|
|
{ |
46
|
|
|
return $this->withType(Schema::TYPE_BIGINT_MULTIRANGE); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function asMultiNumeric(): self |
50
|
|
|
{ |
51
|
|
|
return $this->withType(Schema::TYPE_NUM_MULTIRANGE); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function asMultiDate(): self |
55
|
|
|
{ |
56
|
|
|
return $this->withType(Schema::TYPE_DATE_MULTIRANGE); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function asMultiTimestamp(): self |
60
|
|
|
{ |
61
|
|
|
return $this->withType(Schema::TYPE_TS_MULTIRANGE); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function asMultiTimestampTz(): self |
65
|
|
|
{ |
66
|
|
|
return $this->withType(Schema::TYPE_TS_TZ_MULTIRANGE); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function asCustom(): self |
70
|
|
|
{ |
71
|
|
|
return $this->withType(null); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function parse(?string $value): ?array |
75
|
|
|
{ |
76
|
|
|
if ($value === null) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$trim = ltrim(rtrim($value, '}'), '{'); |
81
|
|
|
|
82
|
|
|
if ($trim === '') { |
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!preg_match_all('/([\[\(][^,]*,[^\)\]]*[\)\]])/', $trim, $matches)) { |
87
|
|
|
throw new InvalidArgumentException('Unsupported range format'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$woMatches = str_replace($matches[0], '', $trim); |
91
|
|
|
$woCommas = str_replace(',', '', $woMatches); |
92
|
|
|
|
93
|
|
|
if ($woCommas !== '') { |
94
|
|
|
throw new InvalidArgumentException('Unsupported range format'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$type = self::RANGES[$this->type] ?? $this->type; |
98
|
|
|
$parser = new RangeParser($type); |
99
|
|
|
|
100
|
|
|
return array_map([$parser, 'parse'], $matches[0]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public static function isAllowedType(string $type): bool |
104
|
|
|
{ |
105
|
|
|
return isset(self::RANGES[$type]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|