2014-02-17 13:12:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Validator\Context;
|
|
|
|
|
2014-02-18 12:03:34 +00:00
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
2014-02-17 13:12:00 +00:00
|
|
|
use Symfony\Component\Validator\ClassBasedInterface;
|
2014-02-19 17:06:10 +00:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
2014-02-18 12:03:34 +00:00
|
|
|
use Symfony\Component\Validator\ConstraintViolation;
|
2014-02-17 13:12:00 +00:00
|
|
|
use Symfony\Component\Validator\ConstraintViolationList;
|
2014-02-19 17:06:10 +00:00
|
|
|
use Symfony\Component\Validator\Exception\BadMethodCallException;
|
|
|
|
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
|
2014-02-17 13:12:00 +00:00
|
|
|
use Symfony\Component\Validator\Group\GroupManagerInterface;
|
|
|
|
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
|
2014-02-19 17:06:10 +00:00
|
|
|
use Symfony\Component\Validator\MetadataFactoryInterface;
|
2014-02-17 13:12:00 +00:00
|
|
|
use Symfony\Component\Validator\Node\Node;
|
2014-02-18 12:03:34 +00:00
|
|
|
use Symfony\Component\Validator\Util\PropertyPath;
|
2014-02-17 13:12:00 +00:00
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
2014-02-18 12:03:34 +00:00
|
|
|
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
|
2014-02-17 13:12:00 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-18 15:18:15 +00:00
|
|
|
* The context used and created by {@link ExecutionContextManager}.
|
|
|
|
*
|
|
|
|
* @since 2.5
|
2014-02-17 13:12:00 +00:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2014-02-18 15:18:15 +00:00
|
|
|
*
|
|
|
|
* @see ExecutionContextInterface
|
2014-02-17 13:12:00 +00:00
|
|
|
*/
|
2014-02-19 17:06:10 +00:00
|
|
|
class ExecutionContext implements ExecutionContextInterface, LegacyExecutionContextInterface
|
2014-02-17 13:12:00 +00:00
|
|
|
{
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* The root value of the validated object graph.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
private $root;
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* The violations generated in the current context.
|
|
|
|
*
|
|
|
|
* @var ConstraintViolationList
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
private $violations;
|
|
|
|
|
|
|
|
/**
|
2014-02-18 15:18:15 +00:00
|
|
|
* The current node under validation.
|
|
|
|
*
|
2014-02-17 13:12:00 +00:00
|
|
|
* @var Node
|
|
|
|
*/
|
|
|
|
private $node;
|
|
|
|
|
|
|
|
/**
|
2014-02-18 15:18:15 +00:00
|
|
|
* The trace of nodes from the root node to the current node.
|
|
|
|
*
|
2014-02-17 13:12:00 +00:00
|
|
|
* @var \SplStack
|
|
|
|
*/
|
|
|
|
private $nodeStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ValidatorInterface
|
|
|
|
*/
|
|
|
|
private $validator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GroupManagerInterface
|
|
|
|
*/
|
|
|
|
private $groupManager;
|
|
|
|
|
2014-02-18 12:03:34 +00:00
|
|
|
/**
|
|
|
|
* @var TranslatorInterface
|
|
|
|
*/
|
|
|
|
private $translator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $translationDomain;
|
|
|
|
|
2014-02-18 17:03:59 +00:00
|
|
|
/**
|
|
|
|
* Creates a new execution context.
|
|
|
|
*
|
|
|
|
* @param mixed $root The root value of the
|
|
|
|
* validated object graph
|
|
|
|
* @param ValidatorInterface $validator The validator
|
|
|
|
* @param GroupManagerInterface $groupManager The manager for accessing
|
|
|
|
* the currently validated
|
|
|
|
* group
|
|
|
|
* @param TranslatorInterface $translator The translator
|
|
|
|
* @param string|null $translationDomain The translation domain to
|
|
|
|
* use for translating
|
|
|
|
* violation messages
|
|
|
|
*
|
|
|
|
* @internal Called by {@link ExecutionContextManager}. Should not be used
|
|
|
|
* in user code.
|
|
|
|
*/
|
2014-02-18 12:03:34 +00:00
|
|
|
public function __construct($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null)
|
2014-02-17 13:12:00 +00:00
|
|
|
{
|
2014-02-18 12:03:34 +00:00
|
|
|
$this->root = $root;
|
2014-02-17 13:12:00 +00:00
|
|
|
$this->validator = $validator;
|
|
|
|
$this->groupManager = $groupManager;
|
2014-02-18 12:03:34 +00:00
|
|
|
$this->translator = $translator;
|
|
|
|
$this->translationDomain = $translationDomain;
|
2014-02-17 13:12:00 +00:00
|
|
|
$this->violations = new ConstraintViolationList();
|
2014-02-18 12:03:34 +00:00
|
|
|
$this->nodeStack = new \SplStack();
|
2014-02-17 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the values of the context to match the given node.
|
|
|
|
*
|
|
|
|
* Internally, all nodes are stored on a stack and can be removed from that
|
|
|
|
* stack using {@link popNode()}.
|
|
|
|
*
|
|
|
|
* @param Node $node The currently validated node
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function pushNode(Node $node)
|
|
|
|
{
|
2014-02-18 15:18:15 +00:00
|
|
|
$this->nodeStack->push($node);
|
2014-02-17 13:12:00 +00:00
|
|
|
$this->node = $node;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the values of the context to match the previous node.
|
|
|
|
*
|
|
|
|
* The current node is removed from the internal stack and returned.
|
|
|
|
*
|
|
|
|
* @return Node|null The currently validated node or null, if no node was
|
|
|
|
* on the stack
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function popNode()
|
|
|
|
{
|
2014-02-18 15:18:15 +00:00
|
|
|
// Nothing to do if the stack is empty
|
2014-02-17 13:12:00 +00:00
|
|
|
if (0 === count($this->nodeStack)) {
|
2014-02-18 15:18:15 +00:00
|
|
|
return null;
|
2014-02-17 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 12:04:59 +00:00
|
|
|
// Remove the current node from the stack
|
|
|
|
$poppedNode = $this->nodeStack->pop();
|
2014-02-18 15:18:15 +00:00
|
|
|
|
2014-02-19 12:04:59 +00:00
|
|
|
// Adjust the current node to the previous node
|
|
|
|
$this->node = count($this->nodeStack) > 0
|
|
|
|
? $this->nodeStack->top()
|
|
|
|
: null;
|
2014-02-17 13:12:00 +00:00
|
|
|
|
|
|
|
return $poppedNode;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-19 17:06:10 +00:00
|
|
|
public function addViolation($message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
|
2014-02-17 13:12:00 +00:00
|
|
|
{
|
2014-02-19 17:06:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-02-18 12:03:34 +00:00
|
|
|
$this->violations->add(new ConstraintViolation(
|
|
|
|
$this->translator->trans($message, $parameters, $this->translationDomain),
|
|
|
|
$message,
|
|
|
|
$parameters,
|
|
|
|
$this->root,
|
|
|
|
$this->getPropertyPath(),
|
|
|
|
$this->getValue(),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
));
|
2014-02-17 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-18 12:03:34 +00:00
|
|
|
public function buildViolation($message, array $parameters = array())
|
2014-02-17 13:12:00 +00:00
|
|
|
{
|
2014-02-18 12:03:34 +00:00
|
|
|
return new ConstraintViolationBuilder(
|
|
|
|
$this->violations,
|
|
|
|
$message,
|
|
|
|
$parameters,
|
|
|
|
$this->root,
|
|
|
|
$this->getPropertyPath(),
|
|
|
|
$this->getValue(),
|
|
|
|
$this->translator,
|
|
|
|
$this->translationDomain
|
|
|
|
);
|
2014-02-17 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getViolations()
|
|
|
|
{
|
|
|
|
return $this->violations;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getValidator()
|
|
|
|
{
|
|
|
|
return $this->validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getRoot()
|
|
|
|
{
|
|
|
|
return $this->root;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getValue()
|
|
|
|
{
|
|
|
|
return $this->node ? $this->node->value : null;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getMetadata()
|
|
|
|
{
|
|
|
|
return $this->node ? $this->node->metadata : null;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getGroup()
|
|
|
|
{
|
|
|
|
return $this->groupManager->getCurrentGroup();
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
$metadata = $this->getMetadata();
|
|
|
|
|
|
|
|
return $metadata instanceof ClassBasedInterface ? $metadata->getClassName() : null;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getPropertyName()
|
|
|
|
{
|
|
|
|
$metadata = $this->getMetadata();
|
|
|
|
|
|
|
|
return $metadata instanceof PropertyMetadataInterface ? $metadata->getPropertyName() : null;
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:18:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-02-17 13:12:00 +00:00
|
|
|
public function getPropertyPath($subPath = '')
|
|
|
|
{
|
|
|
|
$propertyPath = $this->node ? $this->node->propertyPath : '';
|
|
|
|
|
2014-02-18 12:03:34 +00:00
|
|
|
return PropertyPath::append($propertyPath, $subPath);
|
2014-02-17 13:12:00 +00:00
|
|
|
}
|
2014-02-19 17:06:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
|
|
|
|
{
|
|
|
|
throw new BadMethodCallException(
|
|
|
|
'addViolationAt() is not supported anymore in the new API. '.
|
|
|
|
'Please use buildViolation() or enable the legacy mode.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
|
|
|
|
{
|
|
|
|
throw new BadMethodCallException(
|
|
|
|
'validate() is not supported anymore in the new API. '.
|
|
|
|
'Please use getValidator() or enable the legacy mode.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function validateValue($value, $constraints, $subPath = '', $groups = null)
|
|
|
|
{
|
|
|
|
throw new BadMethodCallException(
|
|
|
|
'validateValue() is not supported anymore in the new API. '.
|
|
|
|
'Please use getValidator() or enable the legacy mode.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getMetadataFactory()
|
|
|
|
{
|
|
|
|
throw new BadMethodCallException(
|
|
|
|
'getMetadataFactory() is not supported anymore in the new API. '.
|
|
|
|
'Please use getMetadataFor() or hasMetadataFor() or enable the '.
|
|
|
|
'legacy mode.'
|
|
|
|
);
|
|
|
|
}
|
2014-02-17 13:12:00 +00:00
|
|
|
}
|