diff --git a/lib/Doctrine/ORM/Query/Expr/Andx.php b/lib/Doctrine/ORM/Query/Expr/Andx.php index c5cf1f3ab..6ca2d5b70 100644 --- a/lib/Doctrine/ORM/Query/Expr/Andx.php +++ b/lib/Doctrine/ORM/Query/Expr/Andx.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class Andx extends Composite { - protected $_separator = ' AND '; - protected $_allowedClasses = array( + /** + * @var string + */ + protected $separator = ' AND '; + + /** + * @var array + */ + protected $allowedClasses = array( 'Doctrine\ORM\Query\Expr\Comparison', 'Doctrine\ORM\Query\Expr\Func', 'Doctrine\ORM\Query\Expr\Orx', 'Doctrine\ORM\Query\Expr\Andx', ); + + /** + * @return array + */ + public function getParts() + { + return $this->parts; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Base.php b/lib/Doctrine/ORM/Query/Expr/Base.php index 975d450cd..64705c2b6 100644 --- a/lib/Doctrine/ORM/Query/Expr/Base.php +++ b/lib/Doctrine/ORM/Query/Expr/Base.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ abstract class Base { - protected $_preSeparator = '('; - protected $_separator = ', '; - protected $_postSeparator = ')'; - protected $_allowedClasses = array(); + /** + * @var string + */ + protected $preSeparator = '('; - protected $_parts = array(); + /** + * @var string + */ + protected $separator = ', '; + /** + * @var string + */ + protected $postSeparator = ')'; + + /** + * @var array + */ + protected $allowedClasses = array(); + + /** + * @var array + */ + protected $parts = array(); + + /** + * @param array $args + */ public function __construct($args = array()) { $this->addMultiple($args); } + /** + * @param array $args + * @return Base + */ public function addMultiple($args = array()) { foreach ((array) $args as $arg) { @@ -55,6 +77,10 @@ abstract class Base return $this; } + /** + * @param mixed $arg + * @return Base + */ public function add($arg) { if ( $arg !== null || ($arg instanceof self && $arg->count() > 0) ) { @@ -62,28 +88,34 @@ abstract class Base if ( ! is_string($arg)) { $class = get_class($arg); - if ( ! in_array($class, $this->_allowedClasses)) { + if ( ! in_array($class, $this->allowedClasses)) { throw new \InvalidArgumentException("Expression of type '$class' not allowed in this context."); } } - $this->_parts[] = $arg; + $this->parts[] = $arg; } return $this; } + /** + * @return integer + */ public function count() { - return count($this->_parts); + return count($this->parts); } + /** + * @return string + */ public function __toString() { if ($this->count() == 1) { - return (string) $this->_parts[0]; + return (string) $this->parts[0]; } - return $this->_preSeparator . implode($this->_separator, $this->_parts) . $this->_postSeparator; + return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator; } } diff --git a/lib/Doctrine/ORM/Query/Expr/Comparison.php b/lib/Doctrine/ORM/Query/Expr/Comparison.php index d42560eb7..f15c92435 100644 --- a/lib/Doctrine/ORM/Query/Expr/Comparison.php +++ b/lib/Doctrine/ORM/Query/Expr/Comparison.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel @@ -41,19 +38,64 @@ class Comparison const GT = '>'; const GTE = '>='; - private $_leftExpr; - private $_operator; - private $_rightExpr; + /** + * @var mixed + */ + private $leftExpr; + /** + * @var string + */ + private $operator; + + /** + * @var mixed + */ + private $rightExpr; + + /** + * Creates a comparison expression with the given arguments. + * + * @param mixed $leftExpr + * @param string $operator + * @param mixed $rightExpr + */ public function __construct($leftExpr, $operator, $rightExpr) { - $this->_leftExpr = $leftExpr; - $this->_operator = $operator; - $this->_rightExpr = $rightExpr; + $this->leftExpr = $leftExpr; + $this->operator = $operator; + $this->rightExpr = $rightExpr; } + /** + * @return mixed + */ + public function getLeftExpr() + { + return $this->leftExpr; + } + + /** + * @return string + */ + public function getOperator() + { + return $this->operator; + } + + /** + * @return mixed + */ + public function getRightExpr() + { + return $this->rightExpr; + } + + /** + * @return string + */ public function __toString() { - return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr; + return $this->leftExpr . ' ' . $this->operator . ' ' . $this->rightExpr; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Composite.php b/lib/Doctrine/ORM/Query/Expr/Composite.php index 036b241a5..cc2d2f8bd 100644 --- a/lib/Doctrine/ORM/Query/Expr/Composite.php +++ b/lib/Doctrine/ORM/Query/Expr/Composite.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class Composite extends Base { + /** + * @return string + */ public function __toString() { if ($this->count() === 1) { - return (string) $this->_parts[0]; + return (string) $this->parts[0]; } $components = array(); - foreach ($this->_parts as $part) { + foreach ($this->parts as $part) { $components[] = $this->processQueryPart($part); } - return implode($this->_separator, $components); + return implode($this->separator, $components); } + /** + * @param string $part + * @return string + */ private function processQueryPart($part) { $queryPart = (string) $part; if (is_object($part) && $part instanceof self && $part->count() > 1) { - return $this->_preSeparator . $queryPart . $this->_postSeparator; + return $this->preSeparator . $queryPart . $this->postSeparator; } // Fixes DDC-1237: User may have added a where item containing nested expression (with "OR" or "AND") if (stripos($queryPart, ' OR ') !== false || stripos($queryPart, ' AND ') !== false) { - return $this->_preSeparator . $queryPart . $this->_postSeparator; + return $this->preSeparator . $queryPart . $this->postSeparator; } return $queryPart; diff --git a/lib/Doctrine/ORM/Query/Expr/From.php b/lib/Doctrine/ORM/Query/Expr/From.php index e5707cce6..b9c70cb37 100644 --- a/lib/Doctrine/ORM/Query/Expr/From.php +++ b/lib/Doctrine/ORM/Query/Expr/From.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel @@ -37,17 +34,17 @@ class From /** * @var string */ - private $_from; + private $from; /** * @var string */ - private $_alias; + private $alias; /** * @var string */ - private $_indexBy; + private $indexBy; /** * @param string $from The class name. @@ -56,9 +53,9 @@ class From */ public function __construct($from, $alias, $indexBy = null) { - $this->_from = $from; - $this->_alias = $alias; - $this->_indexBy = $indexBy; + $this->from = $from; + $this->alias = $alias; + $this->indexBy = $indexBy; } /** @@ -66,7 +63,7 @@ class From */ public function getFrom() { - return $this->_from; + return $this->from; } /** @@ -74,7 +71,15 @@ class From */ public function getAlias() { - return $this->_alias; + return $this->alias; + } + + /** + * @return string + */ + public function getIndexBy() + { + return $this->indexBy; } /** @@ -82,7 +87,7 @@ class From */ public function __toString() { - return $this->_from . ' ' . $this->_alias . - ($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : ''); + return $this->from . ' ' . $this->alias . + ($this->indexBy ? ' INDEX BY ' . $this->indexBy : ''); } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Func.php b/lib/Doctrine/ORM/Query/Expr/Func.php index 48b1a5b5e..35e79b0d5 100644 --- a/lib/Doctrine/ORM/Query/Expr/Func.php +++ b/lib/Doctrine/ORM/Query/Expr/Func.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class Func { - private $_name; - private $_arguments; + /** + * @var string + */ + private $name; + /** + * @var array + */ + private $arguments; + + /** + * Creates a function, with the given argument. + * + * @param string $name + * @param array $arguments + */ public function __construct($name, $arguments) { - $this->_name = $name; - $this->_arguments = (array) $arguments; + $this->name = $name; + $this->arguments = (array) $arguments; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return array + */ + public function getArguments() + { + return $this->arguments; } public function __toString() { - return $this->_name . '(' . implode(', ', $this->_arguments) . ')'; + return $this->name . '(' . implode(', ', $this->arguments) . ')'; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/GroupBy.php b/lib/Doctrine/ORM/Query/Expr/GroupBy.php index dc36ba3d6..f0ad7c326 100644 --- a/lib/Doctrine/ORM/Query/Expr/GroupBy.php +++ b/lib/Doctrine/ORM/Query/Expr/GroupBy.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class GroupBy extends Base { - protected $_preSeparator = ''; - protected $_postSeparator = ''; + /** + * @var string + */ + protected $preSeparator = ''; + + /** + * @var string + */ + protected $postSeparator = ''; + + /** + * @return array + */ + public function getParts() + { + return $this->parts; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Join.php b/lib/Doctrine/ORM/Query/Expr/Join.php index 14f5b43cd..7c75d0318 100644 --- a/lib/Doctrine/ORM/Query/Expr/Join.php +++ b/lib/Doctrine/ORM/Query/Expr/Join.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class Join { - const INNER_JOIN = 'INNER'; - const LEFT_JOIN = 'LEFT'; + const INNER_JOIN = 'INNER'; + const LEFT_JOIN = 'LEFT'; - const ON = 'ON'; - const WITH = 'WITH'; + const ON = 'ON'; + const WITH = 'WITH'; - private $_joinType; - private $_join; - private $_alias; - private $_conditionType; - private $_condition; - private $_indexBy; + /** + * @var string + */ + private $joinType; + /** + * @var string + */ + private $join; + + /** + * @var string + */ + private $alias; + + /** + * @var string + */ + private $conditionType; + + /** + * @var string + */ + private $condition; + + /** + * @var string + */ + private $indexBy; + + /** + * @param string $joinType The condition type constant. Either INNER_JOIN or LEFT_JOIN. + * @param string $join The relationship to join + * @param string $alias The alias of the join + * @param string $conditionType The condition type constant. Either ON or WITH. + * @param string $condition The condition for the join + * @param string $indexBy The index for the join + */ public function __construct($joinType, $join, $alias = null, $conditionType = null, $condition = null, $indexBy = null) { - $this->_joinType = $joinType; - $this->_join = $join; - $this->_alias = $alias; - $this->_conditionType = $conditionType; - $this->_condition = $condition; - $this->_indexBy = $indexBy; + $this->joinType = $joinType; + $this->join = $join; + $this->alias = $alias; + $this->conditionType = $conditionType; + $this->condition = $condition; + $this->indexBy = $indexBy; } + /** + * @return string + */ + public function getJoinType() + { + return $this->joinType; + } + + /** + * @return string + */ + public function getJoin() + { + return $this->join; + } + + /** + * @return string + */ + public function getAlias() + { + return $this->alias; + } + + /** + * @return string + */ + public function getConditionType() + { + return $this->conditionType; + } + + /** + * @return string + */ + public function getCondition() + { + return $this->condition; + } + + /** + * @return string + */ + public function getIndexBy() + { + return $this->indexBy; + } + + + /** + * @return string + */ public function __toString() { - return strtoupper($this->_joinType) . ' JOIN ' . $this->_join - . ($this->_alias ? ' ' . $this->_alias : '') - . ($this->_condition ? ' ' . strtoupper($this->_conditionType) . ' ' . $this->_condition : '') - . ($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : ''); + return strtoupper($this->joinType) . ' JOIN ' . $this->join + . ($this->alias ? ' ' . $this->alias : '') + . ($this->condition ? ' ' . strtoupper($this->conditionType) . ' ' . $this->condition : '') + . ($this->indexBy ? ' INDEX BY ' . $this->indexBy : ''); } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Literal.php b/lib/Doctrine/ORM/Query/Expr/Literal.php index c1dd5f783..200b22079 100644 --- a/lib/Doctrine/ORM/Query/Expr/Literal.php +++ b/lib/Doctrine/ORM/Query/Expr/Literal.php @@ -1,9 +1,52 @@ . + */ namespace Doctrine\ORM\Query\Expr; +/** + * Expression class for generating DQL functions + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @author Guilherme Blanco + * @author Jonathan Wage + * @author Roman Borschel + */ class Literal extends Base { - protected $_preSeparator = ''; - protected $_postSeparator = ''; + /** + * @var string + */ + protected $preSeparator = ''; + + /** + * @var string + */ + protected $postSeparator = ''; + + /** + * @return array + */ + public function getParts() + { + return $this->parts; + } + } diff --git a/lib/Doctrine/ORM/Query/Expr/Math.php b/lib/Doctrine/ORM/Query/Expr/Math.php index e7e8b329c..3bd8f4cdb 100644 --- a/lib/Doctrine/ORM/Query/Expr/Math.php +++ b/lib/Doctrine/ORM/Query/Expr/Math.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class Math { - private $_leftExpr; - private $_operator; - private $_rightExpr; + /** + * @var mixed + */ + private $leftExpr; + /** + * @var string + */ + private $operator; + + /** + * @var mixed + */ + private $rightExpr; + + /** + * Creates a mathematical expression with the given arguments. + * + * @param mixed $leftExpr + * @param string $operator + * @param mixed $rightExpr + */ public function __construct($leftExpr, $operator, $rightExpr) { - $this->_leftExpr = $leftExpr; - $this->_operator = $operator; - $this->_rightExpr = $rightExpr; + $this->leftExpr = $leftExpr; + $this->operator = $operator; + $this->rightExpr = $rightExpr; + } + + /** + * @return mixed + */ + public function getLeftExpr() + { + return $this->leftExpr; + } + + /** + * @return string + */ + public function getOperator() + { + return $this->operator; + } + + /** + * @return mixed + */ + public function getRightExpr() + { + return $this->rightExpr; } public function __toString() { // Adjusting Left Expression - $leftExpr = (string) $this->_leftExpr; + $leftExpr = (string) $this->leftExpr; - if ($this->_leftExpr instanceof Math) { + if ($this->leftExpr instanceof Math) { $leftExpr = '(' . $leftExpr . ')'; } // Adjusting Right Expression - $rightExpr = (string) $this->_rightExpr; + $rightExpr = (string) $this->rightExpr; - if ($this->_rightExpr instanceof Math) { + if ($this->rightExpr instanceof Math) { $rightExpr = '(' . $rightExpr . ')'; } - return $leftExpr . ' ' . $this->_operator . ' ' . $rightExpr; + return $leftExpr . ' ' . $this->operator . ' ' . $rightExpr; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/OrderBy.php b/lib/Doctrine/ORM/Query/Expr/OrderBy.php index a24e286a2..c4e32ab03 100644 --- a/lib/Doctrine/ORM/Query/Expr/OrderBy.php +++ b/lib/Doctrine/ORM/Query/Expr/OrderBy.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class OrderBy { - protected $_preSeparator = ''; - protected $_separator = ', '; - protected $_postSeparator = ''; - protected $_allowedClasses = array(); + /** + * @var string + */ + protected $preSeparator = ''; - private $_parts = array(); + /** + * @var string + */ + protected $separator = ', '; + /** + * @var string + */ + protected $postSeparator = ''; + + /** + * @var array + */ + protected $allowedClasses = array(); + + /** + * @var array + */ + private $parts = array(); + + /** + * @param string $sort + * @param string $order + */ public function __construct($sort = null, $order = null) { if ($sort) { @@ -48,19 +67,37 @@ class OrderBy } } + /** + * @param string $sort + * @param string $order + */ public function add($sort, $order = null) { $order = ! $order ? 'ASC' : $order; - $this->_parts[] = $sort . ' '. $order; + $this->parts[] = $sort . ' '. $order; } + /** + * @return integer + */ public function count() { - return count($this->_parts); + return count($this->parts); } + /** + * @return array + */ + public function getParts() + { + return $this->parts; + } + + /** + * @return string + */ public function __tostring() { - return $this->_preSeparator . implode($this->_separator, $this->_parts) . $this->_postSeparator; + return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Orx.php b/lib/Doctrine/ORM/Query/Expr/Orx.php index 742e499f7..b2721aa88 100644 --- a/lib/Doctrine/ORM/Query/Expr/Orx.php +++ b/lib/Doctrine/ORM/Query/Expr/Orx.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class Orx extends Composite { - protected $_separator = ' OR '; - protected $_allowedClasses = array( + /** + * @var string + */ + protected $separator = ' OR '; + + /** + * @var array + */ + protected $allowedClasses = array( 'Doctrine\ORM\Query\Expr\Comparison', 'Doctrine\ORM\Query\Expr\Func', 'Doctrine\ORM\Query\Expr\Andx', 'Doctrine\ORM\Query\Expr\Orx', ); + + /** + * @return array + */ + public function getParts() + { + return $this->parts; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/Expr/Select.php b/lib/Doctrine/ORM/Query/Expr/Select.php index a310a0c7b..983ae70b7 100644 --- a/lib/Doctrine/ORM/Query/Expr/Select.php +++ b/lib/Doctrine/ORM/Query/Expr/Select.php @@ -1,7 +1,5 @@ * @author Jonathan Wage * @author Roman Borschel */ class Select extends Base { - protected $_preSeparator = ''; - protected $_postSeparator = ''; - protected $_allowedClasses = array( + /** + * @var string + */ + protected $preSeparator = ''; + + /** + * @var string + */ + protected $postSeparator = ''; + + /** + * @var array + */ + protected $allowedClasses = array( 'Doctrine\ORM\Query\Expr\Func' ); + + /** + * @return array + */ + public function getParts() + { + return $this->parts; + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Query/ExprTest.php b/tests/Doctrine/Tests/ORM/Query/ExprTest.php index 265b2ba90..122b98f03 100644 --- a/tests/Doctrine/Tests/ORM/Query/ExprTest.php +++ b/tests/Doctrine/Tests/ORM/Query/ExprTest.php @@ -345,4 +345,68 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('true', $this->_expr->literal(true)); $this->assertEquals('false', $this->_expr->literal(false)); } + + + /** + * @group DDC-1686 + */ + public function testExpressionGetter() + { + + // Andx + $andx = new Expr\Andx(array('1 = 1', '2 = 2')); + $this->assertEquals(array('1 = 1', '2 = 2'), $andx->getParts()); + + // Comparison + $comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar'); + $this->assertEquals('foo', $comparison->getLeftExpr()); + $this->assertEquals('bar', $comparison->getRightExpr()); + $this->assertEquals(Expr\Comparison::EQ, $comparison->getOperator()); + + // From + $from = new Expr\From('Foo', 'f', 'f.id'); + $this->assertEquals('f', $from->getAlias()); + $this->assertEquals('Foo', $from->getFrom()); + $this->assertEquals('f.id', $from->getIndexBy()); + + // Func + $func = new Expr\Func('MAX', array('f.id')); + $this->assertEquals('MAX', $func->getName()); + $this->assertEquals(array('f.id'), $func->getArguments()); + + // GroupBy + $group = new Expr\GroupBy(array('foo DESC', 'bar ASC')); + $this->assertEquals(array('foo DESC', 'bar ASC'), $group->getParts()); + + // Join + $join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id'); + $this->assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType()); + $this->assertEquals(Expr\Join::ON, $join->getConditionType()); + $this->assertEquals('b.bar_id = 1', $join->getCondition()); + $this->assertEquals('b.bar_id', $join->getIndexBy()); + $this->assertEquals('f.bar', $join->getJoin()); + $this->assertEquals('b', $join->getAlias()); + + // Literal + $literal = new Expr\Literal(array('foo')); + $this->assertEquals(array('foo'), $literal->getParts()); + + // Math + $math = new Expr\Math(10, '+', 20); + $this->assertEquals(10, $math->getLeftExpr()); + $this->assertEquals(20, $math->getRightExpr()); + $this->assertEquals('+', $math->getOperator()); + + // OrderBy + $order = new Expr\OrderBy('foo', 'DESC'); + $this->assertEquals(array('foo DESC'), $order->getParts()); + + // Andx + $orx = new Expr\Orx(array('foo = 1', 'bar = 2')); + $this->assertEquals(array('foo = 1', 'bar = 2'), $orx->getParts()); + + // Select + $select = new Expr\Select(array('foo', 'bar')); + $this->assertEquals(array('foo', 'bar'), $select->getParts()); + } }