caisse-bliss/vendor/symfony/validator/Violation/ConstraintViolationBuilder.php

168 lines
4.1 KiB
PHP
Raw Normal View History

2023-06-19 12:13:30 +02: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\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Default implementation of {@link ConstraintViolationBuilderInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal since version 2.5. Code against ConstraintViolationBuilderInterface instead.
*/
class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
{
private $violations;
private string|\Stringable $message;
private array $parameters;
private mixed $root;
private mixed $invalidValue;
private string $propertyPath;
private $translator;
private ?string $translationDomain;
private ?int $plural = null;
private $constraint;
private ?string $code = null;
private mixed $cause = null;
public function __construct(ConstraintViolationList $violations, ?Constraint $constraint, string|\Stringable $message, array $parameters, mixed $root, ?string $propertyPath, mixed $invalidValue, TranslatorInterface $translator, string $translationDomain = null)
{
$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;
}
/**
* {@inheritdoc}
*/
public function atPath(string $path): static
{
$this->propertyPath = PropertyPath::append($this->propertyPath, $path);
return $this;
}
/**
* {@inheritdoc}
*/
public function setParameter(string $key, string $value): static
{
$this->parameters[$key] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function setParameters(array $parameters): static
{
$this->parameters = $parameters;
return $this;
}
/**
* {@inheritdoc}
*/
public function setTranslationDomain(string $translationDomain): static
{
$this->translationDomain = $translationDomain;
return $this;
}
/**
* {@inheritdoc}
*/
public function setInvalidValue(mixed $invalidValue): static
{
$this->invalidValue = $invalidValue;
return $this;
}
/**
* {@inheritdoc}
*/
public function setPlural(int $number): static
{
$this->plural = $number;
return $this;
}
/**
* {@inheritdoc}
*/
public function setCode(?string $code): static
{
$this->code = $code;
return $this;
}
/**
* {@inheritdoc}
*/
public function setCause(mixed $cause): static
{
$this->cause = $cause;
return $this;
}
/**
* {@inheritdoc}
*/
public function addViolation()
{
if (null === $this->plural) {
$translatedMessage = $this->translator->trans(
$this->message,
$this->parameters,
$this->translationDomain
);
} else {
$translatedMessage = $this->translator->trans(
$this->message,
['%count%' => $this->plural] + $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
));
}
}