class ValidationVisitor implements ValidationVisitorInterface, GlobalExecutionContextInterface {
private $root;
private $metadataFactory;
private $validatorFactory;
private $translator;
private $translationDomain;
private $objectInitializers;
private $violations;
private $validatedObjects = array();
public function __construct($root, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, TranslatorInterface $translator, $translationDomain = null, array $objectInitializers = array()) {
foreach ($objectInitializers as $initializer) {
if (!$initializer instanceof ObjectInitializerInterface) {
throw new UnexpectedTypeException($initializer, 'Symfony\\Component\\Validator\\ObjectInitializerInterface');
}
}
$this->root = $root;
$this->metadataFactory = $metadataFactory;
$this->validatorFactory = $validatorFactory;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->objectInitializers = $objectInitializers;
$this->violations = new ConstraintViolationList();
}
public function visit(MetadataInterface $metadata, $value, $group, $propertyPath) {
$context = new ExecutionContext($this, $this->translator, $this->translationDomain, $metadata, $value, $group, $propertyPath);
$context
->validateValue($value, $metadata
->findConstraints($group));
}
public function validate($value, $group, $propertyPath, $traverse = false, $deep = false) {
if (null === $value) {
return;
}
if (is_object($value)) {
$hash = spl_object_hash($value);
if (isset($this->validatedObjects[$hash][$group])) {
return;
}
$this->validatedObjects[$hash][$group] = true;
foreach ($this->objectInitializers as $initializer) {
if (!$initializer instanceof ObjectInitializerInterface) {
throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
}
$initializer
->initialize($value);
}
}
if (is_array($value) || $traverse && $value instanceof \Traversable) {
foreach ($value as $key => $element) {
if (is_object($element) || is_array($element)) {
$this
->validate($element, $group, $propertyPath . '[' . $key . ']', $deep, $deep);
}
}
try {
$this->metadataFactory
->getMetadataFor($value)
->accept($this, $value, $group, $propertyPath);
} catch (NoSuchMetadataException $e) {
}
}
else {
$this->metadataFactory
->getMetadataFor($value)
->accept($this, $value, $group, $propertyPath);
}
}
public function getViolations() {
return $this->violations;
}
public function getRoot() {
return $this->root;
}
public function getVisitor() {
return $this;
}
public function getValidatorFactory() {
return $this->validatorFactory;
}
public function getMetadataFactory() {
return $this->metadataFactory;
}
}