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

Commit

Permalink
Merge branch 'hotfix/3803'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Feb 19, 2013
2 parents 4e5e97b + 614dd81 commit 3836aa0
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/Identical.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getToken()
*/
public function setToken($token)
{
$this->tokenString = (is_array($token) ? implode($token) : (string) $token);
$this->tokenString = (is_array($token) ? var_export($token, true) : (string) $token);
$this->token = $token;
return $this;
}
Expand Down Expand Up @@ -121,15 +121,41 @@ public function setStrict($strict)
* @param mixed $value
* @param array $context
* @return bool
* @throws Exception\RuntimeException if the token doesn't exist in the context array
*/
public function isValid($value, $context = null)
{
$this->setValue($value);

if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
$token = $context[$this->getToken()];
} else {
$token = $this->getToken();
$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 (is_array($token)) {
while (is_array($token)){
$key = key($token);
if (!isset($context[$key])) {
break;
}
$context = $context[$key];
$token = $token[$key];
}
}

// if $token is an array it means the above loop didn't went all the way down to the leaf,
// so the $token structure doesn't match the $context structure
if (is_array($token) || !isset($context[$token])) {
throw new Exception\RuntimeException("The token doesn't exist in the context");
} else {
$token = $context[$token];
}
}

if ($token === null) {
Expand Down
84 changes: 84 additions & 0 deletions test/IdenticalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,88 @@ public function testEqualsMessageVariables()
$this->assertAttributeEquals($validator->getOption('messageVariables'),
'messageVariables', $validator);
}

public function testValidatingStringTokenInContext()
{
$this->validator->setToken('email');

$this->assertTrue($this->validator->isValid(
'john@doe.com',
array('email' => 'john@doe.com')
));

$this->assertFalse($this->validator->isValid(
'john@doe.com',
array('email' => 'harry@hoe.com')
));

$this->assertFalse($this->validator->isValid(
'harry@hoe.com',
array('email' => 'john@doe.com')
));
}

public function testValidatingArrayTokenInContext()
{
$this->validator->setToken(array('user' => 'email'));

$this->assertTrue($this->validator->isValid(
'john@doe.com',
array(
'user' => array(
'email' => 'john@doe.com'
)
)
));

$this->assertFalse($this->validator->isValid(
'john@doe.com',
array(
'user' => array(
'email' => 'harry@hoe.com'
)
)
));

$this->assertFalse($this->validator->isValid(
'harry@hoe.com',
array(
'user' => array(
'email' => 'john@doe.com'
)
)
));
}

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

$this->validator->isValid(
'john@doe.com',
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(
'john@doe.com',
array(
'admin' => array( // Here is 'admin' instead of 'user', must throw an exception
'email' => 'john@doe.com'
)
)
);
}
}

0 comments on commit 3836aa0

Please sign in to comment.