1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Driver\Postgres\Injection; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Injection\JsonExpression; |
15
|
|
|
|
16
|
|
|
abstract class PostgresJsonExpression extends JsonExpression |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param non-empty-string $statement |
|
|
|
|
20
|
|
|
* |
21
|
|
|
* @return non-empty-string |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
protected function getField(string $statement): string |
24
|
|
|
{ |
25
|
|
|
$path = \explode('->', $statement); |
26
|
|
|
|
27
|
|
|
return $this->quoter->quote(\array_shift($path)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param non-empty-string $statement |
|
|
|
|
32
|
|
|
* |
33
|
|
|
* @return array<non-empty-string> |
|
|
|
|
34
|
|
|
*/ |
35
|
|
|
protected function getWrappedPath(string $statement, string $quote = "'"): array |
36
|
|
|
{ |
37
|
|
|
$path = \explode('->', $statement); |
38
|
|
|
\array_shift($path); // remove field name (first element) |
39
|
|
|
|
40
|
|
|
$result = []; |
41
|
|
|
foreach ($path as $pathAttribute) { |
42
|
|
|
$parsedAttributes = $this->parseJsonPathArrayKeys($pathAttribute); |
43
|
|
|
foreach ($parsedAttributes as $attribute) { |
44
|
|
|
$result[] = \filter_var($attribute, FILTER_VALIDATE_INT) !== false |
45
|
|
|
? $attribute |
46
|
|
|
: $quote . $attribute . $quote; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param non-empty-string $attribute |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @return array<non-empty-string> |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
private function parseJsonPathArrayKeys(string $attribute): array |
59
|
|
|
{ |
60
|
|
|
if (\preg_match('/(\[[^\]]+\])+$/', $attribute, $parts)) { |
61
|
|
|
$key = \substr($attribute, 0, \strpos($attribute, $parts[0])); |
62
|
|
|
|
63
|
|
|
\preg_match_all('/\[([^\]]+)\]/', $parts[0], $matches); |
64
|
|
|
$keys = $matches[1]; |
65
|
|
|
|
66
|
|
|
$cleanKeys = \array_values(\array_filter($keys, static fn ($key) => $key !== '')); |
67
|
|
|
|
68
|
|
|
return \array_merge([$key], $cleanKeys); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [$attribute]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|