<?php 

namespace  OfficeBrain\Bundle\CoreBundle\DoctrineExtensions\DqlFunction;

use Doctrine\ORM\Query\AST\Functions\FunctionNode,
    Doctrine\ORM\Query\Lexer;

class Round extends FunctionNode {

	protected $amount;
    protected $precision;

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {

        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

       	$this->amount = $parser->ArithmeticPrimary(); 

        $parser->match(Lexer::T_COMMA);

        $this->precision = $parser->ArithmeticPrimary();

        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
    	return sprintf( ' ROUND( '.$this->amount->dispatch($sqlWalker). '::numeric , '. $this->precision->dispatch($sqlWalker).') ');
    }
}