Skip to content

Commit

Permalink
Relax Explode validator isValid value type, defer to bound validator
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmartin committed Jan 23, 2013
1 parent 960e722 commit a976073
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 13 additions & 9 deletions library/Zend/Validator/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace Zend\Validator;

use Traversable;
use Zend\Stdlib\ArrayUtils;

/**
* @category Zend
* @package Zend_Validator
Expand All @@ -22,7 +25,7 @@ class Explode extends AbstractValidator
* @var array
*/
protected $messageTemplates = array(
self::INVALID => "Invalid type given. String expected",
self::INVALID => "Invalid type given.",
);

/**
Expand Down Expand Up @@ -116,20 +119,21 @@ public function isBreakOnFirstFailure()
*
* Returns true if all values validate true
*
* @param string|array $value
* @param mixed $value
* @return bool
* @throws Exception\RuntimeException
*/
public function isValid($value)
{
if (!is_string($value) && !is_array($value)) {
$this->error(self::INVALID);
return false;
}

$this->setValue($value);

if (!is_array($value)) {
if ($value instanceof Traversable) {
$value = ArrayUtils::iteratorToArray($value);
}

if (is_array($value)) {
$values = $value;
} elseif (is_string($value)) {
$delimiter = $this->getValueDelimiter();
// Skip explode if delimiter is null,
// used when value is expected to be either an
Expand All @@ -139,7 +143,7 @@ public function isValid($value)
? explode($this->valueDelimiter, $value)
: array($value);
} else {
$values = $value;
$values = array($value);
}

$retval = true;
Expand Down
6 changes: 4 additions & 2 deletions tests/ZendTest/Validator/ExplodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function getExpectedData()
array(array('a', 'b'), null, false, 2, true, array(), true),
array(array('a', 'b'), null, false, 2, false, array('X', 'X'), false),
array('foo', null, false, 1, true, array(), true),
array(1, ',', false, 0, true, array(Explode::INVALID => 'Invalid'), false),
array(1, ',', false, 1, true, array(), true),
array(null, ',', false, 1, true, array(), true),
array(new \stdClass(), ',', false, 1, true, array(), true),
array(new \ArrayObject(array('a', 'b')), null, false, 2, true, array(), true),
);
}

Expand All @@ -62,7 +65,6 @@ public function testExpectedBehavior($value, $delimiter, $breakOnFirst, $numIsVa
'valueDelimiter' => $delimiter,
'breakOnFirstFailure' => $breakOnFirst,
));
$validator->setMessage('Invalid', Explode::INVALID);

$this->assertEquals($expects, $validator->isValid($value));
$this->assertEquals($messages, $validator->getMessages());
Expand Down

0 comments on commit a976073

Please sign in to comment.