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

Debug toolbar