diff --git a/library/Zend/Validator/Identical.php b/library/Zend/Validator/Identical.php index f2cec5ee9ad..9615dde22ef 100644 --- a/library/Zend/Validator/Identical.php +++ b/library/Zend/Validator/Identical.php @@ -43,7 +43,8 @@ class Identical extends AbstractValidator */ protected $tokenString; protected $token; - protected $strict = true; + protected $strict = true; + protected $literal = false; /** * Sets validator options @@ -61,6 +62,10 @@ public function __construct($token = null) $this->setStrict($token['strict']); } + if (array_key_exists('literal', $token)) { + $this->setLiteral($token['literal']); + } + $this->setToken($token['token']); } elseif (null !== $token) { $this->setToken($token); @@ -72,7 +77,7 @@ public function __construct($token = null) /** * Retrieve token * - * @return string + * @return mixed */ public function getToken() { @@ -105,7 +110,7 @@ public function getStrict() /** * Sets the strict parameter * - * @param Zend\Validator\Identical + * @param bool $strict * @return Identical */ public function setStrict($strict) @@ -114,6 +119,28 @@ public function setStrict($strict) return $this; } + /** + * Returns the literal parameter + * + * @return bool + */ + public function getLiteral() + { + return $this->literal; + } + + /** + * Sets the literal parameter + * + * @param bool $literal + * @return Identical + */ + public function setLiteral($literal) + { + $this->literal = (bool) $literal; + return $this; + } + /** * Returns true if and only if a token has been set and the provided value * matches that token. @@ -123,21 +150,13 @@ public function setStrict($strict) * @return bool * @throws Exception\RuntimeException if the token doesn't exist in the context array */ - public function isValid($value, $context = null) + public function isValid($value, array $context = null) { $this->setValue($value); $token = $this->getToken(); - if ($context !== null) { - if (!is_array($context)) { - throw new Exception\InvalidArgumentException(sprintf( - 'Context passed to %s must be an array or null; received "%s"', - __METHOD__, - (is_object($context) ? get_class($context) : gettype($context)) - )); - } - + if (!$this->getLiteral() && $context !== null) { if (is_array($token)) { while (is_array($token)){ $key = key($token); diff --git a/tests/ZendTest/Validator/IdenticalTest.php b/tests/ZendTest/Validator/IdenticalTest.php index 49ff2b323fc..5aaba8b2d2c 100644 --- a/tests/ZendTest/Validator/IdenticalTest.php +++ b/tests/ZendTest/Validator/IdenticalTest.php @@ -213,4 +213,34 @@ public function testSetArrayTokenNonExistentInContext() ) ); } + + public function testCanSetLiteralParameterThroughConstructor() + { + $validator = new Identical(array('token' => 'foo', 'literal' => true)); + // Default is false + $validator->setLiteral(true); + $this->assertTrue($validator->getLiteral()); + } + + public function testLiteralParameterDoesNotAffectValidationWhenNoContextIsProvided() + { + $this->validator->setToken(array('foo' => 'bar')); + + $this->validator->setLiteral(false); + $this->assertTrue($this->validator->isValid(array('foo' => 'bar'))); + + $this->validator->setLiteral(true); + $this->assertTrue($this->validator->isValid(array('foo' => 'bar'))); + } + + public function testLiteralParameterWorksWhenContextIsProvided() + { + $this->validator->setToken(array('foo' => 'bar')); + $this->validator->setLiteral(true); + + $this->assertTrue($this->validator->isValid( + array('foo' => 'bar'), + array('foo' => 'baz') // Provide a context to make sure the literal parameter will work + )); + } }