Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.7] Removed use of Legacy validator. #13458

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ public function testFullyConfiguredValidationService()

$container = $this->createContainerFromFile('full');

$this->assertInstanceOf('Symfony\Component\Validator\ValidatorInterface', $container->get('validator'));
$this->assertInstanceOf('Symfony\Component\Validator\Validator\ValidatorInterface', $container->get('validator'));
}

public function testValidationService()
{
$container = $this->createContainerFromFile('validation_annotations');

$this->assertInstanceOf('Symfony\Component\Validator\ValidatorInterface', $container->get('validator'));
$this->assertInstanceOf('Symfony\Component\Validator\Validator\ValidatorInterface', $container->get('validator'));
}

public function testAnnotations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testLegacyDefaultApiVersion()
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

// Legacy compatible implementation
$this->assertInstanceOf('Symfony\Component\Validator\Validator\LegacyValidator', $this->builder->getValidator());
$this->assertInstanceOf('Symfony\Component\Validator\Validator\RecursiveValidator', $this->builder->getValidator());
}

public function testSetApiVersion25()
Expand All @@ -129,6 +129,6 @@ public function testLegacySetApiVersion24And25()
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$this->assertSame($this->builder, $this->builder->setApiVersion(Validation::API_VERSION_2_5_BC));
$this->assertInstanceOf('Symfony\Component\Validator\Validator\LegacyValidator', $this->builder->getValidator());
$this->assertInstanceOf('Symfony\Component\Validator\Validator\RecursiveValidator', $this->builder->getValidator());
}
}
28 changes: 1 addition & 27 deletions src/Symfony/Component/Validator/Validator/LegacyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

namespace Symfony\Component\Validator\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;

/**
Expand All @@ -31,22 +28,9 @@ class LegacyValidator extends RecursiveValidator implements LegacyValidatorInter
{
public function validate($value, $groups = null, $traverse = false, $deep = false)
{
$numArgs = func_num_args();

// Use new signature if constraints are given in the second argument
if (self::testConstraints($groups) && ($numArgs < 3 || 3 === $numArgs && self::testGroups($traverse))) {
// Rename to avoid total confusion ;)
$constraints = $groups;
$groups = $traverse;

return parent::validate($value, $constraints, $groups);
}

trigger_error('The '.__METHOD__.' method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\Component\Validator\Validator\ValidatorInterface::validate method instead.', E_USER_DEPRECATED);

$constraint = new Valid(array('traverse' => $traverse, 'deep' => $deep));

return parent::validate($value, $constraint, $groups);
return parent::validate($value, false, $groups, $traverse, $deep);
}

public function validateValue($value, $constraints, $groups = null)
Expand All @@ -62,14 +46,4 @@ public function getMetadataFactory()

return $this->metadataFactory;
}

private static function testConstraints($constraints)
{
return null === $constraints || $constraints instanceof Constraint || (is_array($constraints) && current($constraints) instanceof Constraint);
}

private static function testGroups($groups)
{
return null === $groups || is_string($groups) || $groups instanceof GroupSequence || (is_array($groups) && (is_string(current($groups)) || current($groups) instanceof GroupSequence));
}
}
37 changes: 37 additions & 0 deletions src/Symfony/Component/Validator/Validator/RecursiveValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\Validator\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\Context\ExecutionContextFactoryInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
Expand Down Expand Up @@ -112,6 +115,22 @@ public function hasMetadataFor($object)
*/
public function validate($value, $constraints = null, $groups = null)
{
// The following code must be removed in 3.0
$numArgs = func_num_args();
if ($numArgs > 3) {
trigger_error('The '.__METHOD__.' method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\Component\Validator\Validator\ValidatorInterface::validate method instead.', E_USER_DEPRECATED);
$traverse = func_get_arg(3);
$deep = func_get_arg(4);
$constraints = new Valid(array('traverse' => $traverse, 'deep' => $deep));

if (self::testConstraints($groups)) {
if ((!$traverse && !$deep) || self::testGroups($traverse)) {
$constraints = $groups;
$groups = $traverse;
}
}
}

return $this->startContext($value)
->validate($value, $constraints, $groups)
->getViolations();
Expand All @@ -137,4 +156,22 @@ public function validatePropertyValue($objectOrClass, $propertyName, $value, $gr
->validatePropertyValue($objectOrClass, $propertyName, $value, $groups)
->getViolations();
}

/**
* @internal
* @deprecated since version 2.5, to be removed in 3.0.
*/
private static function testConstraints($constraints)
{
return null === $constraints || $constraints instanceof Constraint || (is_array($constraints) && current($constraints) instanceof Constraint);
}

/**
* @internal
* @deprecated since version 2.5, to be removed in 3.0.
*/
private static function testGroups($groups)
{
return null === $groups || is_string($groups) || $groups instanceof GroupSequence || (is_array($groups) && (is_string(current($groups)) || current($groups) instanceof GroupSequence));
}
}
12 changes: 2 additions & 10 deletions src/Symfony/Component/Validator/ValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Context\LegacyExecutionContextFactory;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
Expand All @@ -31,7 +30,6 @@
use Symfony\Component\Validator\Mapping\Loader\XmlFilesLoader;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\Mapping\Loader\YamlFilesLoader;
use Symfony\Component\Validator\Validator\LegacyValidator;
use Symfony\Component\Validator\Validator\RecursiveValidator;

/**
Expand Down Expand Up @@ -393,14 +391,8 @@ public function getValidator()
$translator->setLocale('en');
}

if (Validation::API_VERSION_2_5 === $apiVersion) {
$contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
$contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);

return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
}

$contextFactory = new LegacyExecutionContextFactory($metadataFactory, $translator, $this->translationDomain);

return new LegacyValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
}
}