Passed
Push — 2.x ( 2e04ba...406928 )
by Aleksei
17:53
created

PostgresJsonExpression::findAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 12
rs 10
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
        $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
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...
56
     *
57
     * @return int|non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|non-empty-string|null at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in int|non-empty-string|null.
Loading history...
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
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...
77
     *
78
     * @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...
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