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

Skip to content

Remove the API version in the validator component #13517

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 2 commits 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 @@ -53,6 +53,14 @@ public function getConfigTreeBuilder()
return $v;
})
->end()
->beforeNormalization()
->ifTrue(function ($v) { return isset($v['validation']['api']); })
->then(function ($v) {
trigger_error('The validation.api configuration key is deprecated since version 2.7 and will be removed in 3.0', E_USER_DEPRECATED);

return $v;
})
->end()
->children()
->scalarNode('secret')->end()
->scalarNode('http_method_override')
Expand Down Expand Up @@ -495,6 +503,7 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
->scalarNode('translation_domain')->defaultValue('validators')->end()
->booleanNode('strict_email')->defaultFalse()->end()
->enumNode('api')
->info('Deprecated since version 2.7, to be removed in 3.0')
->values(array('2.4', '2.5', '2.5-bc', 'auto'))
->beforeNormalization()
// XML/YAML parse as numbers, not as strings
Expand All @@ -505,19 +514,6 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
->end()
->end()
->end()
->validate()
->ifTrue(function ($v) { return !isset($v['validation']['api']) || 'auto' === $v['validation']['api']; })
->then(function ($v) {
// This condition is duplicated in ValidatorBuilder. This
// duplication is necessary in order to know the desired
// API version already during container configuration
// (to adjust service classes etc.)
// See https://github.com/symfony/symfony/issues/11580
$v['validation']['api'] = '2.5-bc';

return $v;
})
->end()
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,20 +756,10 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
$validatorBuilder->addMethodCall('setMetadataCache', array(new Reference($config['cache'])));
}

if ('2.5' === $config['api']) {
$api = Validation::API_VERSION_2_5;
} else {
// 2.4 is now the same as 2.5 BC
$api = Validation::API_VERSION_2_5_BC;
// the validation class needs to be changed for BC
$container->setParameter('validator.class', 'Symfony\Component\Validator\ValidatorInterface');
}

$validatorBuilder->addMethodCall('setApiVersion', array($api));

// You can use this parameter to check the API version in your own
// bundle extension classes
$container->setParameter('validator.api', $api);
// @deprecated since version 2.7, to be removed in 3.0
$container->setParameter('validator.api', $config['api']);
}

private function getValidatorMappingFiles(ContainerBuilder $container)
Expand Down
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
19 changes: 1 addition & 18 deletions src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,8 @@ public function testSetTranslationDomain()
$this->assertSame($this->builder, $this->builder->setTranslationDomain('TRANS_DOMAIN'));
}

public function testLegacyDefaultApiVersion()
public function testGetValidator()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

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

public function testSetApiVersion25()
{
$this->assertSame($this->builder, $this->builder->setApiVersion(Validation::API_VERSION_2_5));
$this->assertInstanceOf('Symfony\Component\Validator\Validator\RecursiveValidator', $this->builder->getValidator());
}

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());
}
}
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);
Copy link
Member

Choose a reason for hiding this comment

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

the method itself is not deprecated. Only the signature with more arguments

$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
Copy link
Member

Choose a reason for hiding this comment

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

no need to mark as internal. It is private already

* @deprecated since version 2.5, to be removed in 3.0.
Copy link
Member

Choose a reason for hiding this comment

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

no need to mark as deprecated. It is a private utility method

*/
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));
}
}
33 changes: 7 additions & 26 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 @@ -96,11 +94,6 @@ class ValidatorBuilder implements ValidatorBuilderInterface
*/
private $propertyAccessor;

/**
* @var int|null
*/
private $apiVersion;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -320,18 +313,17 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)

/**
* {@inheritdoc}
*
* @deprecated since version 2.7, to be removed in 3.0.
*/
public function setApiVersion($apiVersion)
{
trigger_error('The '.__METHOD__.' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);

if (!in_array($apiVersion, array(Validation::API_VERSION_2_4, Validation::API_VERSION_2_5, Validation::API_VERSION_2_5_BC))) {
throw new InvalidArgumentException(sprintf(
'The requested API version is invalid: "%s"',
$apiVersion
));
throw new InvalidArgumentException(sprintf('The requested API version is invalid: "%s"', $apiVersion));
}

$this->apiVersion = $apiVersion;

return $this;
}

Expand All @@ -341,11 +333,6 @@ public function setApiVersion($apiVersion)
public function getValidator()
{
$metadataFactory = $this->metadataFactory;
$apiVersion = $this->apiVersion;

if (null === $apiVersion) {
$apiVersion = Validation::API_VERSION_2_5_BC;
}

if (!$metadataFactory) {
$loaders = array();
Expand Down Expand Up @@ -393,14 +380,8 @@ public function getValidator()
$translator->setLocale('en');
}

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

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

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

return new LegacyValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/ValidatorBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
*
* @see Validation::API_VERSION_2_5
* @see Validation::API_VERSION_2_5_BC
*
* @deprecated since version 2.7, to be removed in 3.0.
*/
public function setApiVersion($apiVersion);

Expand Down