1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\Validator\Context; 13 14use Symfony\Component\Translation\TranslatorInterface; 15use Symfony\Component\Validator\Constraints\Valid; 16use Symfony\Component\Validator\MetadataFactoryInterface; 17use Symfony\Component\Validator\Validator\ValidatorInterface; 18 19/** 20 * An execution context that is compatible with the legacy API (< 2.5). 21 * 22 * @since 2.5 23 * @author Bernhard Schussek <bschussek@gmail.com> 24 * 25 * @deprecated Implemented for backwards compatibility with Symfony < 2.5. 26 * To be removed in Symfony 3.0. 27 */ 28class LegacyExecutionContext extends ExecutionContext 29{ 30 /** 31 * @var MetadataFactoryInterface 32 */ 33 private $metadataFactory; 34 35 /** 36 * Creates a new context. 37 * 38 * @see ExecutionContext::__construct() 39 * 40 * @internal Called by {@link LegacyExecutionContextFactory}. Should not be used 41 * in user code. 42 */ 43 public function __construct(ValidatorInterface $validator, $root, MetadataFactoryInterface $metadataFactory, TranslatorInterface $translator, $translationDomain = null) 44 { 45 parent::__construct( 46 $validator, 47 $root, 48 $translator, 49 $translationDomain 50 ); 51 52 $this->metadataFactory = $metadataFactory; 53 } 54 55 /** 56 * {@inheritdoc} 57 */ 58 public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null) 59 { 60 if (func_num_args() > 2) { 61 $this 62 ->buildViolation($message, $parameters) 63 ->setInvalidValue($invalidValue) 64 ->setPlural($plural) 65 ->setCode($code) 66 ->addViolation() 67 ; 68 69 return; 70 } 71 72 parent::addViolation($message, $parameters); 73 } 74 75 /** 76 * {@inheritdoc} 77 */ 78 public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null) 79 { 80 if (func_num_args() > 2) { 81 $this 82 ->buildViolation($message, $parameters) 83 ->atPath($subPath) 84 ->setInvalidValue($invalidValue) 85 ->setPlural($plural) 86 ->setCode($code) 87 ->addViolation() 88 ; 89 90 return; 91 } 92 93 $this 94 ->buildViolation($message, $parameters) 95 ->atPath($subPath) 96 ->addViolation() 97 ; 98 } 99 100 /** 101 * {@inheritdoc} 102 */ 103 public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false) 104 { 105 if (is_array($value)) { 106 // The $traverse flag is ignored for arrays 107 $constraint = new Valid(array('traverse' => true, 'deep' => $deep)); 108 109 return $this 110 ->getValidator() 111 ->inContext($this) 112 ->atPath($subPath) 113 ->validate($value, $constraint, $groups) 114 ; 115 } 116 117 if ($traverse && $value instanceof \Traversable) { 118 $constraint = new Valid(array('traverse' => true, 'deep' => $deep)); 119 120 return $this 121 ->getValidator() 122 ->inContext($this) 123 ->atPath($subPath) 124 ->validate($value, $constraint, $groups) 125 ; 126 } 127 128 return $this 129 ->getValidator() 130 ->inContext($this) 131 ->atPath($subPath) 132 ->validate($value, null, $groups) 133 ; 134 } 135 136 /** 137 * {@inheritdoc} 138 */ 139 public function validateValue($value, $constraints, $subPath = '', $groups = null) 140 { 141 return $this 142 ->getValidator() 143 ->inContext($this) 144 ->atPath($subPath) 145 ->validate($value, $constraints, $groups) 146 ; 147 } 148 149 /** 150 * {@inheritdoc} 151 */ 152 public function getMetadataFactory() 153 { 154 return $this->metadataFactory; 155 } 156} 157