* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Validator\Context; use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Validator\ClassBasedInterface; 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; use Symfony\Component\Validator\Util\PropertyPath; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; /** * The context used and created by {@link ExecutionContextManager}. * * @since 2.5 * @author Bernhard Schussek * * @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; /** * @var TranslatorInterface */ private $translator; /** * @var string */ private $translationDomain; public function __construct($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain = null) { $this->root = $root; $this->validator = $validator; $this->groupManager = $groupManager; $this->translator = $translator; $this->translationDomain = $translationDomain; $this->violations = new ConstraintViolationList(); $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} */ public function addViolation($message, array $parameters = array()) { $this->violations->add(new ConstraintViolation( $this->translator->trans($message, $parameters, $this->translationDomain), $message, $parameters, $this->root, $this->getPropertyPath(), $this->getValue(), null, null )); } /** * {@inheritdoc} */ public function buildViolation($message, array $parameters = array()) { 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 : ''; return PropertyPath::append($propertyPath, $subPath); } }