|
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\Exception\DriverException; |
|
15
|
|
|
use Cycle\Database\Injection\JsonExpression; |
|
16
|
|
|
|
|
17
|
|
|
abstract class PostgresJsonExpression extends JsonExpression |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Returns the compiled quoted path without the field name and attribute. |
|
21
|
|
|
* |
|
22
|
|
|
* @param non-empty-string $statement |
|
|
|
|
|
|
23
|
|
|
* |
|
24
|
|
|
* @return non-empty-string |
|
|
|
|
|
|
25
|
|
|
*/ |
|
26
|
|
|
protected function getPath(string $statement, string $quote = "'"): string |
|
27
|
|
|
{ |
|
28
|
|
|
$path = $this->getPathArray($statement, $quote); |
|
29
|
|
|
|
|
30
|
|
|
\array_pop($path); |
|
31
|
|
|
|
|
32
|
|
|
return \implode('->', $path); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns the attribute (last part of the full path). |
|
37
|
|
|
* |
|
38
|
|
|
* @param non-empty-string $statement |
|
|
|
|
|
|
39
|
|
|
* |
|
40
|
|
|
* @return int|non-empty-string |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
protected function getAttribute(string $statement): string|int |
|
43
|
|
|
{ |
|
44
|
|
|
$attribute = $this->findAttribute($statement); |
|
45
|
|
|
if ($attribute === null) { |
|
46
|
|
|
throw new DriverException('Invalid statement. Unable to extract attribute.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $attribute; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns the attribute (last part of the full path). Returns null if the attribute is not found. |
|
54
|
|
|
* |
|
55
|
|
|
* @param non-empty-string $statement |
|
|
|
|
|
|
56
|
|
|
* |
|
57
|
|
|
* @return int|non-empty-string|null |
|
|
|
|
|
|
58
|
|
|
*/ |
|
59
|
|
|
protected function findAttribute(string $statement): string|int|null |
|
60
|
|
|
{ |
|
61
|
|
|
$path = $this->getPathArray($statement); |
|
62
|
|
|
if ($path === []) { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$attribute = \array_pop($path); |
|
67
|
|
|
|
|
68
|
|
|
return \filter_var($attribute, FILTER_VALIDATE_INT) |
|
69
|
|
|
? (int) $attribute |
|
70
|
|
|
: $attribute; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns array of the compiled quoted path and attribute without the field name. |
|
75
|
|
|
* |
|
76
|
|
|
* @param non-empty-string $statement |
|
|
|
|
|
|
77
|
|
|
* |
|
78
|
|
|
* @return array<non-empty-string> |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
|
|
private function getPathArray(string $statement, string $quote = "'"): array |
|
81
|
|
|
{ |
|
82
|
|
|
$path = \explode('->', $statement); |
|
83
|
|
|
\array_shift($path); // remove field name (first element) |
|
84
|
|
|
|
|
85
|
|
|
$result = []; |
|
86
|
|
|
foreach ($path as $pathAttribute) { |
|
87
|
|
|
$parsedAttributes = $this->parseArraySyntax($pathAttribute); |
|
88
|
|
|
foreach ($parsedAttributes as $attribute) { |
|
89
|
|
|
$result[] = \filter_var($attribute, FILTER_VALIDATE_INT) !== false |
|
90
|
|
|
? $attribute |
|
91
|
|
|
: $quote . $attribute . $quote; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $result; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|