3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Sql\Predicate
;
12 use Zend\Db\Sql\Exception
;
14 class Operator
implements PredicateInterface
16 const OPERATOR_EQUAL_TO
= '=';
19 const OPERATOR_NOT_EQUAL_TO
= '!=';
22 const OPERATOR_LESS_THAN
= '<';
25 const OPERATOR_LESS_THAN_OR_EQUAL_TO
= '<=';
28 const OPERATOR_GREATER_THAN
= '>';
31 const OPERATOR_GREATER_THAN_OR_EQUAL_TO
= '>=';
34 protected $allowedTypes = array(
35 self
::TYPE_IDENTIFIER
,
39 protected $left = null;
40 protected $leftType = self
::TYPE_IDENTIFIER
;
41 protected $operator = self
::OPERATOR_EQUAL_TO
;
42 protected $right = null;
43 protected $rightType = self
::TYPE_VALUE
;
48 * @param int|float|bool|string $left
49 * @param string $operator
50 * @param int|float|bool|string $right
51 * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes}
52 * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes}
54 public function __construct($left = null, $operator = self
::OPERATOR_EQUAL_TO
, $right = null, $leftType = self
::TYPE_IDENTIFIER
, $rightType = self
::TYPE_VALUE
)
57 $this->setLeft($left);
60 if ($operator !== self
::OPERATOR_EQUAL_TO
) {
61 $this->setOperator($operator);
64 if ($right !== null) {
65 $this->setRight($right);
68 if ($leftType !== self
::TYPE_IDENTIFIER
) {
69 $this->setLeftType($leftType);
72 if ($rightType !== self
::TYPE_VALUE
) {
73 $this->setRightType($rightType);
78 * Set left side of operator
80 * @param int|float|bool|string $left
83 public function setLeft($left)
90 * Get left side of operator
92 * @return int|float|bool|string
94 public function getLeft()
100 * Set parameter type for left side of operator
102 * @param string $type TYPE_IDENTIFIER or TYPE_VALUE {@see allowedTypes}
103 * @throws Exception\InvalidArgumentException
106 public function setLeftType($type)
108 if (!in_array($type, $this->allowedTypes
)) {
109 throw new Exception\
InvalidArgumentException(sprintf(
110 'Invalid type "%s" provided; must be of type "%s" or "%s"',
112 __CLASS__
. '::TYPE_IDENTIFIER',
113 __CLASS__
. '::TYPE_VALUE'
116 $this->leftType
= $type;
121 * Get parameter type on left side of operator
125 public function getLeftType()
127 return $this->leftType
;
131 * Set operator string
133 * @param string $operator
136 public function setOperator($operator)
138 $this->operator
= $operator;
143 * Get operator string
147 public function getOperator()
149 return $this->operator
;
153 * Set right side of operator
155 * @param int|float|bool|string $value
158 public function setRight($value)
160 $this->right
= $value;
165 * Get right side of operator
167 * @return int|float|bool|string
169 public function getRight()
175 * Set parameter type for right side of operator
177 * @param string $type TYPE_IDENTIFIER or TYPE_VALUE {@see allowedTypes}
178 * @throws Exception\InvalidArgumentException
181 public function setRightType($type)
183 if (!in_array($type, $this->allowedTypes
)) {
184 throw new Exception\
InvalidArgumentException(sprintf(
185 'Invalid type "%s" provided; must be of type "%s" or "%s"',
187 __CLASS__
. '::TYPE_IDENTIFIER',
188 __CLASS__
. '::TYPE_VALUE'
191 $this->rightType
= $type;
196 * Get parameter type on right side of operator
200 public function getRightType()
202 return $this->rightType
;
206 * Get predicate parts for where statement
210 public function getExpressionData()
213 '%s ' . $this->operator
. ' %s',
214 array($this->left
, $this->right
),
215 array($this->leftType
, $this->rightType
)