small code refactoring
This commit is contained in:
parent
0c1a8cd43f
commit
0e60c50c5e
@ -20,7 +20,7 @@
|
|||||||
namespace Doctrine\ORM\Query\AST;
|
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
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||||
* @link www.doctrine-project.org
|
* @link www.doctrine-project.org
|
||||||
@ -32,21 +32,21 @@ class NewObjectExpression extends Node
|
|||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $identificationVariable;
|
public $className;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $fieldSet;
|
public $args;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param type $identificationVariable
|
* @param type $className
|
||||||
* @param array $fieldSet
|
* @param array $args
|
||||||
*/
|
*/
|
||||||
public function __construct($identificationVariable, array $fieldSet)
|
public function __construct($className, array $args)
|
||||||
{
|
{
|
||||||
$this->identificationVariable = $identificationVariable;
|
$this->className = $className;
|
||||||
$this->fieldSet = $fieldSet;
|
$this->args = $args;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1630,7 +1630,7 @@ class Parser
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NewObjectExpression ::= "NEW" IdentificationVariable "(" SimpleSelectExpression {"," SimpleSelectExpression}* ")"
|
* NewObjectExpression ::= "NEW" IdentificationVariable "(" SelectExpression {"," SelectExpression}* ")"
|
||||||
* @return \Doctrine\ORM\Query\AST\NewObjectExpression
|
* @return \Doctrine\ORM\Query\AST\NewObjectExpression
|
||||||
*/
|
*/
|
||||||
public function NewObjectExpression()
|
public function NewObjectExpression()
|
||||||
@ -1638,20 +1638,33 @@ class Parser
|
|||||||
$this->match(Lexer::T_NEW);
|
$this->match(Lexer::T_NEW);
|
||||||
$this->match(Lexer::T_IDENTIFIER);
|
$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);
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
||||||
|
|
||||||
$fieldSet[] = $this->SelectExpression();
|
$args[] = $this->SelectExpression();
|
||||||
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
||||||
$this->match(Lexer::T_COMMA);
|
$this->match(Lexer::T_COMMA);
|
||||||
|
|
||||||
$fieldSet[] = $this->SelectExpression();
|
$args[] = $this->SelectExpression();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
$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 ?
|
// @TODO : Defer NewObjectExpression validation ?
|
||||||
return $expression;
|
return $expression;
|
||||||
@ -1984,7 +1997,6 @@ class Parser
|
|||||||
// NewObjectExpression (New ClassName(id, name))
|
// NewObjectExpression (New ClassName(id, name))
|
||||||
case ($lookaheadType === Lexer::T_NEW):
|
case ($lookaheadType === Lexer::T_NEW):
|
||||||
$expression = $this->NewObjectExpression();
|
$expression = $this->NewObjectExpression();
|
||||||
//$identVariable = $expression->identificationVariable;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -1222,7 +1222,7 @@ class SqlWalker implements TreeWalker
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case ($expr instanceof AST\NewObjectExpression):
|
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);
|
$sql .= implode(', ', $sqlSelectExpressions);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -1561,7 +1561,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
public function testSupportsNewOperator()
|
public function testSupportsNewOperator()
|
||||||
{
|
{
|
||||||
$this->assertSqlGeneration(
|
$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'
|
'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'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user