mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-28 13:06:13 +00:00
54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Doctrine\DQL;
|
|
|
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
|
|
use Doctrine\ORM\Query\AST\Node;
|
|
use Doctrine\ORM\Query\Lexer;
|
|
use Doctrine\ORM\Query\Parser;
|
|
use Doctrine\ORM\Query\SqlWalker;
|
|
|
|
/**
|
|
* Return an aggregation of values in a json representation, as a string.
|
|
*
|
|
* Internally, this function use the postgresql `jsonb_agg` function. Using
|
|
* json allow to aggregate data from different types without have to cast them.
|
|
*/
|
|
class JsonBuildObject extends FunctionNode
|
|
{
|
|
/**
|
|
* @var array|Node[]
|
|
*/
|
|
private array $exprs = [];
|
|
|
|
public function getSql(SqlWalker $sqlWalker)
|
|
{
|
|
return 'JSONB_BUILD_OBJECT(' . implode(', ', array_map(static fn (Node $expr) => $expr->dispatch($sqlWalker), $this->exprs)) . ')';
|
|
}
|
|
|
|
public function parse(Parser $parser)
|
|
{
|
|
$lexer = $parser->getLexer();
|
|
$parser->match(Lexer::T_IDENTIFIER);
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS);
|
|
|
|
$this->exprs[] = $parser->ArithmeticPrimary();
|
|
|
|
while (Lexer::T_COMMA === $lexer->lookahead['type']) {
|
|
$parser->match(Lexer::T_COMMA);
|
|
$this->exprs[] = $parser->ArithmeticPrimary();
|
|
}
|
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
|
|
}
|
|
}
|