This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/Context/ExecutionContext.php

269 lines
6.7 KiB
PHP
Raw Normal View History

<?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;
use Symfony\Component\Validator\ClassBasedInterface;
2014-02-18 12:03:34 +00:00
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Group\GroupManagerInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
use Symfony\Component\Validator\Node\Node;
2014-02-18 12:03:34 +00:00
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Validator\ValidatorInterface;
2014-02-18 12:03:34 +00:00
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
/**
* The context used and created by {@link ExecutionContextManager}.
*
* @since 2.5
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see ExecutionContextInterface
*/
class ExecutionContext implements ExecutionContextInterface
{
/**
* The root value of the validated object graph.
*
* @var mixed
*/
private $root;
/**
* The violations generated in the current context.
*
* @var ConstraintViolationList
*/
private $violations;
/**
* The current node under validation.
*
* @var Node
*/
private $node;
/**
* The trace of nodes from the root node to the current node.
*
* @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;
/**
* 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-18 12:03:34 +00:00
$this->root = $root;
$this->validator = $validator;
$this->groupManager = $groupManager;
2014-02-18 12:03:34 +00:00
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->violations = new ConstraintViolationList();
2014-02-18 12:03:34 +00:00
$this->nodeStack = new \SplStack();
}
/**
* 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
*/
public function pushNode(Node $node)
{
$this->nodeStack->push($node);
$this->node = $node;
}
/**
* 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
*/
public function popNode()
{
// Nothing to do if the stack is empty
if (0 === count($this->nodeStack)) {
return null;
}
$poppedNode = $this->node;
// After removing the last node, the stack is empty and the node
// is null
if (1 === count($this->nodeStack)) {
$this->nodeStack->pop();
$this->node = null;
return $poppedNode;
}
$this->nodeStack->pop();
$this->node = $this->nodeStack->top();
return $poppedNode;
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function addViolation($message, array $parameters = array())
{
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
));
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function buildViolation($message, array $parameters = array())
{
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
);
}
/**
* {@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->node ? $this->node->value : null;
}
/**
* {@inheritdoc}
*/
public function getMetadata()
{
return $this->node ? $this->node->metadata : null;
}
/**
* {@inheritdoc}
*/
public function getGroup()
{
return $this->groupManager->getCurrentGroup();
}
/**
* {@inheritdoc}
*/
public function getClassName()
{
$metadata = $this->getMetadata();
return $metadata instanceof ClassBasedInterface ? $metadata->getClassName() : null;
}
/**
* {@inheritdoc}
*/
public function getPropertyName()
{
$metadata = $this->getMetadata();
return $metadata instanceof PropertyMetadataInterface ? $metadata->getPropertyName() : null;
}
/**
* {@inheritdoc}
*/
public function getPropertyPath($subPath = '')
{
$propertyPath = $this->node ? $this->node->propertyPath : '';
2014-02-18 12:03:34 +00:00
return PropertyPath::append($propertyPath, $subPath);
}
}