public function compile(Expression $expression, DoctrineQueryBuilderParametersBinder $binder) { $result = ''; if ($expression->getFunction()) { $compiledArgs = array(); foreach ($expression->getFunctionArgs() as $arg) { if ($arg instanceof Expression) { $compiledArgs[] = $this->compile($arg, $binder); } else { $compiledArgs[] = $this->resolveArgument($arg, $binder); } } $result = $expression->getFunction() . '(' . implode(', ', $compiledArgs) . ')'; } else { $result = $this->resolveArgument($expression->getExpression(), $binder); } if ($expression->getAlias()) { $result .= ' AS ' . $expression->getAlias(); } return $result; }
public function testFunctionCallExpressionWithAlias() { $rawExpr = array('function' => 'CONCAT', 'args' => array(':firstname', array('function' => 'CONCAT', 'args' => array(' ', ':lastname')))); $expr = new Expression($rawExpr, 'fullname'); $this->assertEquals('fullname', $expr->getAlias()); $this->assertSame($rawExpr, $expr->getExpression()); $this->assertEquals($rawExpr['function'], $expr->getFunction()); $this->assertTrue(is_array($expr->getFunctionArgs())); $args1 = $expr->getFunctionArgs(); $this->assertEquals(2, count($args1)); $this->assertEquals(':firstname', $args1[0]); $this->assertInstanceOf(Expression::clazz(), $args1[1]); /* @var Expression $fetchSubArg */ $fetchSubArg = $args1[1]; $this->assertFalse($fetchSubArg->getAlias()); $args2 = $fetchSubArg->getFunctionArgs(); $this->assertTrue(is_array($args2)); $this->assertEquals(2, count($args2)); $this->assertEquals(' ', $args2[0]); $this->assertEquals(':lastname', $args2[1]); }