Passed
Push — main ( 1276f8...8d18b3 )
by Martin
40:19 queued 25:29
created

JsonGetField   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 24
ccs 8
cts 10
cp 0.8
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A customizeFunction() 0 3 1
A feedParserWithNodes() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\Lexer;
8
use Doctrine\ORM\Query\Parser;
9
use Doctrine\ORM\Query\QueryException;
10
use Doctrine\ORM\Query\TokenType;
11
use MartinGeorgiev\Utils\DoctrineOrm;
12
13
/**
14
 * Implementation of PostgreSQL json field retrieval, filtered by key (using ->).
15
 *
16
 * Supports both string keys for object property access and integer indices for array element access:
17
 * - JSON_GET_FIELD(json_column, 'property_name') -> json_column->'property_name'
18
 * - JSON_GET_FIELD(json_column, 0) -> json_column->0
19
 *
20
 * @see https://www.postgresql.org/docs/9.4/static/functions-json.html
21
 * @since 0.1
22
 *
23
 * @author Martin Georgiev <[email protected]>
24
 */
25
class JsonGetField extends BaseFunction
26
{
27 3
    protected function customizeFunction(): void
28
    {
29 3
        $this->setFunctionPrototype('(%s -> %s)');
30
    }
31
32 3
    protected function feedParserWithNodes(Parser $parser): void
33
    {
34 3
        $shouldUseLexer = DoctrineOrm::isPre219();
35
36 3
        $nodeForJsonDocumentName = $parser->StringPrimary();
37 3
        $parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_COMMA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
39
        // Second parameter can be either an index or a property name
40
        try {
41 3
            $nodeForJsonIndexOrPropertyName = $parser->ArithmeticPrimary();
42
        } catch (QueryException) {
43
            // If ArithmeticPrimary fails (e.g., when encountering a property name rather than an index), try StringPrimary
44
            $nodeForJsonIndexOrPropertyName = $parser->StringPrimary();
45
        }
46
47
        /* @phpstan-ignore-next-line assign.propertyType */
48 3
        $this->nodes = [$nodeForJsonDocumentName, $nodeForJsonIndexOrPropertyName];
49
    }
50
}
51