Skip to content

Commit

Permalink
Complete the removal of API versions in the validator component
Browse files Browse the repository at this point in the history
Thanks to the PHP version requirement bump to 5.3.9+, the BC layer can
be available all the time.
  • Loading branch information
stof committed Mar 22, 2015
1 parent d20f8e2 commit 625816d
Show file tree
Hide file tree
Showing 51 changed files with 122 additions and 1,389 deletions.
105 changes: 82 additions & 23 deletions Context/ExecutionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\ClassBasedInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Exception\BadMethodCallException;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;

/**
Expand Down Expand Up @@ -187,11 +188,17 @@ public function addViolation($message, array $parameters = array(), $invalidValu
// API, as they are not present in the new interface anymore.
// You should use buildViolation() instead.
if (func_num_args() > 2) {
throw new BadMethodCallException(
'The parameters $invalidValue, $plural and $code are '.
'not supported anymore as of Symfony 2.5. Please use '.
'buildViolation() instead or enable the legacy mode.'
);
trigger_error('The parameters $invalidValue, $plural and $code in method '.__METHOD__.' are deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);

$this
->buildViolation($message, $parameters)
->setInvalidValue($invalidValue)
->setPlural($plural)
->setCode($code)
->addViolation()
;

return;
}

$this->violations->add(new ConstraintViolation(
Expand Down Expand Up @@ -310,44 +317,96 @@ public function getPropertyPath($subPath = '')
*/
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
{
throw new BadMethodCallException(
'addViolationAt() is not supported anymore as of Symfony 2.5. '.
'Please use buildViolation() instead or enable the legacy mode.'
);
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);

if (func_num_args() > 2) {
$this
->buildViolation($message, $parameters)
->atPath($subPath)
->setInvalidValue($invalidValue)
->setPlural($plural)
->setCode($code)
->addViolation()
;

return;
}

$this
->buildViolation($message, $parameters)
->atPath($subPath)
->addViolation()
;
}

/**
* {@inheritdoc}
*/
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
{
throw new BadMethodCallException(
'validate() is not supported anymore as of Symfony 2.5. '.
'Please use getValidator() instead or enable the legacy mode.'
);
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);

if (is_array($value)) {
// The $traverse flag is ignored for arrays
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, $constraint, $groups)
;
}

if ($traverse && $value instanceof \Traversable) {
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, $constraint, $groups)
;
}

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, null, $groups)
;
}

/**
* {@inheritdoc}
*/
public function validateValue($value, $constraints, $subPath = '', $groups = null)
{
throw new BadMethodCallException(
'validateValue() is not supported anymore as of Symfony 2.5. '.
'Please use getValidator() instead or enable the legacy mode.'
);
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, $constraints, $groups)
;
}

/**
* {@inheritdoc}
*/
public function getMetadataFactory()
{
throw new BadMethodCallException(
'getMetadataFactory() is not supported anymore as of Symfony 2.5. '.
'Please use getValidator() in combination with getMetadataFor() '.
'or hasMetadataFor() instead or enable the legacy mode.'
);
trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0. Use the new Symfony\Component\Validator\Context\ExecutionContext::getValidator method in combination with Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);

$validator = $this->getValidator();

if ($validator instanceof LegacyValidatorInterface) {
return $validator->getMetadataFactory();
}

// The ValidatorInterface extends from the deprecated MetadataFactoryInterface, so return it when we don't have the factory instance itself
return $validator;
}

/**
Expand Down
113 changes: 2 additions & 111 deletions Context/LegacyExecutionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace Symfony\Component\Validator\Context;

trigger_error('The '.__NAMESPACE__.'\LegacyExecutionContext class is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);

use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

Expand Down Expand Up @@ -50,114 +51,4 @@ public function __construct(ValidatorInterface $validator, $root, MetadataFactor

$this->metadataFactory = $metadataFactory;
}

/**
* {@inheritdoc}
*/
public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
{
if (func_num_args() > 2) {
trigger_error('The parameters $invalidValue, $plural and $code in method '.__METHOD__.' are deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);

$this
->buildViolation($message, $parameters)
->setInvalidValue($invalidValue)
->setPlural($plural)
->setCode($code)
->addViolation()
;

return;
}

parent::addViolation($message, $parameters);
}

/**
* {@inheritdoc}
*/
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
{
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);

if (func_num_args() > 2) {
$this
->buildViolation($message, $parameters)
->atPath($subPath)
->setInvalidValue($invalidValue)
->setPlural($plural)
->setCode($code)
->addViolation()
;

return;
}

$this
->buildViolation($message, $parameters)
->atPath($subPath)
->addViolation()
;
}

/**
* {@inheritdoc}
*/
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
{
if (is_array($value)) {
// The $traverse flag is ignored for arrays
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, $constraint, $groups)
;
}

if ($traverse && $value instanceof \Traversable) {
$constraint = new Valid(array('traverse' => true, 'deep' => $deep));

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, $constraint, $groups)
;
}

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, null, $groups)
;
}

/**
* {@inheritdoc}
*/
public function validateValue($value, $constraints, $subPath = '', $groups = null)
{
trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::validate method instead.', E_USER_DEPRECATED);

return $this
->getValidator()
->inContext($this)
->atPath($subPath)
->validate($value, $constraints, $groups)
;
}

/**
* {@inheritdoc}
*/
public function getMetadataFactory()
{
trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0. Use the new Symfony\Component\Validator\Context\ExecutionContext::getValidator method in combination with Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);

return $this->metadataFactory;
}
}
2 changes: 2 additions & 0 deletions Context/LegacyExecutionContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator\Context;

trigger_error('The '.__NAMESPACE__.'\LegacyExecutionContextFactory class is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);

use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down
6 changes: 4 additions & 2 deletions Tests/Constraints/AbstractConstraintValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext;
use Symfony\Component\Validator\Validation;

/**
Expand Down Expand Up @@ -303,7 +302,10 @@ protected function buildViolation($message)
return new ConstraintViolationAssertion($this->context, $message, $this->constraint);
}

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

abstract protected function createValidator();
}
Expand Down
27 changes: 0 additions & 27 deletions Tests/Constraints/LegacyAllValidatorLegacyApiTest.php

This file was deleted.

27 changes: 0 additions & 27 deletions Tests/Constraints/LegacyBlankValidatorLegacyApiTest.php

This file was deleted.

27 changes: 0 additions & 27 deletions Tests/Constraints/LegacyCallbackValidatorLegacyApiTest.php

This file was deleted.

Loading

0 comments on commit 625816d

Please sign in to comment.