[Validator] Made "symfony/property-access" an optional dependency
This commit is contained in:
parent
c5629bb7ee
commit
3dc2b4d861
@ -57,6 +57,8 @@ CHANGELOG
|
|||||||
* added `Exception\UnsupportedMetadataException`
|
* added `Exception\UnsupportedMetadataException`
|
||||||
* made `Exception\ValidatorException` extend `Exception\RuntimeException`
|
* made `Exception\ValidatorException` extend `Exception\RuntimeException`
|
||||||
* added `Util\PropertyPath`
|
* added `Util\PropertyPath`
|
||||||
|
* made the PropertyAccess component an optional dependency
|
||||||
|
* deprecated `ValidatorBuilder::setPropertyAccessor()`
|
||||||
|
|
||||||
|
|
||||||
2.4.0
|
2.4.0
|
||||||
|
@ -11,8 +11,6 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Validator;
|
namespace Symfony\Component\Validator;
|
||||||
|
|
||||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
|
||||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
|
||||||
use Symfony\Component\Validator\Constraints\ExpressionValidator;
|
use Symfony\Component\Validator\Constraints\ExpressionValidator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,14 +26,11 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
|
|||||||
{
|
{
|
||||||
protected $validators = array();
|
protected $validators = array();
|
||||||
|
|
||||||
/**
|
|
||||||
* @var PropertyAccessorInterface
|
|
||||||
*/
|
|
||||||
private $propertyAccessor;
|
private $propertyAccessor;
|
||||||
|
|
||||||
public function __construct(PropertyAccessorInterface $propertyAccessor = null)
|
public function __construct($propertyAccessor = null)
|
||||||
{
|
{
|
||||||
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
|
$this->propertyAccessor = $propertyAccessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,10 +12,12 @@
|
|||||||
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\PropertyAccessorInterface;
|
||||||
use Symfony\Component\PropertyAccess\PropertyPath;
|
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;
|
||||||
|
|
||||||
@ -35,8 +37,17 @@ class ExpressionValidator extends ConstraintValidator
|
|||||||
*/
|
*/
|
||||||
private $expressionLanguage;
|
private $expressionLanguage;
|
||||||
|
|
||||||
public function __construct(PropertyAccessorInterface $propertyAccessor)
|
/**
|
||||||
|
* @param PropertyAccessorInterface|null $propertyAccessor Optional as of Symfony 2.5
|
||||||
|
*
|
||||||
|
* @throws UnexpectedTypeException If the property accessor is invalid
|
||||||
|
*/
|
||||||
|
public function __construct($propertyAccessor = null)
|
||||||
{
|
{
|
||||||
|
if (null !== $propertyAccessor && !$propertyAccessor instanceof PropertyAccessorInterface) {
|
||||||
|
throw new UnexpectedTypeException($propertyAccessor, 'null or \Symfony\Component\PropertyAccess\PropertyAccessorInterface');
|
||||||
|
}
|
||||||
|
|
||||||
$this->propertyAccessor = $propertyAccessor;
|
$this->propertyAccessor = $propertyAccessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +66,12 @@ class ExpressionValidator extends ConstraintValidator
|
|||||||
|
|
||||||
$variables = array();
|
$variables = array();
|
||||||
|
|
||||||
if (null === $this->context->getPropertyName()) {
|
// Symfony 2.5+
|
||||||
|
if ($this->context instanceof ExecutionContextInterface) {
|
||||||
|
$variables['value'] = $value;
|
||||||
|
$variables['this'] = $this->context->getObject();
|
||||||
|
} elseif (null === $this->context->getPropertyName()) {
|
||||||
|
$variables['value'] = $value;
|
||||||
$variables['this'] = $value;
|
$variables['this'] = $value;
|
||||||
} else {
|
} else {
|
||||||
// Extract the object that the property belongs to from the object
|
// Extract the object that the property belongs to from the object
|
||||||
@ -65,7 +81,7 @@ class ExpressionValidator extends ConstraintValidator
|
|||||||
$root = $this->context->getRoot();
|
$root = $this->context->getRoot();
|
||||||
|
|
||||||
$variables['value'] = $value;
|
$variables['value'] = $value;
|
||||||
$variables['this'] = $parentPath ? $this->propertyAccessor->getValue($root, $parentPath) : $root;
|
$variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
|
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
|
||||||
@ -84,4 +100,16 @@ 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ use Doctrine\Common\Annotations\AnnotationRegistry;
|
|||||||
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\PropertyAccess;
|
|
||||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
use Symfony\Component\Validator\Context\LegacyExecutionContextFactory;
|
use Symfony\Component\Validator\Context\LegacyExecutionContextFactory;
|
||||||
@ -395,8 +394,7 @@ class ValidatorBuilder implements ValidatorBuilderInterface
|
|||||||
$metadataFactory = new ClassMetadataFactory($loader, $this->metadataCache);
|
$metadataFactory = new ClassMetadataFactory($loader, $this->metadataCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
$propertyAccessor = $this->propertyAccessor ?: PropertyAccess::createPropertyAccessor();
|
$validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($this->propertyAccessor);
|
||||||
$validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($propertyAccessor);
|
|
||||||
$translator = $this->translator ?: new DefaultTranslator();
|
$translator = $this->translator ?: new DefaultTranslator();
|
||||||
$apiVersion = $this->apiVersion;
|
$apiVersion = $this->apiVersion;
|
||||||
|
|
||||||
|
@ -166,6 +166,8 @@ interface ValidatorBuilderInterface
|
|||||||
* @param PropertyAccessorInterface $propertyAccessor The property accessor
|
* @param PropertyAccessorInterface $propertyAccessor The property accessor
|
||||||
*
|
*
|
||||||
* @return ValidatorBuilderInterface The builder object
|
* @return ValidatorBuilderInterface The builder object
|
||||||
|
*
|
||||||
|
* @deprecated Deprecated since version 2.5, to be removed in Symfony 3.0.
|
||||||
*/
|
*/
|
||||||
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor);
|
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor);
|
||||||
|
|
||||||
|
@ -17,14 +17,14 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.3",
|
"php": ">=5.3.3",
|
||||||
"symfony/translation": "~2.0",
|
"symfony/translation": "~2.0"
|
||||||
"symfony/property-access": "~2.2"
|
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"symfony/http-foundation": "~2.1",
|
"symfony/http-foundation": "~2.1",
|
||||||
"symfony/intl": "~2.3",
|
"symfony/intl": "~2.3",
|
||||||
"symfony/yaml": "~2.0",
|
"symfony/yaml": "~2.0",
|
||||||
"symfony/config": "~2.2",
|
"symfony/config": "~2.2",
|
||||||
|
"symfony/property-access": "~2.2",
|
||||||
"doctrine/annotations": "~1.0",
|
"doctrine/annotations": "~1.0",
|
||||||
"doctrine/cache": "~1.0",
|
"doctrine/cache": "~1.0",
|
||||||
"egulias/email-validator": "~1.0"
|
"egulias/email-validator": "~1.0"
|
||||||
@ -37,6 +37,7 @@
|
|||||||
"symfony/yaml": "",
|
"symfony/yaml": "",
|
||||||
"symfony/config": "",
|
"symfony/config": "",
|
||||||
"egulias/email-validator": "Strict (RFC compliant) email validation"
|
"egulias/email-validator": "Strict (RFC compliant) email validation"
|
||||||
|
"symfony/property-access": "For using the 2.4 Validator API"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": { "Symfony\\Component\\Validator\\": "" }
|
"psr-0": { "Symfony\\Component\\Validator\\": "" }
|
||||||
|
Reference in New Issue
Block a user