Skip to content

Commit

Permalink
[Validator] remove the API_VERSION
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 1, 2015
1 parent 2a6b629 commit 8fd32ba
Show file tree
Hide file tree
Showing 50 changed files with 20 additions and 1,012 deletions.
Expand Up @@ -22,7 +22,6 @@
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
use Symfony\Component\Validator\Validation;
use Doctrine\ORM\Tools\SchemaTool;

/**
Expand All @@ -47,11 +46,6 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
*/
protected $repository;

protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function setUp()
{
$this->em = DoctrineTestHelper::createTestEntityManager();
Expand Down
Expand Up @@ -22,7 +22,6 @@
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
use Symfony\Component\Validator\Validation;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -56,11 +55,6 @@ protected function setUp()
parent::setUp();
}

protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new FormValidator($this->serverParams);
Expand Down
16 changes: 4 additions & 12 deletions src/Symfony/Component/Validator/Constraints/AllValidator.php
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
Expand All @@ -40,17 +39,10 @@ public function validate($value, Constraint $constraint)

$context = $this->context;

if ($context instanceof ExecutionContextInterface) {
$validator = $context->getValidator()->inContext($context);

foreach ($value as $key => $element) {
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
}
} else {
// 2.4 API
foreach ($value as $key => $element) {
$context->validateValue($element, $constraint->constraints, '['.$key.']');
}
$validator = $context->getValidator()->inContext($context);

foreach ($value as $key => $element) {
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
}
}
}
56 changes: 16 additions & 40 deletions src/Symfony/Component/Validator/Constraints/CollectionValidator.php
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
Expand Down Expand Up @@ -56,53 +55,30 @@ public function validate($value, Constraint $constraint)

if ($existsInArray || $existsInArrayAccess) {
if (count($fieldConstraint->constraints) > 0) {
if ($context instanceof ExecutionContextInterface) {
$context->getValidator()
->inContext($context)
->atPath('['.$field.']')
->validate($value[$field], $fieldConstraint->constraints);
} else {
// 2.4 API
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']');
}
}
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
if ($context instanceof ExecutionContextInterface) {
$context->buildViolation($constraint->missingFieldsMessage)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue(null)
->setCode(Collection::MISSING_FIELD_ERROR)
->addViolation();
} else {
$this->buildViolationInContext($context, $constraint->missingFieldsMessage)
$context->getValidator()
->inContext($context)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue(null)
->setCode(Collection::MISSING_FIELD_ERROR)
->addViolation();
->validate($value[$field], $fieldConstraint->constraints);
}
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$context->buildViolation($constraint->missingFieldsMessage)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue(null)
->setCode(Collection::MISSING_FIELD_ERROR)
->addViolation();
}
}

if (!$constraint->allowExtraFields) {
foreach ($value as $field => $fieldValue) {
if (!isset($constraint->fields[$field])) {
if ($context instanceof ExecutionContextInterface) {
$context->buildViolation($constraint->extraFieldsMessage)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue($fieldValue)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->addViolation();
} else {
$this->buildViolationInContext($context, $constraint->extraFieldsMessage)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue($fieldValue)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->addViolation();
}
$context->buildViolation($constraint->extraFieldsMessage)
->atPath('['.$field.']')
->setParameter('{{ field }}', $this->formatValue($field))
->setInvalidValue($fieldValue)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->addViolation();
}
}
}
Expand Down
Expand Up @@ -204,11 +204,6 @@ protected function buildViolation($message)
return new ConstraintViolationAssertion($this->context, $message, $this->constraint);
}

protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

abstract protected function createValidator();
}

Expand Down
Expand Up @@ -19,11 +19,6 @@

class AllValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new AllValidator();
Expand Down
Expand Up @@ -17,11 +17,6 @@

class BlankValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new BlankValidator();
Expand Down
Expand Up @@ -46,11 +46,6 @@ public static function validateStatic($object, ExecutionContextInterface $contex

class CallbackValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new CallbackValidator();
Expand Down
Expand Up @@ -17,11 +17,6 @@

class CardSchemeValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new CardSchemeValidator();
Expand Down
Expand Up @@ -22,11 +22,6 @@ function choice_callback()

class ChoiceValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new ChoiceValidator();
Expand Down
Expand Up @@ -21,11 +21,6 @@

abstract class CollectionValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new CollectionValidator();
Expand Down
Expand Up @@ -20,11 +20,6 @@
*/
abstract class CountValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new CountValidator();
Expand Down
Expand Up @@ -18,11 +18,6 @@

class CountryValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new CountryValidator();
Expand Down
Expand Up @@ -18,11 +18,6 @@

class CurrencyValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new CurrencyValidator();
Expand Down
Expand Up @@ -17,11 +17,6 @@

class DateTimeValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new DateTimeValidator();
Expand Down
Expand Up @@ -17,11 +17,6 @@

class DateValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new DateValidator();
Expand Down
Expand Up @@ -17,11 +17,6 @@

class EmailValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new EmailValidator(false);
Expand Down
Expand Up @@ -20,11 +20,6 @@
*/
class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new EqualToValidator();
Expand Down
Expand Up @@ -19,11 +19,6 @@

class ExpressionValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new ExpressionValidator(PropertyAccess::createPropertyAccessor());
Expand Down
Expand Up @@ -22,11 +22,6 @@ abstract class FileValidatorTest extends AbstractConstraintValidatorTest

protected $file;

protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new FileValidator();
Expand Down
Expand Up @@ -20,11 +20,6 @@
*/
class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new GreaterThanOrEqualValidator();
Expand Down
Expand Up @@ -20,11 +20,6 @@
*/
class GreaterThanValidatorTest extends AbstractComparisonValidatorTestCase
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new GreaterThanValidator();
Expand Down
Expand Up @@ -17,11 +17,6 @@

class IbanValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new IbanValidator();
Expand Down
Expand Up @@ -20,11 +20,6 @@
*/
class IdenticalToValidatorTest extends AbstractComparisonValidatorTestCase
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new IdenticalToValidator();
Expand Down
Expand Up @@ -33,11 +33,6 @@ class ImageValidatorTest extends AbstractConstraintValidatorTest
protected $imagePortrait;
protected $image4By3;

protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}

protected function createValidator()
{
return new ImageValidator();
Expand Down

1 comment on commit 8fd32ba

@SoboLAN
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is the most beatiful thing I've seen today. That whole API_VERSION thing was kind of a pain-in-the-ass.

Really awesome job 👍

Please sign in to comment.