[Validator] Checked the constraint class in constraint validators

This commit is contained in:
Bernhard Schussek 2014-03-10 12:51:04 +00:00
parent 7baeaa2fd7
commit df56c23d8f
36 changed files with 182 additions and 0 deletions

View File

@ -46,6 +46,10 @@ class UniqueEntityValidator extends ConstraintValidator
*/
public function validate($entity, Constraint $constraint)
{
if (!$constraint instanceof UniqueEntity) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UniqueEntity');
}
if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
throw new UnexpectedTypeException($constraint->fields, 'array');
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -42,6 +43,10 @@ class FormValidator extends ConstraintValidator
*/
public function validate($form, Constraint $constraint)
{
if (!$constraint instanceof Form) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Form');
}
if (!$form instanceof FormInterface) {
return;
}

View File

@ -17,6 +17,7 @@ use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class UserPasswordValidator extends ConstraintValidator
{
@ -34,6 +35,10 @@ class UserPasswordValidator extends ConstraintValidator
*/
public function validate($password, Constraint $constraint)
{
if (!$constraint instanceof UserPassword) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword');
}
$user = $this->securityContext->getToken()->getUser();
if (!$user instanceof UserInterface) {

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Provides a base class for the validation of property comparisons.
@ -26,6 +27,10 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof AbstractComparison) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
}
if (null === $value) {
return;
}

View File

@ -27,6 +27,10 @@ class AllValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof All) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All');
}
if (null === $value) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -26,6 +27,10 @@ class BlankValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Blank) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Blank');
}
if ('' !== $value && null !== $value) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}

View File

@ -30,6 +30,10 @@ class CallbackValidator extends ConstraintValidator
*/
public function validate($object, Constraint $constraint)
{
if (!$constraint instanceof Callback) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Callback');
}
if (null === $object) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Validates that a card number belongs to a specified scheme.
@ -103,6 +104,10 @@ class CardSchemeValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof CardScheme) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -32,6 +32,10 @@ class ChoiceValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Choice) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
}
if (!$constraint->choices && !$constraint->callback) {
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
}

View File

@ -27,6 +27,10 @@ class CollectionValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Collection) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Collection');
}
if (null === $value) {
return;
}

View File

@ -30,6 +30,10 @@ class CountryValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Country) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Country');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -30,6 +30,10 @@ class CurrencyValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Currency) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Currency');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -11,6 +11,9 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
@ -19,4 +22,28 @@ namespace Symfony\Component\Validator\Constraints;
class DateTimeValidator extends DateValidator
{
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof DateTime) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
}
if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}
}
}

View File

@ -29,6 +29,10 @@ class DateValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Date) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
}
if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}

View File

@ -27,6 +27,10 @@ class EmailValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Email) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -17,6 +17,7 @@ use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Exception\RuntimeException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Fabien Potencier <fabien@symfony.com>
@ -44,6 +45,10 @@ class ExpressionValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Expression) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -26,6 +27,10 @@ class FalseValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof False) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\False');
}
if (null === $value || false === $value || 0 === $value || '0' === $value) {
return;
}

View File

@ -30,6 +30,10 @@ class FileValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof File) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\File');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Manuel Reinhard <manu@sprain.ch>
@ -26,6 +27,10 @@ class IbanValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Iban) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Iban');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Validates whether a value is a valid image file and is valid
@ -27,6 +28,10 @@ class ImageValidator extends FileValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Image) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Image');
}
$violations = count($this->context->getViolations());
parent::validate($value, $constraint);

View File

@ -30,6 +30,10 @@ class IpValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Ip) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Ip');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -29,6 +29,10 @@ class IsbnValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Isbn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Isbn');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -29,6 +29,10 @@ class IssnValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Issn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Issn');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -30,6 +30,10 @@ class LanguageValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Language) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Language');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -25,6 +25,10 @@ class LengthValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Length) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -30,6 +30,10 @@ class LocaleValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Locale) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Locale');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -35,6 +35,10 @@ class LuhnValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Luhn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -26,6 +27,10 @@ class NotBlankValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof NotBlank) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotBlank');
}
if (false === $value || (empty($value) && '0' != $value)) {
$this->context->addViolation($constraint->message);
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -26,6 +27,10 @@ class NotNullValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof NotNull) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotNull');
}
if (null === $value) {
$this->context->addViolation($constraint->message);
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -26,6 +27,10 @@ class NullValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Null) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Null');
}
if (null !== $value) {
if (is_object($value)) {
$value = get_class($value);

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -24,6 +25,10 @@ class RangeValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Range) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Range');
}
if (null === $value) {
return;
}

View File

@ -30,6 +30,10 @@ class RegexValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Regex) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Regex');
}
if (null === $value || '' === $value) {
return;
}

View File

@ -29,6 +29,10 @@ class TimeValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Time) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Time');
}
if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -26,6 +27,10 @@ class TrueValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof True) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\True');
}
if (null === $value) {
return;
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
@ -26,6 +27,10 @@ class TypeValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Type) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Type');
}
if (null === $value) {
return;
}

View File

@ -42,6 +42,10 @@ class UrlValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Url) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Url');
}
if (null === $value || '' === $value) {
return;
}