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
|
|
|
$path = $this->getPathArray($statement); |
45
|
|
|
if ($path === []) { |
46
|
|
|
throw new DriverException('Invalid statement. Unable to extract attribute.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$attribute = \array_pop($path); |
50
|
|
|
|
51
|
|
|
return \filter_var($attribute, FILTER_VALIDATE_INT) |
52
|
|
|
? (int) $attribute |
53
|
|
|
: $attribute; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns array of the compiled quoted path and attribute without the field name. |
58
|
|
|
* |
59
|
|
|
* @param non-empty-string $statement |
|
|
|
|
60
|
|
|
* |
61
|
|
|
* @return array<non-empty-string> |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
private function getPathArray(string $statement, string $quote = "'"): array |
64
|
|
|
{ |
65
|
|
|
$path = \explode('->', $statement); |
66
|
|
|
\array_shift($path); // remove field name (first element) |
67
|
|
|
|
68
|
|
|
$result = []; |
69
|
|
|
foreach ($path as $pathAttribute) { |
70
|
|
|
$parsedAttributes = $this->parseArraySyntax($pathAttribute); |
71
|
|
|
foreach ($parsedAttributes as $attribute) { |
72
|
|
|
$result[] = \filter_var($attribute, FILTER_VALIDATE_INT) !== false |
73
|
|
|
? $attribute |
74
|
|
|
: $quote . $attribute . $quote; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|