sfValidatorError.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorError represents a validation error.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorError.class.php 15393 2009-02-10 12:58:49Z fabien $
  16. */
  17. class sfValidatorError extends Exception implements Serializable
  18. {
  19. protected
  20. $validator = null,
  21. $arguments = array();
  22. /**
  23. * Constructor.
  24. *
  25. * @param sfValidatorBase $validator An sfValidatorBase instance
  26. * @param string $code The error code
  27. * @param array $arguments An array of named arguments needed to render the error message
  28. */
  29. public function __construct(sfValidatorBase $validator, $code, $arguments = array())
  30. {
  31. $this->validator = $validator;
  32. $this->arguments = $arguments;
  33. // override default exception message and code
  34. $this->code = $code;
  35. if (!$messageFormat = $this->getMessageFormat())
  36. {
  37. $messageFormat = $code;
  38. }
  39. $this->message = strtr($messageFormat, $this->getArguments());
  40. }
  41. /**
  42. * Returns the string representation of the error.
  43. *
  44. * @return string The error message
  45. */
  46. public function __toString()
  47. {
  48. return $this->getMessage();
  49. }
  50. /**
  51. * Returns the input value that triggered this error.
  52. *
  53. * @return mixed The input value
  54. */
  55. public function getValue()
  56. {
  57. return isset($this->arguments['value']) ? $this->arguments['value'] : null;
  58. }
  59. /**
  60. * Returns the validator that triggered this error.
  61. *
  62. * @return sfValidatorBase A sfValidatorBase instance
  63. */
  64. public function getValidator()
  65. {
  66. return $this->validator;
  67. }
  68. /**
  69. * Returns the arguments needed to format the message.
  70. *
  71. * @param bool $raw false to use it as arguments for the message format, true otherwise (default to false)
  72. *
  73. * @see getMessageFormat()
  74. */
  75. public function getArguments($raw = false)
  76. {
  77. if ($raw)
  78. {
  79. return $this->arguments;
  80. }
  81. $arguments = array();
  82. foreach ($this->arguments as $key => $value)
  83. {
  84. if (is_array($value))
  85. {
  86. continue;
  87. }
  88. $arguments["%$key%"] = htmlspecialchars($value, ENT_QUOTES, sfValidatorBase::getCharset());
  89. }
  90. return $arguments;
  91. }
  92. /**
  93. * Returns the message format for this error.
  94. *
  95. * This is the string you need to use if you need to internationalize
  96. * error messages:
  97. *
  98. * $i18n->__($error->getMessageFormat(), $error->getArguments());
  99. *
  100. * If no message format has been set in the validator, the exception standard
  101. * message is returned.
  102. *
  103. * @return string The message format
  104. */
  105. public function getMessageFormat()
  106. {
  107. $messageFormat = $this->validator->getMessage($this->code);
  108. if (!$messageFormat)
  109. {
  110. $messageFormat = $this->getMessage();
  111. }
  112. return $messageFormat;
  113. }
  114. /**
  115. * Serializes the current instance.
  116. *
  117. * We must implement the Serializable interface to overcome a problem with PDO
  118. * used as a session handler.
  119. *
  120. * The default serialization process serializes the exception trace, and because
  121. * the trace can contain a PDO instance which is not serializable, serializing won't
  122. * work when using PDO.
  123. *
  124. * @return string The instance as a serialized string
  125. */
  126. public function serialize()
  127. {
  128. return serialize(array($this->validator, $this->arguments, $this->code, $this->message));
  129. }
  130. /**
  131. * Unserializes a sfValidatorError instance.
  132. *
  133. * @param string $serialized A serialized sfValidatorError instance
  134. *
  135. */
  136. public function unserialize($serialized)
  137. {
  138. list($this->validator, $this->arguments, $this->code, $this->message) = unserialize($serialized);
  139. }
  140. }