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/Violation/ConstraintViolationBuilder.php

225 lines
4.7 KiB
PHP
Raw Normal View History

2014-02-18 12:03:34 +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\Violation;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
2014-02-18 12:03:34 +00:00
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Util\PropertyPath;
/**
* Default implementation of {@link ConstraintViolationBuilderInterface}.
*
2014-02-18 12:03:34 +00:00
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal You should not instantiate or use this class. Code against
* {@link ConstraintViolationBuilderInterface} instead.
2014-02-18 12:03:34 +00:00
*/
class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
{
/**
* @var ConstraintViolationList
*/
2014-02-18 12:03:34 +00:00
private $violations;
/**
* @var string
*/
2014-02-18 12:03:34 +00:00
private $message;
/**
* @var array
*/
2014-02-18 12:03:34 +00:00
private $parameters;
/**
* @var mixed
*/
2014-02-18 12:03:34 +00:00
private $root;
/**
* @var mixed
*/
2014-02-18 12:03:34 +00:00
private $invalidValue;
/**
* @var string
*/
2014-02-18 12:03:34 +00:00
private $propertyPath;
/**
* @var TranslatorInterface
*/
2014-02-18 12:03:34 +00:00
private $translator;
/**
* @var string|null
*/
2014-02-18 12:03:34 +00:00
private $translationDomain;
/**
2014-04-16 11:36:34 +01:00
* @var int|null
*/
private $plural;
2014-02-18 12:03:34 +00:00
/**
* @var Constraint
*/
private $constraint;
/**
* @var mixed
*/
2014-02-18 12:03:34 +00:00
private $code;
/**
* @var mixed
*/
private $cause;
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
2014-02-18 12:03:34 +00:00
{
$this->violations = $violations;
$this->message = $message;
$this->parameters = $parameters;
$this->root = $root;
$this->propertyPath = $propertyPath;
$this->invalidValue = $invalidValue;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->constraint = $constraint;
2014-02-18 12:03:34 +00:00
}
/**
* {@inheritdoc}
*/
public function atPath($path)
2014-02-18 12:03:34 +00:00
{
$this->propertyPath = PropertyPath::append($this->propertyPath, $path);
2014-02-18 12:03:34 +00:00
return $this;
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function setParameter($key, $value)
{
$this->parameters[$key] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function setParameters(array $parameters)
{
$this->parameters = $parameters;
return $this;
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function setTranslationDomain($translationDomain)
{
$this->translationDomain = $translationDomain;
return $this;
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function setInvalidValue($invalidValue)
{
$this->invalidValue = $invalidValue;
return $this;
}
/**
* {@inheritdoc}
*/
public function setPlural($number)
2014-02-18 12:03:34 +00:00
{
$this->plural = $number;
2014-02-18 12:03:34 +00:00
return $this;
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* {@inheritdoc}
*/
public function setCause($cause)
{
$this->cause = $cause;
return $this;
}
/**
* {@inheritdoc}
*/
2014-02-18 12:03:34 +00:00
public function addViolation()
{
if (null === $this->plural) {
2014-02-18 12:03:34 +00:00
$translatedMessage = $this->translator->trans(
$this->message,
$this->parameters,
$this->translationDomain
);
} else {
try {
$translatedMessage = $this->translator->transChoice(
$this->message,
$this->plural,
2014-02-18 12:03:34 +00:00
$this->parameters,
$this->translationDomain
2014-02-18 12:03:34 +00:00
);
} catch (\InvalidArgumentException $e) {
$translatedMessage = $this->translator->trans(
$this->message,
$this->parameters,
$this->translationDomain
);
}
}
$this->violations->add(new ConstraintViolation(
$translatedMessage,
$this->message,
$this->parameters,
$this->root,
$this->propertyPath,
$this->invalidValue,
$this->plural,
$this->code,
$this->constraint,
$this->cause
2014-02-18 12:03:34 +00:00
));
}
}