Skip to content

More intuitive matching of bundle directory lookups for Doctrine extensions #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Prev Previous commit
Next Next commit
Allow ValidValidator to walk \Traversable in addition to array
  • Loading branch information
cranberyxl committed Nov 9, 2010
commit 75a53cf18c003c489eec1b5e30e8f626ae09ceb3
14 changes: 8 additions & 6 deletions src/Symfony/Component/Validator/Constraints/ValidValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ public function isValid($value, Constraint $constraint)
$propertyPath = $this->context->getPropertyPath();
$factory = $this->context->getClassMetadataFactory();

if (is_array($value)) {
foreach ($value as $key => $element) {
$walker->walkConstraint($constraint, $element, $group, $propertyPath.'['.$key.']');
}
} else if (!is_object($value)) {
if (!is_array($value) && !is_object($value)) {
throw new UnexpectedTypeException($value, 'object or array');
} else if ($constraint->class && !$value instanceof $constraint->class) {
$this->setMessage($constraint->message, array('class' => $constraint->class));

return false;
} else {
} else if (!is_array($value)) {
$metadata = $factory->getClassMetadata(get_class($value));
$walker->walkClass($metadata, $value, $group, $propertyPath);
}

if (is_array($value) || $value instanceof \Traversable) {
foreach ($value as $key => $element) {
$walker->walkConstraint($constraint, $element, $group, $propertyPath.'['.$key.']');
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ public function testWalkArray()
$this->assertTrue($this->validator->isValid($array, $constraint));
}

public function testWalkTraversable()
{
$this->context->setGroup('MyGroup');
$this->context->setPropertyPath('foo');

$constraint = new Valid();
$metadata = $this->createClassMetadata();
$entity = new Entity();
// can only test for one object due to PHPUnit's mocking limitations
$traversable = new \ArrayObject( array('key' => $entity));

$this->walker->expects($this->once())
->method('walkConstraint')
->with($this->equalTo($constraint), $this->equalTo($entity), 'MyGroup', 'foo[key]');

$this->factory->expects($this->once())
->method('getClassMetadata')
->with($this->equalTo(get_class($traversable)))
->will($this->returnValue($metadata));


$this->walker->expects($this->once())
->method('walkClass')
->with($this->equalTo($metadata), $this->equalTo($traversable), 'MyGroup', 'foo');


$this->assertTrue($this->validator->isValid($traversable, $constraint));
}

public function testValidateClass_Succeeds()
{
$metadata = $this->createClassMetadata();
Expand Down