Commit 373d925d authored by alexpott's avatar alexpott

Issue #2343035 by fago, dawehner, larowlan, ianthomas_uk, cilefen, hussainweb,...

Issue #2343035 by fago, dawehner, larowlan, ianthomas_uk, cilefen, hussainweb, Miroling, jibran, beejeebus, dashaforbes, webmozart, klausi: Upgrade validator integration for Symfony versions 2.5+
parent d57ee5f9
......@@ -14,7 +14,7 @@
"symfony/http-kernel": "2.6.*",
"symfony/routing": "2.6.*",
"symfony/serializer": "2.6.*",
"symfony/validator": "2.6.*",
"symfony/validator": "2.6.*@dev",
"symfony/process": "2.6.*",
"symfony/yaml": "2.6.*",
"twig/twig": "1.18.*",
......
......@@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "c523fe48318d98a520d2bc45286106e2",
"hash": "ba1a97bf2c0bcef4fb771231a4c1fbdb",
"packages": [
{
"name": "behat/mink",
......@@ -461,7 +461,7 @@
},
"dist": {
"type": "zip",
"url": "https://github.com/doctrine/inflector/archive/v1.0.zip",
"url": "https://api.github.com/repos/doctrine/inflector/zipball/54b8333d2a5682afdc690060c1cf384ba9f47f08",
"reference": "v1.0",
"shasum": ""
},
......@@ -578,7 +578,7 @@
},
"dist": {
"type": "zip",
"url": "https://github.com/doctrine/lexer/archive/v1.0.zip",
"url": "https://api.github.com/repos/doctrine/lexer/zipball/2f708a85bb3aab5d99dab8be435abd73e0b18acb",
"reference": "v1.0",
"shasum": ""
},
......@@ -1530,12 +1530,12 @@
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log",
"url": "https://github.com/php-fig/log.git",
"reference": "1.0.0"
},
"dist": {
"type": "zip",
"url": "https://github.com/php-fig/log/archive/1.0.0.zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/fe0936ee26643249e916849d48e3a51d5f5e278b",
"reference": "1.0.0",
"shasum": ""
},
......@@ -2891,17 +2891,17 @@
},
{
"name": "symfony/validator",
"version": "v2.6.6",
"version": "2.6.x-dev",
"target-dir": "Symfony/Component/Validator",
"source": {
"type": "git",
"url": "https://github.com/symfony/Validator.git",
"reference": "85d9b42fe71bf88e7a1e5dec2094605dc9fbff28"
"reference": "6bb1b474d25cb80617d8da6cb14c955ba914e495"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/Validator/zipball/85d9b42fe71bf88e7a1e5dec2094605dc9fbff28",
"reference": "85d9b42fe71bf88e7a1e5dec2094605dc9fbff28",
"url": "https://api.github.com/repos/symfony/Validator/zipball/6bb1b474d25cb80617d8da6cb14c955ba914e495",
"reference": "6bb1b474d25cb80617d8da6cb14c955ba914e495",
"shasum": ""
},
"require": {
......@@ -2948,18 +2948,18 @@
"MIT"
],
"authors": [
{
"name": "Symfony Community",
"homepage": "http://symfony.com/contributors"
},
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Validator Component",
"homepage": "http://symfony.com",
"time": "2015-03-30 15:54:10"
"homepage": "https://symfony.com",
"time": "2015-05-05 01:29:27"
},
{
"name": "symfony/yaml",
......@@ -3221,7 +3221,9 @@
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"stability-flags": {
"symfony/validator": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
......
......@@ -13,11 +13,11 @@
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\TypedData\Validation\MetadataFactory;
use Drupal\Core\TypedData\Validation\ExecutionContextFactory;
use Drupal\Core\TypedData\Validation\RecursiveValidator;
use Drupal\Core\Validation\ConstraintManager;
use Drupal\Core\Validation\ConstraintValidatorFactory;
use Drupal\Core\Validation\DrupalTranslator;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
......@@ -28,7 +28,7 @@ class TypedDataManager extends DefaultPluginManager {
/**
* The validator used for validating typed data.
*
* @var \Symfony\Component\Validator\ValidatorInterface
* @var \Symfony\Component\Validator\Validator\ValidatorInterface
*/
protected $validator;
......@@ -331,12 +331,11 @@ public function setValidator(ValidatorInterface $validator) {
*/
public function getValidator() {
if (!isset($this->validator)) {
$this->validator = Validation::createValidatorBuilder()
->setMetadataFactory(new MetadataFactory($this))
->setTranslator(new DrupalTranslator())
->setConstraintValidatorFactory(new ConstraintValidatorFactory($this->classResolver))
->setApiVersion(Validation::API_VERSION_2_4)
->getValidator();
$this->validator = new RecursiveValidator(
new ExecutionContextFactory(new DrupalTranslator()),
new ConstraintValidatorFactory($this->classResolver),
$this
);
}
return $this->validator;
}
......
<?php
namespace Drupal\Core\TypedData\Validation;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
/**
* Defines a constraint violation builder for the Typed Data validator.
*
* We do not use the builder provided by Symfony as it is marked internal.
*
* @codingStandardsIgnoreStart
*/
class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface {
/**
* The list of violations.
*
* @var \Symfony\Component\Validator\ConstraintViolationList
*/
protected $violations;
/**
* The violation message.
*
* @var string
*/
protected $message;
/**
* The message parameters.
*
* @var array
*/
protected $parameters;
/**
* The root path.
*
* @var mixed
*/
protected $root;
/**
* The invalid value caused the violation.
*
* @var mixed
*/
protected $invalidValue;
/**
* The property path.
*
* @var string
*/
protected $propertyPath;
/**
* The translator.
*
* @var \Symfony\Component\Translation\TranslatorInterface
*/
protected $translator;
/**
* The translation domain.
*
* @var string|null
*/
protected $translationDomain;
/**
* The number used
* @var int|null
*/
protected $plural;
/**
* @var Constraint
*/
protected $constraint;
/**
* @var mixed
*/
protected $code;
/**
* @var mixed
*/
protected $cause;
/**
* Constructs a new ConstraintViolationBuilder instance.
*
* @param \Symfony\Component\Validator\ConstraintViolationList $violations
* The violation list.
* @param \Symfony\Component\Validator\Constraint $constraint
* The constraint.
* @param string $message
* The message.
* @param array $parameters
* The message parameters.
* @param mixed $root
* The root.
* @param string $propertyPath
* The property string.
* @param mixed $invalidValue
* The invalid value.
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* The translator.
* @param null $translationDomain
* (optional) The translation domain.
*/
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
{
$this->violations = $violations;
$this->message = $message;
$this->parameters = $parameters;
$this->root = $root;
$this->propertyPath = $propertyPath;
$this->invalidValue = $invalidValue;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->constraint = $constraint;
}
/**
* {@inheritdoc}
*/
public function atPath($path)
{
$this->propertyPath = PropertyPath::append($this->propertyPath, $path);
return $this;
}
/**
* {@inheritdoc}
*/
public function setParameter($key, $value)
{
$this->parameters[$key] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function setParameters(array $parameters)
{
$this->parameters = $parameters;
return $this;
}
/**
* {@inheritdoc}
*/
public function setTranslationDomain($translationDomain)
{
$this->translationDomain = $translationDomain;
return $this;
}
/**
* {@inheritdoc}
*/
public function setInvalidValue($invalidValue)
{
$this->invalidValue = $invalidValue;
return $this;
}
/**
* {@inheritdoc}
*/
public function setPlural($number)
{
$this->plural = $number;
return $this;
}
/**
* {@inheritdoc}
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* {@inheritdoc}
*/
public function setCause($cause)
{
$this->cause = $cause;
return $this;
}
/**
* {@inheritdoc}
*/
public function addViolation()
{
if (null === $this->plural) {
$translatedMessage = $this->translator->trans(
$this->message,
$this->parameters,
$this->translationDomain
);
} else {
try {
$translatedMessage = $this->translator->transChoice(
$this->message,
$this->plural,
$this->parameters,
$this->translationDomain#
);
} catch (\InvalidArgumentException $e) {
$translatedMessage = $this->translator->trans(
$this->message,
$this->parameters,
$this->translationDomain
);
}
}
$this->violations->add(new ConstraintViolation(
$translatedMessage,
$this->message,
$this->parameters,
$this->root,
$this->propertyPath,
$this->invalidValue,
$this->plural,
$this->code,
$this->constraint,
$this->cause
));
}
}
<?php
/**
* @file
* Contains \Drupal\Core\TypedData\Validation\RecursiveContextualValidatorInterface.
*/
namespace Drupal\Core\TypedData\Validation;
use Symfony\Component\Validator\Validator\ContextualValidatorInterface as ContextualValidatorInterfaceBase;
/**
* Extends the contextual validator validate method by a new parameter.
*/
interface ContextualValidatorInterface extends ContextualValidatorInterfaceBase {
/**
* Validates a value against a constraint or a list of constraints.
*
* If no constraint is passed, the constraint
* \Symfony\Component\Validator\Constraints\Valid is assumed.
*
* @param mixed $value
* The value to validate
* @param \Symfony\Component\Validator\Constraint|\Symfony\Component\Validator\Constraint[] $constraints
* The constraint(s) to validate against.
* @param array|null $groups
* The validation groups to validate, defaults to "Default".
* @param bool $is_root_call
* (optional) Whether its the most upper call in the typed data tree.
*
* @see \Symfony\Component\Validator\Constraints\Valid
*
* @return $this
*/
public function validate($value, $constraints = NULL, $groups = NULL, $is_root_call = TRUE);
}
<?php
/**
* @file
* Contains \Drupal\Core\TypedData\Validation\ExecutionContext.
*/
namespace Drupal\Core\TypedData\Validation;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Defines an execution context class.
*
* We do not use the context provided by Symfony as it is marked internal, so
* this class is pretty much the same, but has some code style changes as well
* as exceptions for methods we don't support.
*/
class ExecutionContext implements ExecutionContextInterface {
/**
* @var \Symfony\Component\Validator\ValidatorInterface
*/
protected $validator;
/**
* The root value of the validated object graph.
*
* @var mixed
*/
protected $root;
/**
* @var \Symfony\Component\Translation\TranslatorInterface
*/
protected $translator;
/**
* @var string
*/
protected $translationDomain;
/**
* The violations generated in the current context.
*
* @var \Symfony\Component\Validator\ConstraintViolationList
*/
protected $violations;
/**
* The currently validated value.
*
* @var mixed
*/
protected $value;
/**
* The currently validated typed data object.
*
* @var \Drupal\Core\TypedData\TypedDataInterface
*/
protected $data;
/**
* The property path leading to the current value.
*
* @var string
*/
protected $propertyPath = '';
/**
* The current validation metadata.
*
* @var \Symfony\Component\Validator\Mapping\MetadataInterface|null
*/
protected $metadata;
/**
* The currently validated group.
*
* @var string|null
*/
protected $group;
/**
* The currently validated constraint.
*
* @var \Symfony\Component\Validator\Constraint|null
*/
protected $constraint;
/**
* Stores which objects have been validated in which group.
*
* @var array
*/
protected $validatedObjects = array();
/**
* Stores which class constraint has been validated for which object.
*
* @var array
*/
protected $validatedConstraints = array();
/**
* Creates a new ExecutionContext.
*
* @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
* The validator.
* @param mixed $root
* The root.
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* The translator.
* @param string $translationDomain
* (optional) The translation domain.
*
* @internal Called by \Drupal\Core\TypedData\Validation\ExecutionContextFactory.
* Should not be used in user code.
*/
public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = NULL) {
$this->validator = $validator;
$this->root = $root;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->violations = new ConstraintViolationList();
}
/**
* {@inheritdoc}
*/
public function setNode($value, $object, MetadataInterface $metadata = NULL, $propertyPath) {
$this->value = $value;
$this->data = $object;
$this->metadata = $metadata;
$this->propertyPath = (string) $propertyPath;
}
/**
* {@inheritdoc}
*/
public function setGroup($group) {
$this->group = $group;
}
/**
* {@inheritdoc}
*/
public function setConstraint(Constraint $constraint) {
$this->constraint = $constraint;
}
/**
* {@inheritdoc}
*/
public function addViolation($message, array $parameters = array(), $invalidValue = NULL, $plural = NULL, $code = NULL) {
// The parameters $invalidValue and following are ignored by the new
// API, as they are not present in the new interface anymore.
// You should use buildViolation() instead.
if (func_num_args() > 2) {
throw new \LogicException('Legacy validator API is unsupported.');
}
$this->violations->add(new ConstraintViolation($this->translator->trans($message, $parameters, $this->translationDomain), $message, $parameters, $this->root, $this->propertyPath, $this->value, NULL, NULL, $this->constraint));
}
/**
* {@inheritdoc}
*/
public function buildViolation($message, array $parameters = array()) {
return new ConstraintViolationBuilder($this->violations, $this->constraint, $message, $parameters, $this->root, $this->propertyPath, $this->value, $this->translator, $this->translationDomain);
}
/**
* {@inheritdoc}
*/
public function getViolations() {
return $this->violations;
}
/**
* {@inheritdoc}
*/
public function getValidator() {
return $this->validator;
}
/**
* {@inheritdoc}
*/
public function getRoot() {
return $this->root;
}
/**
* {@inheritdoc}
*/
public function getValue() {
return $this->value;
}
/**
* {@inheritdoc}
*/
public function getObject() {
return $this->data;
}
/**
* {@inheritdoc}
*/
public function getMetadata()