[Validator] remove obsolete context and PropertyAccess code

This commit is contained in:
Tobias Schultze 2016-01-16 04:28:49 +01:00 committed by Fabien Potencier
parent 753811f73e
commit a9d9d62e9b
7 changed files with 11 additions and 89 deletions

View File

@ -38,7 +38,6 @@
</service> </service>
<service id="validator.expression" class="Symfony\Component\Validator\Constraints\ExpressionValidator"> <service id="validator.expression" class="Symfony\Component\Validator\Constraints\ExpressionValidator">
<argument type="service" id="property_accessor" />
<tag name="validator.constraint_validator" alias="validator.expression" /> <tag name="validator.constraint_validator" alias="validator.expression" />
</service> </service>

View File

@ -15,7 +15,6 @@ use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/** /**
@ -38,11 +37,8 @@ class FormValidator extends ConstraintValidator
/* @var FormInterface $form */ /* @var FormInterface $form */
$config = $form->getConfig(); $config = $form->getConfig();
$validator = null;
if ($this->context instanceof ExecutionContextInterface) { $validator = $this->context->getValidator()->inContext($this->context);
$validator = $this->context->getValidator()->inContext($this->context);
}
if ($form->isSynchronized()) { if ($form->isSynchronized()) {
// Validate the form data only if transformation succeeded // Validate the form data only if transformation succeeded
@ -51,12 +47,7 @@ class FormValidator extends ConstraintValidator
// Validate the data against its own constraints // Validate the data against its own constraints
if (self::allowDataWalking($form)) { if (self::allowDataWalking($form)) {
foreach ($groups as $group) { foreach ($groups as $group) {
if ($validator) { $validator->atPath('data')->validate($form->getData(), null, $group);
$validator->atPath('data')->validate($form->getData(), null, $group);
} else {
// 2.4 API
$this->context->validate($form->getData(), 'data', $group, true);
}
} }
} }
@ -66,12 +57,7 @@ class FormValidator extends ConstraintValidator
foreach ($constraints as $constraint) { foreach ($constraints as $constraint) {
// For the "Valid" constraint, validate the data in all groups // For the "Valid" constraint, validate the data in all groups
if ($constraint instanceof Valid) { if ($constraint instanceof Valid) {
if ($validator) { $validator->atPath('data')->validate($form->getData(), $constraint, $groups);
$validator->atPath('data')->validate($form->getData(), $constraint, $groups);
} else {
// 2.4 API
$this->context->validateValue($form->getData(), $constraint, 'data', $groups);
}
continue; continue;
} }
@ -80,12 +66,7 @@ class FormValidator extends ConstraintValidator
// matching group // matching group
foreach ($groups as $group) { foreach ($groups as $group) {
if (in_array($group, $constraint->groups)) { if (in_array($group, $constraint->groups)) {
if ($validator) { $validator->atPath('data')->validate($form->getData(), $constraint, $group);
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
} else {
// 2.4 API
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
}
// Prevent duplicate validation // Prevent duplicate validation
continue 2; continue 2;

View File

@ -26,11 +26,8 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{ {
protected $validators = array(); protected $validators = array();
private $propertyAccessor; public function __construct()
public function __construct($propertyAccessor = null)
{ {
$this->propertyAccessor = $propertyAccessor;
} }
/** /**
@ -42,7 +39,7 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
if (!isset($this->validators[$className])) { if (!isset($this->validators[$className])) {
$this->validators[$className] = 'validator.expression' === $className $this->validators[$className] = 'validator.expression' === $className
? new ExpressionValidator($this->propertyAccessor) ? new ExpressionValidator()
: new $className(); : new $className();
} }

View File

@ -12,12 +12,8 @@
namespace Symfony\Component\Validator\Constraints; namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\RuntimeException; use Symfony\Component\Validator\Exception\RuntimeException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@ -27,19 +23,13 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
*/ */
class ExpressionValidator extends ConstraintValidator class ExpressionValidator extends ConstraintValidator
{ {
/**
* @var PropertyAccessorInterface
*/
private $propertyAccessor;
/** /**
* @var ExpressionLanguage * @var ExpressionLanguage
*/ */
private $expressionLanguage; private $expressionLanguage;
public function __construct(PropertyAccessorInterface $propertyAccessor = null, ExpressionLanguage $expressionLanguage = null) public function __construct($propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
{ {
$this->propertyAccessor = $propertyAccessor;
$this->expressionLanguage = $expressionLanguage; $this->expressionLanguage = $expressionLanguage;
} }
@ -53,28 +43,8 @@ class ExpressionValidator extends ConstraintValidator
} }
$variables = array(); $variables = array();
$variables['value'] = $value;
// Symfony 2.5+ $variables['this'] = $this->context->getObject();
if ($this->context instanceof ExecutionContextInterface) {
$variables['value'] = $value;
$variables['this'] = $this->context->getObject();
} elseif (null === $this->context->getPropertyName()) {
$variables['value'] = $value;
$variables['this'] = $value;
} else {
$root = $this->context->getRoot();
$variables['value'] = $value;
if (is_object($root)) {
// Extract the object that the property belongs to from the object
// graph
$path = new PropertyPath($this->context->getPropertyPath());
$parentPath = $path->getParent();
$variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
} else {
$variables['this'] = null;
}
}
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) { if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->buildViolation($constraint->message) $this->context->buildViolation($constraint->message)
@ -95,16 +65,4 @@ class ExpressionValidator extends ConstraintValidator
return $this->expressionLanguage; return $this->expressionLanguage;
} }
private function getPropertyAccessor()
{
if (null === $this->propertyAccessor) {
if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
throw new RuntimeException('Unable to use expressions as the Symfony PropertyAccess component is not installed.');
}
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}
return $this->propertyAccessor;
}
} }

View File

@ -11,17 +11,15 @@
namespace Symfony\Component\Validator\Tests\Constraints; namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Constraints\Expression; use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\ExpressionValidator; use Symfony\Component\Validator\Constraints\ExpressionValidator;
use Symfony\Component\Validator\Tests\Fixtures\Entity; use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Validation;
class ExpressionValidatorTest extends AbstractConstraintValidatorTest class ExpressionValidatorTest extends AbstractConstraintValidatorTest
{ {
protected function createValidator() protected function createValidator()
{ {
return new ExpressionValidator(PropertyAccess::createPropertyAccessor()); return new ExpressionValidator();
} }
public function testExpressionIsEvaluatedWithNullValue() public function testExpressionIsEvaluatedWithNullValue()

View File

@ -15,7 +15,6 @@ use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader; use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader; use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Translation\IdentityTranslator; use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextFactory; use Symfony\Component\Validator\Context\ExecutionContextFactory;
@ -89,11 +88,6 @@ class ValidatorBuilder implements ValidatorBuilderInterface
*/ */
private $translationDomain; private $translationDomain;
/**
* @var PropertyAccessorInterface|null
*/
private $propertyAccessor;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -263,10 +257,6 @@ class ValidatorBuilder implements ValidatorBuilderInterface
*/ */
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory) public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
{ {
if (null !== $this->propertyAccessor) {
throw new ValidatorException('You cannot set a validator factory after setting a custom property accessor. Remove the call to setPropertyAccessor() if you want to call setConstraintValidatorFactory().');
}
$this->validatorFactory = $validatorFactory; $this->validatorFactory = $validatorFactory;
return $this; return $this;
@ -333,7 +323,7 @@ class ValidatorBuilder implements ValidatorBuilderInterface
$metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache); $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
} }
$validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($this->propertyAccessor); $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory();
$translator = $this->translator; $translator = $this->translator;
if (null === $translator) { if (null === $translator) {

View File

@ -24,7 +24,6 @@
"symfony/intl": "~2.8|~3.0", "symfony/intl": "~2.8|~3.0",
"symfony/yaml": "~2.8|~3.0", "symfony/yaml": "~2.8|~3.0",
"symfony/config": "~2.8|~3.0", "symfony/config": "~2.8|~3.0",
"symfony/property-access": "~2.8|~3.0",
"symfony/expression-language": "~2.8|~3.0", "symfony/expression-language": "~2.8|~3.0",
"doctrine/annotations": "~1.0", "doctrine/annotations": "~1.0",
"doctrine/cache": "~1.0", "doctrine/cache": "~1.0",