DX: Add a DQL AGE function to calculate age between two dates

This commit is contained in:
2022-10-12 11:03:05 +02:00
parent 6f1ec00d3a
commit 3a8640db43
3 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?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\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class Age extends FunctionNode
{
private $value1;
private $value2;
public function getSql(SqlWalker $sqlWalker)
{
if (null !== $this->value2) {
return sprintf(
'AGE(%s, %s)',
$this->value1->dispatch($sqlWalker),
$this->value2->dispatch($sqlWalker)
);
}
return sprintf(
'AGE(%s)',
$this->value1->dispatch($sqlWalker),
);
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->value1 = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->value2 = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}