1
0
mirror of synced 2025-01-17 22:11:41 +03:00

small code refactoring

This commit is contained in:
Fabio B. Silva 2012-01-07 14:42:44 -02:00
parent 0c1a8cd43f
commit 0e60c50c5e
4 changed files with 28 additions and 16 deletions

View File

@ -20,7 +20,7 @@
namespace Doctrine\ORM\Query\AST;
/**
* NewObjectExpression ::= "NEW" IdentificationVariable "(" SimpleSelectExpression {"," SimpleSelectExpression}* ")"
* NewObjectExpression ::= "NEW" IdentificationVariable "(" SelectExpression {"," SelectExpression}* ")"
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
@ -32,21 +32,21 @@ class NewObjectExpression extends Node
/**
* @var string
*/
public $identificationVariable;
public $className;
/**
* @var array
*/
public $fieldSet;
public $args;
/**
* @param type $identificationVariable
* @param array $fieldSet
* @param type $className
* @param array $args
*/
public function __construct($identificationVariable, array $fieldSet)
public function __construct($className, array $args)
{
$this->identificationVariable = $identificationVariable;
$this->fieldSet = $fieldSet;
$this->className = $className;
$this->args = $args;
}
/**

View File

@ -1630,7 +1630,7 @@ class Parser
}
/**
* NewObjectExpression ::= "NEW" IdentificationVariable "(" SimpleSelectExpression {"," SimpleSelectExpression}* ")"
* NewObjectExpression ::= "NEW" IdentificationVariable "(" SelectExpression {"," SelectExpression}* ")"
* @return \Doctrine\ORM\Query\AST\NewObjectExpression
*/
public function NewObjectExpression()
@ -1638,20 +1638,33 @@ class Parser
$this->match(Lexer::T_NEW);
$this->match(Lexer::T_IDENTIFIER);
$identificationVariable = $this->_lexer->token['value'];
$className = $this->_lexer->token['value'];
if ( ! class_exists($className, true)) {
$this->semanticalError("Class '$className' is not defined.", $this->_lexer->token);
}
$class = new \ReflectionClass($className);
if($class->getConstructor() === null) {
$this->semanticalError("Class '$className' has not a valid contructor.", $this->_lexer->token);
}
$this->match(Lexer::T_OPEN_PARENTHESIS);
$fieldSet[] = $this->SelectExpression();
$args[] = $this->SelectExpression();
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
$this->match(Lexer::T_COMMA);
$fieldSet[] = $this->SelectExpression();
$args[] = $this->SelectExpression();
}
$this->match(Lexer::T_CLOSE_PARENTHESIS);
$expression = new AST\NewObjectExpression($identificationVariable, $fieldSet);
if($class->getConstructor()->getNumberOfRequiredParameters() > sizeof($args)) {
$this->semanticalError("Number of arguments does not match definition.", $this->_lexer->token);
}
$expression = new AST\NewObjectExpression($className, $args);
// @TODO : Defer NewObjectExpression validation ?
return $expression;
@ -1984,7 +1997,6 @@ class Parser
// NewObjectExpression (New ClassName(id, name))
case ($lookaheadType === Lexer::T_NEW):
$expression = $this->NewObjectExpression();
//$identVariable = $expression->identificationVariable;
break;
default:

View File

@ -1222,7 +1222,7 @@ class SqlWalker implements TreeWalker
break;
case ($expr instanceof AST\NewObjectExpression):
$sqlSelectExpressions = array_filter(array_map(array($this, 'walkSelectExpression'), $expr->fieldSet));
$sqlSelectExpressions = array_filter(array_map(array($this, 'walkSelectExpression'), $expr->args));
$sql .= implode(', ', $sqlSelectExpressions);
break;

View File

@ -1561,7 +1561,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
public function testSupportsNewOperator()
{
$this->assertSqlGeneration(
'SELECT new Doctrine\Tests\Models\CMS\CmsUser(u.name, e.email, a.city) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e JOIN u.address a',
'SELECT new Doctrine\Tests\Models\CMS\CmsUserDTO(u.name, e.email, a.city) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e JOIN u.address a',
'SELECT c0_.name AS name0, c1_.email AS email1, c2_.city AS city2 FROM cms_users c0_ INNER JOIN cms_emails c1_ ON c0_.email_id = c1_.id INNER JOIN cms_addresses c2_ ON c0_.id = c2_.user_id'
);
}