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|object $message         The violation message as a string or a stringable object
  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 string|null   $code            The error code of the violation
  44.      * @param mixed         $cause           The cause of the violation
  45.      */
  46.     public function __construct($message, ?string $messageTemplate, array $parameters$root, ?string $propertyPath$invalidValueint $plural null$code nullConstraint $constraint null$cause null)
  47.     {
  48.         if (null === $message) {
  49.             @trigger_error(sprintf('Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.'__CLASS__), E_USER_DEPRECATED);
  50.             $message '';
  51.         }
  52.         if (null !== $code && !\is_string($code)) {
  53.             @trigger_error(sprintf('Not using a string as the error code in %s() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.'__METHOD__), E_USER_DEPRECATED);
  54.         }
  55.         if (!\is_string($message) && !(\is_object($message) && method_exists($message'__toString'))) {
  56.             throw new \TypeError('Constraint violation message should be a string or an object which implements the __toString() method.');
  57.         }
  58.         $this->message $message;
  59.         $this->messageTemplate $messageTemplate;
  60.         $this->parameters $parameters;
  61.         $this->plural $plural;
  62.         $this->root $root;
  63.         $this->propertyPath $propertyPath;
  64.         $this->invalidValue $invalidValue;
  65.         $this->constraint $constraint;
  66.         $this->code $code;
  67.         $this->cause $cause;
  68.     }
  69.     /**
  70.      * Converts the violation into a string for debugging purposes.
  71.      *
  72.      * @return string The violation as string
  73.      */
  74.     public function __toString()
  75.     {
  76.         if (\is_object($this->root)) {
  77.             $class 'Object('.\get_class($this->root).')';
  78.         } elseif (\is_array($this->root)) {
  79.             $class 'Array';
  80.         } else {
  81.             $class = (string) $this->root;
  82.         }
  83.         $propertyPath = (string) $this->propertyPath;
  84.         $code = (string) $this->code;
  85.         if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
  86.             $class .= '.';
  87.         }
  88.         if ('' !== $code) {
  89.             $code ' (code '.$code.')';
  90.         }
  91.         return $class.$propertyPath.":\n    ".$this->getMessage().$code;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     public function getMessageTemplate()
  97.     {
  98.         return $this->messageTemplate;
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      */
  103.     public function getParameters()
  104.     {
  105.         return $this->parameters;
  106.     }
  107.     /**
  108.      * {@inheritdoc}
  109.      */
  110.     public function getPlural()
  111.     {
  112.         return $this->plural;
  113.     }
  114.     /**
  115.      * {@inheritdoc}
  116.      */
  117.     public function getMessage()
  118.     {
  119.         return $this->message;
  120.     }
  121.     /**
  122.      * {@inheritdoc}
  123.      */
  124.     public function getRoot()
  125.     {
  126.         return $this->root;
  127.     }
  128.     /**
  129.      * {@inheritdoc}
  130.      */
  131.     public function getPropertyPath()
  132.     {
  133.         return $this->propertyPath;
  134.     }
  135.     /**
  136.      * {@inheritdoc}
  137.      */
  138.     public function getInvalidValue()
  139.     {
  140.         return $this->invalidValue;
  141.     }
  142.     /**
  143.      * Returns the constraint whose validation caused the violation.
  144.      *
  145.      * @return Constraint|null The constraint or null if it is not known
  146.      */
  147.     public function getConstraint()
  148.     {
  149.         return $this->constraint;
  150.     }
  151.     /**
  152.      * Returns the cause of the violation.
  153.      *
  154.      * @return mixed
  155.      */
  156.     public function getCause()
  157.     {
  158.         return $this->cause;
  159.     }
  160.     /**
  161.      * {@inheritdoc}
  162.      */
  163.     public function getCode()
  164.     {
  165.         return $this->code;
  166.     }
  167. }