Skip to content

Commit 3dc2b4d

Browse files
committed
[Validator] Made "symfony/property-access" an optional dependency
1 parent c5629bb commit 3dc2b4d

File tree

6 files changed

+41
-15
lines changed

6 files changed

+41
-15
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ CHANGELOG
5757
* added `Exception\UnsupportedMetadataException`
5858
* made `Exception\ValidatorException` extend `Exception\RuntimeException`
5959
* added `Util\PropertyPath`
60+
* made the PropertyAccess component an optional dependency
61+
* deprecated `ValidatorBuilder::setPropertyAccessor()`
6062

6163

6264
2.4.0

src/Symfony/Component/Validator/ConstraintValidatorFactory.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Validator;
1313

14-
use Symfony\Component\PropertyAccess\PropertyAccess;
15-
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1614
use Symfony\Component\Validator\Constraints\ExpressionValidator;
1715

1816
/**
@@ -28,14 +26,11 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
2826
{
2927
protected $validators = array();
3028

31-
/**
32-
* @var PropertyAccessorInterface
33-
*/
3429
private $propertyAccessor;
3530

36-
public function __construct(PropertyAccessorInterface $propertyAccessor = null)
31+
public function __construct($propertyAccessor = null)
3732
{
38-
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
33+
$this->propertyAccessor = $propertyAccessor;
3934
}
4035

4136
/**

src/Symfony/Component/Validator/Constraints/ExpressionValidator.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15+
use Symfony\Component\PropertyAccess\PropertyAccess;
1516
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1617
use Symfony\Component\PropertyAccess\PropertyPath;
1718
use Symfony\Component\Validator\Constraint;
1819
use Symfony\Component\Validator\ConstraintValidator;
20+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1921
use Symfony\Component\Validator\Exception\RuntimeException;
2022
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
2123

@@ -35,8 +37,17 @@ class ExpressionValidator extends ConstraintValidator
3537
*/
3638
private $expressionLanguage;
3739

38-
public function __construct(PropertyAccessorInterface $propertyAccessor)
40+
/**
41+
* @param PropertyAccessorInterface|null $propertyAccessor Optional as of Symfony 2.5
42+
*
43+
* @throws UnexpectedTypeException If the property accessor is invalid
44+
*/
45+
public function __construct($propertyAccessor = null)
3946
{
47+
if (null !== $propertyAccessor && !$propertyAccessor instanceof PropertyAccessorInterface) {
48+
throw new UnexpectedTypeException($propertyAccessor, 'null or \Symfony\Component\PropertyAccess\PropertyAccessorInterface');
49+
}
50+
4051
$this->propertyAccessor = $propertyAccessor;
4152
}
4253

@@ -55,7 +66,12 @@ public function validate($value, Constraint $constraint)
5566

5667
$variables = array();
5768

58-
if (null === $this->context->getPropertyName()) {
69+
// Symfony 2.5+
70+
if ($this->context instanceof ExecutionContextInterface) {
71+
$variables['value'] = $value;
72+
$variables['this'] = $this->context->getObject();
73+
} elseif (null === $this->context->getPropertyName()) {
74+
$variables['value'] = $value;
5975
$variables['this'] = $value;
6076
} else {
6177
// Extract the object that the property belongs to from the object
@@ -65,7 +81,7 @@ public function validate($value, Constraint $constraint)
6581
$root = $this->context->getRoot();
6682

6783
$variables['value'] = $value;
68-
$variables['this'] = $parentPath ? $this->propertyAccessor->getValue($root, $parentPath) : $root;
84+
$variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
6985
}
7086

7187
if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
@@ -84,4 +100,16 @@ private function getExpressionLanguage()
84100

85101
return $this->expressionLanguage;
86102
}
103+
104+
private function getPropertyAccessor()
105+
{
106+
if (null === $this->propertyAccessor) {
107+
if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
108+
throw new RuntimeException('Unable to use expressions as the Symfony PropertyAccess component is not installed.');
109+
}
110+
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
111+
}
112+
113+
return $this->propertyAccessor;
114+
}
87115
}

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Doctrine\Common\Annotations\CachedReader;
1717
use Doctrine\Common\Annotations\Reader;
1818
use Doctrine\Common\Cache\ArrayCache;
19-
use Symfony\Component\PropertyAccess\PropertyAccess;
2019
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2120
use Symfony\Component\Translation\TranslatorInterface;
2221
use Symfony\Component\Validator\Context\LegacyExecutionContextFactory;
@@ -395,8 +394,7 @@ public function getValidator()
395394
$metadataFactory = new ClassMetadataFactory($loader, $this->metadataCache);
396395
}
397396

398-
$propertyAccessor = $this->propertyAccessor ?: PropertyAccess::createPropertyAccessor();
399-
$validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($propertyAccessor);
397+
$validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($this->propertyAccessor);
400398
$translator = $this->translator ?: new DefaultTranslator();
401399
$apiVersion = $this->apiVersion;
402400

src/Symfony/Component/Validator/ValidatorBuilderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ public function setTranslationDomain($translationDomain);
166166
* @param PropertyAccessorInterface $propertyAccessor The property accessor
167167
*
168168
* @return ValidatorBuilderInterface The builder object
169+
*
170+
* @deprecated Deprecated since version 2.5, to be removed in Symfony 3.0.
169171
*/
170172
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor);
171173

src/Symfony/Component/Validator/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
],
1818
"require": {
1919
"php": ">=5.3.3",
20-
"symfony/translation": "~2.0",
21-
"symfony/property-access": "~2.2"
20+
"symfony/translation": "~2.0"
2221
},
2322
"require-dev": {
2423
"symfony/http-foundation": "~2.1",
2524
"symfony/intl": "~2.3",
2625
"symfony/yaml": "~2.0",
2726
"symfony/config": "~2.2",
27+
"symfony/property-access": "~2.2",
2828
"doctrine/annotations": "~1.0",
2929
"doctrine/cache": "~1.0",
3030
"egulias/email-validator": "~1.0"
@@ -37,6 +37,7 @@
3737
"symfony/yaml": "",
3838
"symfony/config": "",
3939
"egulias/email-validator": "Strict (RFC compliant) email validation"
40+
"symfony/property-access": "For using the 2.4 Validator API"
4041
},
4142
"autoload": {
4243
"psr-0": { "Symfony\\Component\\Validator\\": "" }

0 commit comments

Comments
 (0)