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-18 12:03:34 +00:00
|
|
|
use Symfony\Component\Validator\ConstraintViolation;
|
2014-02-17 13:12:00 +00:00
|
|
|
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;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @since %%NextVersion%%
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
|
|
|
class ExecutionContext implements ExecutionContextInterface
|
|
|
|
{
|
|
|
|
private $root;
|
|
|
|
|
|
|
|
private $violations;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Node
|
|
|
|
*/
|
|
|
|
private $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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function pushNode(Node $node)
|
|
|
|
{
|
2014-02-18 12:03:34 +00:00
|
|
|
if (null !== $this->node) {
|
2014-02-17 13:12:00 +00:00
|
|
|
$this->nodeStack->push($this->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->node = $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function popNode()
|
|
|
|
{
|
|
|
|
$poppedNode = $this->node;
|
|
|
|
|
|
|
|
if (0 === count($this->nodeStack)) {
|
|
|
|
$this->node = null;
|
|
|
|
|
|
|
|
return $poppedNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1 === count($this->nodeStack)) {
|
|
|
|
$this->nodeStack->pop();
|
|
|
|
$this->node = null;
|
|
|
|
|
|
|
|
return $poppedNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->nodeStack->pop();
|
|
|
|
$this->node = $this->nodeStack->top();
|
|
|
|
|
|
|
|
return $poppedNode;
|
|
|
|
}
|
|
|
|
|
2014-02-18 12:03:34 +00:00
|
|
|
public function addViolation($message, array $parameters = array())
|
2014-02-17 13:12:00 +00:00
|
|
|
{
|
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 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
|
|
|
}
|
|
|
|
|
|
|
|
public function getViolations()
|
|
|
|
{
|
|
|
|
return $this->violations;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoot()
|
|
|
|
{
|
|
|
|
return $this->root;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValue()
|
|
|
|
{
|
|
|
|
return $this->node ? $this->node->value : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMetadata()
|
|
|
|
{
|
|
|
|
return $this->node ? $this->node->metadata : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGroup()
|
|
|
|
{
|
|
|
|
return $this->groupManager->getCurrentGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
$metadata = $this->getMetadata();
|
|
|
|
|
|
|
|
return $metadata instanceof ClassBasedInterface ? $metadata->getClassName() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPropertyName()
|
|
|
|
{
|
|
|
|
$metadata = $this->getMetadata();
|
|
|
|
|
|
|
|
return $metadata instanceof PropertyMetadataInterface ? $metadata->getPropertyName() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ValidatorInterface
|
|
|
|
*/
|
|
|
|
public function getValidator()
|
|
|
|
{
|
|
|
|
return $this->validator;
|
|
|
|
}
|
|
|
|
}
|