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