Skip to content

Instantly share code, notes, and snippets.

@cranberyxl
Created November 9, 2010 16:05
Show Gist options
  • Save cranberyxl/669287 to your computer and use it in GitHub Desktop.
Save cranberyxl/669287 to your computer and use it in GitHub Desktop.
<?php
namespace Symfony\Component\Validator\Constraints;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class ValidValidator extends ConstraintValidator
{
public function isValid($value, Constraint $constraint)
{
if ($value === null) {
return true;
}
$walker = $this->context->getGraphWalker();
$group = $this->context->getGroup();
$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)) {
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 {
$metadata = $factory->getClassMetadata(get_class($value));
$walker->walkClass($metadata, $value, $group, $propertyPath);
if ($value instanceof \Traversable) {
foreach ($value as $key => $element) {
$walker->walkConstraint($constraint, $element, $group, $propertyPath.'['.$key.']');
}
}
}
return true;
}
}
<?php
namespace Symfony\Component\Validator\Constraints;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class ValidValidator extends ConstraintValidator
{
public function isValid($value, Constraint $constraint)
{
if ($value === null) {
return true;
}
$walker = $this->context->getGraphWalker();
$group = $this->context->getGroup();
$propertyPath = $this->context->getPropertyPath();
$factory = $this->context->getClassMetadataFactory();
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 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment