Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#3844 from wryck7/fix-i…
Browse files Browse the repository at this point in the history
…dentical-validator

Added new option to fix a little issue originated from last PR

Conflicts:
	tests/ZendTest/Validator/IdenticalTest.php
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/Identical.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Identical extends AbstractValidator
*/
protected $tokenString;
protected $token;
protected $strict = true;
protected $strict = true;
protected $literal = false;

/**
* Sets validator options
Expand All @@ -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);
Expand All @@ -72,7 +77,7 @@ public function __construct($token = null)
/**
* Retrieve token
*
* @return string
* @return mixed
*/
public function getToken()
{
Expand Down Expand Up @@ -105,7 +110,7 @@ public function getStrict()
/**
* Sets the strict parameter
*
* @param Zend\Validator\Identical
* @param bool $strict
* @return Identical
*/
public function setStrict($strict)
Expand All @@ -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.
Expand All @@ -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);
Expand Down
62 changes: 62 additions & 0 deletions test/IdenticalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,66 @@ public function testValidatingArrayTokenInContext()
)
));
}

public function testSetStringTokenNonExistentInContext()
{
$this->validator->setToken('email');
$this->setExpectedException(
'Zend\Validator\Exception\RuntimeException',
"The token doesn't exist in the context"
);

$this->validator->isValid(
'[email protected]',
array('name' => 'john') // There's no 'email' key here, must throw an exception
);
}

public function testSetArrayTokenNonExistentInContext()
{
$this->validator->setToken(array('user' => 'email'));
$this->setExpectedException(
'Zend\Validator\Exception\RuntimeException',
"The token doesn't exist in the context"
);

$this->validator->isValid(
'[email protected]',
array(
'admin' => array( // Here is 'admin' instead of 'user', must throw an exception
'email' => '[email protected]'
)
)
);
}

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
));
}
}

0 comments on commit 295f526

Please sign in to comment.