Passed
Pull Request — 2.x (#135)
by Aleksei
18:00
created

PostgresJsonExpression::getField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
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...
20
     *
21
     * @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...
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
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...
32
     *
33
     * @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...
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