FormError.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 implements \Serializable
  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($message, $messageTemplate = null, array $messageParameters = array(), $messagePluralization = null, $cause = null)
  44. {
  45. $this->message = $message;
  46. $this->messageTemplate = $messageTemplate ?: $message;
  47. $this->messageParameters = $messageParameters;
  48. $this->messagePluralization = $messagePluralization;
  49. $this->cause = $cause;
  50. }
  51. /**
  52. * Returns the error message.
  53. *
  54. * @return string
  55. */
  56. public function getMessage()
  57. {
  58. return $this->message;
  59. }
  60. /**
  61. * Returns the error message template.
  62. *
  63. * @return string
  64. */
  65. public function getMessageTemplate()
  66. {
  67. return $this->messageTemplate;
  68. }
  69. /**
  70. * Returns the parameters to be inserted in the message template.
  71. *
  72. * @return array
  73. */
  74. public function getMessageParameters()
  75. {
  76. return $this->messageParameters;
  77. }
  78. /**
  79. * Returns the value for error message pluralization.
  80. *
  81. * @return int|null
  82. */
  83. public function getMessagePluralization()
  84. {
  85. return $this->messagePluralization;
  86. }
  87. /**
  88. * Returns the cause of this error.
  89. *
  90. * @return mixed The cause of this error
  91. */
  92. public function getCause()
  93. {
  94. return $this->cause;
  95. }
  96. /**
  97. * Sets the form that caused this error.
  98. *
  99. * This method must only be called once.
  100. *
  101. * @param FormInterface $origin The form that caused this error
  102. *
  103. * @throws BadMethodCallException If the method is called more than once
  104. */
  105. public function setOrigin(FormInterface $origin)
  106. {
  107. if (null !== $this->origin) {
  108. throw new BadMethodCallException('setOrigin() must only be called once.');
  109. }
  110. $this->origin = $origin;
  111. }
  112. /**
  113. * Returns the form that caused this error.
  114. *
  115. * @return FormInterface The form that caused this error
  116. */
  117. public function getOrigin()
  118. {
  119. return $this->origin;
  120. }
  121. /**
  122. * Serializes this error.
  123. *
  124. * @return string The serialized error
  125. */
  126. public function serialize()
  127. {
  128. return serialize(array(
  129. $this->message,
  130. $this->messageTemplate,
  131. $this->messageParameters,
  132. $this->messagePluralization,
  133. $this->cause,
  134. ));
  135. }
  136. /**
  137. * Unserializes a serialized error.
  138. *
  139. * @param string $serialized The serialized error
  140. */
  141. public function unserialize($serialized)
  142. {
  143. list($this->message, $this->messageTemplate, $this->messageParameters, $this->messagePluralization, $this->cause) = unserialize($serialized);
  144. }
  145. }