Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions library/Zend/Validator/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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you add a space ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To align with the line below!

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
30 changes: 30 additions & 0 deletions tests/ZendTest/Validator/IdenticalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}
}