', '<', '>', '<=', '>=']; protected $_messageTemplates = array( self::INVALID_VALUE => "'%value%' is not valid", ); /** * @var mixed */ protected $_callback = null; /** * @var string */ protected $_operator = null; protected $_rightValue = null; public function __construct($callback = null) { if (is_callable($callback)) { $this->setCallback($callback); } elseif (is_array($callback)) { if (isset($callback['callback'])) { $this->setCallback($callback['callback']); } if (isset($callback['operator'])) { $this->setOperator($callback['operator']); } if (isset($callback['rightValue'])) { $this->setRightValue($callback['rightValue']); } } if (null === $this->_callback && null === $this->_rightValue) { throw new Zend_Validate_Exception('No callback registered or right value defined'); } if (null === $this->getOperator()) { throw new Zend_Validate_Exception('No operator registered'); } } /** * @param null $rightValue * @return $this */ public function setRightValue($rightValue) { $this->_rightValue = $rightValue; return $this; } /** * @return null */ public function getRightValue() { return $this->_rightValue; } public function setCallback($callback) { if (!is_callable($callback)) { throw new Zend_Validate_Exception('Invalid callback given'); } $this->_callback = $callback; return $this; } public function getCallback() { if (!is_callable($this->_callback)) { throw new Zend_Validate_Exception('Callback is undefined'); } return $this->_callback; } /** * @param string $operator * @throws Zend_Validate_Exception * @return $this */ public function setOperator($operator) { if (!in_array($operator, $this->_operators)) { throw new Zend_Validate_Exception('Invalid operator: "' . $operator . '"'); } $this->_operator = $operator; return $this; } /** * @return string */ public function getOperator() { return $this->_operator; } public function isValid($value) { $this->_setValue($value); $operator = $this->getOperator(); if (null === ($rightValue = $this->getRightValue())) { $rightValue = call_user_func($this->getCallback()); } switch ($operator) { case '=': $isValid = $value = $rightValue; break; case '==': $isValid = $value == $rightValue; break; case '!=': $isValid = $value != $rightValue; break; case '!==': $isValid = $value !== $rightValue; break; case '<>': $isValid = $value <> $rightValue; break; case '<': $isValid = $value < $rightValue; break; case '>': $isValid = $value > $rightValue; break; case '<=': $isValid = $value <= $rightValue; break; case '>=': $isValid = $value >= $rightValue; break; default: throw new Zend_Validate_Exception('Invalid operator: "' . $operator . '"'); } if (!$isValid) { $this->_error(self::INVALID_VALUE); return false; } return $isValid; } }