Passed
Push — distinct-support ( f15012 )
by Martin
14:53 queued 10s
created

StringAgg::customizeFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\AST\Node;
8
use Doctrine\ORM\Query\Lexer;
9
use Doctrine\ORM\Query\Parser;
10
use Doctrine\ORM\Query\SqlWalker;
11
use Doctrine\ORM\Query\TokenType;
12
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\DistinctableTrait;
13
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\OrderableTrait;
14
use MartinGeorgiev\Utils\DoctrineOrm;
15
16
/**
17
 * Implementation of PostgreSQL STRING_AGG().
18
 *
19
 * @see https://www.postgresql.org/docs/9.5/functions-aggregate.html
20
 * @since 1.4
21
 *
22
 * @author Martin Georgiev <[email protected]>
23
 */
24
class StringAgg extends BaseFunction
25
{
26
    use OrderableTrait;
27
    use DistinctableTrait;
28
29
    private Node $delimiter;
30
31 1
    protected function customizeFunction(): void
32
    {
33 1
        $this->setFunctionPrototype('string_agg(%s%s, %s%s)');
34 1
        $this->addNodeMapping('StringPrimary');
35 1
        $this->addNodeMapping('StringPrimary');
36
    }
37
38 1
    public function parse(Parser $parser): void
39
    {
40 1
        $shouldUseLexer = DoctrineOrm::isPre219();
41
42 1
        $this->customizeFunction();
43
44 1
        $parser->match($shouldUseLexer ? Lexer::T_IDENTIFIER : TokenType::T_IDENTIFIER);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_IDENTIFIER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45 1
        $parser->match($shouldUseLexer ? Lexer::T_OPEN_PARENTHESIS : TokenType::T_OPEN_PARENTHESIS);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
46
47 1
        $this->parseDistinctClause($parser);
48 1
        $this->expression = $parser->StringPrimary();
49
50 1
        $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...
51
52 1
        $this->delimiter = $parser->StringPrimary();
53 1
        $this->parseOrderByClause($parser);
54
55 1
        $parser->match($shouldUseLexer ? Lexer::T_CLOSE_PARENTHESIS : TokenType::T_CLOSE_PARENTHESIS);
0 ignored issues
show
Bug introduced by
The constant Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
    }
57
58 1
    public function getSql(SqlWalker $sqlWalker): string
59
    {
60 1
        $dispatched = [
61 1
            $this->getOptionalDistinctClause(),
62 1
            $this->expression->dispatch($sqlWalker),
63 1
            $this->delimiter->dispatch($sqlWalker),
64 1
            $this->getOptionalOrderByClause($sqlWalker),
65 1
        ];
66
67 1
        return \vsprintf($this->functionPrototype, $dispatched);
68
    }
69
}
70