vendor/symfony/form/FormError.php line 21

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\Form;
  11. use Symfony\Component\Form\Exception\BadMethodCallException;
  12. /**
  13.  * Wraps errors in forms.
  14.  *
  15.  * @author Bernhard Schussek <bschussek@gmail.com>
  16.  */
  17. class FormError
  18. {
  19.     protected $messageTemplate;
  20.     protected $messageParameters;
  21.     protected $messagePluralization;
  22.     private $message;
  23.     private $cause;
  24.     /**
  25.      * The form that spawned this error.
  26.      *
  27.      * @var FormInterface
  28.      */
  29.     private $origin;
  30.     /**
  31.      * Any array key in $messageParameters will be used as a placeholder in
  32.      * $messageTemplate.
  33.      *
  34.      * @param string      $message              The translated error message
  35.      * @param string|null $messageTemplate      The template for the error message
  36.      * @param array       $messageParameters    The parameters that should be
  37.      *                                          substituted in the message template
  38.      * @param int|null    $messagePluralization The value for error message pluralization
  39.      * @param mixed       $cause                The cause of the error
  40.      *
  41.      * @see \Symfony\Component\Translation\Translator
  42.      */
  43.     public function __construct(?string $messagestring $messageTemplate null, array $messageParameters = [], int $messagePluralization null$cause null)
  44.     {
  45.         if (null === $message) {
  46.             @trigger_error(sprintf('Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.'__CLASS__), \E_USER_DEPRECATED);
  47.             $message '';
  48.         }
  49.         $this->message $message;
  50.         $this->messageTemplate $messageTemplate ?: $message;
  51.         $this->messageParameters $messageParameters;
  52.         $this->messagePluralization $messagePluralization;
  53.         $this->cause $cause;
  54.     }
  55.     /**
  56.      * Returns the error message.
  57.      *
  58.      * @return string
  59.      */
  60.     public function getMessage()
  61.     {
  62.         return $this->message;
  63.     }
  64.     /**
  65.      * Returns the error message template.
  66.      *
  67.      * @return string
  68.      */
  69.     public function getMessageTemplate()
  70.     {
  71.         return $this->messageTemplate;
  72.     }
  73.     /**
  74.      * Returns the parameters to be inserted in the message template.
  75.      *
  76.      * @return array
  77.      */
  78.     public function getMessageParameters()
  79.     {
  80.         return $this->messageParameters;
  81.     }
  82.     /**
  83.      * Returns the value for error message pluralization.
  84.      *
  85.      * @return int|null
  86.      */
  87.     public function getMessagePluralization()
  88.     {
  89.         return $this->messagePluralization;
  90.     }
  91.     /**
  92.      * Returns the cause of this error.
  93.      *
  94.      * @return mixed The cause of this error
  95.      */
  96.     public function getCause()
  97.     {
  98.         return $this->cause;
  99.     }
  100.     /**
  101.      * Sets the form that caused this error.
  102.      *
  103.      * This method must only be called once.
  104.      *
  105.      * @throws BadMethodCallException If the method is called more than once
  106.      */
  107.     public function setOrigin(FormInterface $origin)
  108.     {
  109.         if (null !== $this->origin) {
  110.             throw new BadMethodCallException('setOrigin() must only be called once.');
  111.         }
  112.         $this->origin $origin;
  113.     }
  114.     /**
  115.      * Returns the form that caused this error.
  116.      *
  117.      * @return FormInterface|null The form that caused this error
  118.      */
  119.     public function getOrigin()
  120.     {
  121.         return $this->origin;
  122.     }
  123. }