class ConstraintViolationBuilder

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder
  2. 10 core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder
  3. 10 core/lib/Drupal/Core/Validation/ConstraintViolationBuilder.php \Drupal\Core\Validation\ConstraintViolationBuilder
  4. 11.x core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder
  5. 11.x core/lib/Drupal/Core/Validation/ConstraintViolationBuilder.php \Drupal\Core\Validation\ConstraintViolationBuilder

Defines a constraint violation builder for the Typed Data validator.

We do not use the builder provided by Symfony as it is marked internal.

Hierarchy

  • class \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder implements \Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface

Expanded class hierarchy of ConstraintViolationBuilder

File

core/lib/Drupal/Core/TypedData/Validation/ConstraintViolationBuilder.php, line 21

Namespace

Drupal\Core\TypedData\Validation
View source
class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface {
    
    /**
     * The list of violations.
     *
     * @var \Symfony\Component\Validator\ConstraintViolationList
     */
    protected $violations;
    
    /**
     * The violation message.
     *
     * @var string
     */
    protected $message;
    
    /**
     * The message parameters.
     *
     * @var array
     */
    protected $parameters;
    
    /**
     * The root path.
     *
     * @var mixed
     */
    protected $root;
    
    /**
     * The invalid value caused the violation.
     *
     * @var mixed
     */
    protected $invalidValue;
    
    /**
     * The property path.
     *
     * @var string
     */
    protected $propertyPath;
    
    /**
     * The translator.
     *
     * @var \Drupal\Core\Validation\TranslatorInterface
     */
    protected $translator;
    
    /**
     * The translation domain.
     *
     * @var string|null
     */
    protected $translationDomain;
    
    /**
     * The number used
     * @var int|null
     */
    protected $plural;
    
    /**
     * @var Constraint
     */
    protected $constraint;
    
    /**
     * @var mixed
     */
    protected $code;
    
    /**
     * @var mixed
     */
    protected $cause;
    
    /**
     * Constructs a new ConstraintViolationBuilder instance.
     *
     * @param \Symfony\Component\Validator\ConstraintViolationList $violations
     *   The violation list.
     * @param \Symfony\Component\Validator\Constraint $constraint
     *   The constraint.
     * @param string $message
     *   The message.
     * @param array $parameters
     *   The message parameters.
     * @param mixed $root
     *   The root.
     * @param string $propertyPath
     *   The property string.
     * @param mixed $invalidValue
     *   The invalid value.
     * @param \Drupal\Core\Validation\TranslatorInterface $translator
     *   The translator.
     * @param null $translationDomain
     *   (optional) The translation domain.
     */
    public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $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($path) {
        $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setParameter($key, $value) {
        $this->parameters[$key] = $value;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setParameters(array $parameters) {
        $this->parameters = $parameters;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setTranslationDomain($translationDomain) {
        $this->translationDomain = $translationDomain;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setInvalidValue($invalidValue) {
        $this->invalidValue = $invalidValue;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setPlural($number) {
        $this->plural = $number;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setCode($code) {
        $this->code = $code;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setCause($cause) {
        $this->cause = $cause;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function addViolation() {
        if (null === $this->plural) {
            $translatedMessage = $this->translator
                ->trans($this->message, $this->parameters, $this->translationDomain);
        }
        else {
            try {
                $translatedMessage = $this->translator
                    ->transChoice($this->message, $this->plural, $this->parameters, $this->translationDomain);
            } 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));
    }

}

Members

Title Sort descending Modifiers Object type Summary
ConstraintViolationBuilder::$cause protected property
ConstraintViolationBuilder::$code protected property
ConstraintViolationBuilder::$constraint protected property
ConstraintViolationBuilder::$invalidValue protected property The invalid value caused the violation.
ConstraintViolationBuilder::$message protected property The violation message.
ConstraintViolationBuilder::$parameters protected property The message parameters.
ConstraintViolationBuilder::$plural protected property The number used
ConstraintViolationBuilder::$propertyPath protected property The property path.
ConstraintViolationBuilder::$root protected property The root path.
ConstraintViolationBuilder::$translationDomain protected property The translation domain.
ConstraintViolationBuilder::$translator protected property The translator.
ConstraintViolationBuilder::$violations protected property The list of violations.
ConstraintViolationBuilder::addViolation public function
ConstraintViolationBuilder::atPath public function
ConstraintViolationBuilder::setCause public function
ConstraintViolationBuilder::setCode public function
ConstraintViolationBuilder::setInvalidValue public function
ConstraintViolationBuilder::setParameter public function
ConstraintViolationBuilder::setParameters public function
ConstraintViolationBuilder::setPlural public function
ConstraintViolationBuilder::setTranslationDomain public function
ConstraintViolationBuilder::__construct public function Constructs a new ConstraintViolationBuilder instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.