Completed
Push — 2.x ( 6c47ee...c394f3 )
by Aleksei
24s queued 15s
created

PostgresJsonExpression::getPathArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
23
     *
24
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
39
     *
40
     * @return int|non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|non-empty-string at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in int|non-empty-string.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
60
     *
61
     * @return array<non-empty-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string>.
Loading history...
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