vendor/symfony/validator/ConstraintViolation.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Validator;
  11. /**
  12.  * Default implementation of {@ConstraintViolationInterface}.
  13.  *
  14.  * @author Bernhard Schussek <bschussek@gmail.com>
  15.  */
  16. class ConstraintViolation implements ConstraintViolationInterface
  17. {
  18.     private $message;
  19.     private $messageTemplate;
  20.     private $parameters;
  21.     private $plural;
  22.     private $root;
  23.     private $propertyPath;
  24.     private $invalidValue;
  25.     private $constraint;
  26.     private $code;
  27.     private $cause;
  28.     /**
  29.      * Creates a new constraint violation.
  30.      *
  31.      * @param string          $message         The violation message
  32.      * @param string          $messageTemplate The raw violation message
  33.      * @param array           $parameters      The parameters to substitute in the
  34.      *                                         raw violation message
  35.      * @param mixed           $root            The value originally passed to the
  36.      *                                         validator
  37.      * @param string          $propertyPath    The property path from the root
  38.      *                                         value to the invalid value
  39.      * @param mixed           $invalidValue    The invalid value that caused this
  40.      *                                         violation
  41.      * @param int|null        $plural          The number for determining the plural
  42.      *                                         form when translating the message
  43.      * @param mixed           $code            The error code of the violation
  44.      * @param Constraint|null $constraint      The constraint whose validation
  45.      *                                         caused the violation
  46.      * @param mixed           $cause           The cause of the violation
  47.      */
  48.     public function __construct(?string $message, ?string $messageTemplate, array $parameters$root, ?string $propertyPath$invalidValueint $plural null$code nullConstraint $constraint null$cause null)
  49.     {
  50.         $this->message $message;
  51.         $this->messageTemplate $messageTemplate;
  52.         $this->parameters $parameters;
  53.         $this->plural $plural;
  54.         $this->root $root;
  55.         $this->propertyPath $propertyPath;
  56.         $this->invalidValue $invalidValue;
  57.         $this->constraint $constraint;
  58.         $this->code $code;
  59.         $this->cause $cause;
  60.     }
  61.     /**
  62.      * Converts the violation into a string for debugging purposes.
  63.      *
  64.      * @return string The violation as string
  65.      */
  66.     public function __toString()
  67.     {
  68.         if (\is_object($this->root)) {
  69.             $class 'Object('.\get_class($this->root).')';
  70.         } elseif (\is_array($this->root)) {
  71.             $class 'Array';
  72.         } else {
  73.             $class = (string) $this->root;
  74.         }
  75.         $propertyPath = (string) $this->propertyPath;
  76.         $code = (string) $this->code;
  77.         if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
  78.             $class .= '.';
  79.         }
  80.         if ('' !== $code) {
  81.             $code ' (code '.$code.')';
  82.         }
  83.         return $class.$propertyPath.":\n    ".$this->getMessage().$code;
  84.     }
  85.     /**
  86.      * {@inheritdoc}
  87.      */
  88.     public function getMessageTemplate()
  89.     {
  90.         return $this->messageTemplate;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function getParameters()
  96.     {
  97.         return $this->parameters;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function getPlural()
  103.     {
  104.         return $this->plural;
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function getMessage()
  110.     {
  111.         return $this->message;
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function getRoot()
  117.     {
  118.         return $this->root;
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function getPropertyPath()
  124.     {
  125.         return $this->propertyPath;
  126.     }
  127.     /**
  128.      * {@inheritdoc}
  129.      */
  130.     public function getInvalidValue()
  131.     {
  132.         return $this->invalidValue;
  133.     }
  134.     /**
  135.      * Returns the constraint whose validation caused the violation.
  136.      *
  137.      * @return Constraint|null The constraint or null if it is not known
  138.      */
  139.     public function getConstraint()
  140.     {
  141.         return $this->constraint;
  142.     }
  143.     /**
  144.      * Returns the cause of the violation.
  145.      *
  146.      * @return mixed
  147.      */
  148.     public function getCause()
  149.     {
  150.         return $this->cause;
  151.     }
  152.     /**
  153.      * {@inheritdoc}
  154.      */
  155.     public function getCode()
  156.     {
  157.         return $this->code;
  158.     }
  159. }