|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql; |
|
6
|
|
|
|
|
7
|
|
|
use function in_array; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Array representation to PHP array parser for PostgreSQL Server. |
|
11
|
|
|
*/ |
|
12
|
|
|
final class ArrayParser |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var int Current parsing position |
|
16
|
|
|
*/ |
|
17
|
|
|
private int $pos = 0; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $value The parse string from PostgresSQL |
|
21
|
|
|
*/ |
|
22
|
3 |
|
public function __construct( |
|
23
|
|
|
private string $value, |
|
24
|
|
|
) { |
|
25
|
3 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Parses PostgreSQL encoded array. |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function parse(): array |
|
31
|
|
|
{ |
|
32
|
3 |
|
if ($this->value[++$this->pos] === '}') { |
|
33
|
1 |
|
++$this->pos; |
|
34
|
1 |
|
return []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
for ($result = [];; ++$this->pos) { |
|
38
|
3 |
|
$result[] = match ($this->value[$this->pos]) { |
|
39
|
3 |
|
'{' => $this->parse(), |
|
40
|
3 |
|
',', '}' => null, |
|
41
|
3 |
|
default => $this->parseString(), |
|
42
|
3 |
|
}; |
|
43
|
|
|
|
|
44
|
3 |
|
if ($this->value[$this->pos] === '}') { |
|
45
|
3 |
|
++$this->pos; |
|
46
|
3 |
|
return $result; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Parses PostgreSQL encoded string. |
|
53
|
|
|
*/ |
|
54
|
3 |
|
private function parseString(): string|null |
|
55
|
|
|
{ |
|
56
|
3 |
|
return $this->value[$this->pos] === '"' |
|
57
|
2 |
|
? $this->parseQuotedString() |
|
58
|
3 |
|
: $this->parseUnquotedString(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Parses quoted string. |
|
63
|
|
|
* |
|
64
|
|
|
* @psalm-suppress LoopInvalidation |
|
65
|
|
|
*/ |
|
66
|
2 |
|
private function parseQuotedString(): string |
|
67
|
|
|
{ |
|
68
|
2 |
|
for ($result = '', ++$this->pos;; ++$this->pos) { |
|
69
|
2 |
|
if ($this->value[$this->pos] === '\\') { |
|
70
|
2 |
|
++$this->pos; |
|
71
|
2 |
|
} elseif ($this->value[$this->pos] === '"') { |
|
72
|
2 |
|
++$this->pos; |
|
73
|
2 |
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
$result .= $this->value[$this->pos]; |
|
77
|
|
|
} |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Parses unquoted string. |
|
82
|
|
|
*/ |
|
83
|
3 |
|
private function parseUnquotedString(): string|null |
|
84
|
|
|
{ |
|
85
|
3 |
|
for ($result = '';; ++$this->pos) { |
|
86
|
3 |
|
if (in_array($this->value[$this->pos], [',', '}'], true)) { |
|
87
|
3 |
|
return $result !== 'NULL' |
|
88
|
3 |
|
? $result |
|
89
|
3 |
|
: null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
3 |
|
$result .= $this->value[$this->pos]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: