123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfValidatorError represents a validation error.
- *
- * @package symfony
- * @subpackage validator
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfValidatorError.class.php 15393 2009-02-10 12:58:49Z fabien $
- */
- class sfValidatorError extends Exception implements Serializable
- {
- protected
- $validator = null,
- $arguments = array();
- /**
- * Constructor.
- *
- * @param sfValidatorBase $validator An sfValidatorBase instance
- * @param string $code The error code
- * @param array $arguments An array of named arguments needed to render the error message
- */
- public function __construct(sfValidatorBase $validator, $code, $arguments = array())
- {
- $this->validator = $validator;
- $this->arguments = $arguments;
- // override default exception message and code
- $this->code = $code;
- if (!$messageFormat = $this->getMessageFormat())
- {
- $messageFormat = $code;
- }
- $this->message = strtr($messageFormat, $this->getArguments());
- }
- /**
- * Returns the string representation of the error.
- *
- * @return string The error message
- */
- public function __toString()
- {
- return $this->getMessage();
- }
- /**
- * Returns the input value that triggered this error.
- *
- * @return mixed The input value
- */
- public function getValue()
- {
- return isset($this->arguments['value']) ? $this->arguments['value'] : null;
- }
- /**
- * Returns the validator that triggered this error.
- *
- * @return sfValidatorBase A sfValidatorBase instance
- */
- public function getValidator()
- {
- return $this->validator;
- }
- /**
- * Returns the arguments needed to format the message.
- *
- * @param bool $raw false to use it as arguments for the message format, true otherwise (default to false)
- *
- * @see getMessageFormat()
- */
- public function getArguments($raw = false)
- {
- if ($raw)
- {
- return $this->arguments;
- }
- $arguments = array();
- foreach ($this->arguments as $key => $value)
- {
- if (is_array($value))
- {
- continue;
- }
- $arguments["%$key%"] = htmlspecialchars($value, ENT_QUOTES, sfValidatorBase::getCharset());
- }
- return $arguments;
- }
- /**
- * Returns the message format for this error.
- *
- * This is the string you need to use if you need to internationalize
- * error messages:
- *
- * $i18n->__($error->getMessageFormat(), $error->getArguments());
- *
- * If no message format has been set in the validator, the exception standard
- * message is returned.
- *
- * @return string The message format
- */
- public function getMessageFormat()
- {
- $messageFormat = $this->validator->getMessage($this->code);
- if (!$messageFormat)
- {
- $messageFormat = $this->getMessage();
- }
- return $messageFormat;
- }
- /**
- * Serializes the current instance.
- *
- * We must implement the Serializable interface to overcome a problem with PDO
- * used as a session handler.
- *
- * The default serialization process serializes the exception trace, and because
- * the trace can contain a PDO instance which is not serializable, serializing won't
- * work when using PDO.
- *
- * @return string The instance as a serialized string
- */
- public function serialize()
- {
- return serialize(array($this->validator, $this->arguments, $this->code, $this->message));
- }
- /**
- * Unserializes a sfValidatorError instance.
- *
- * @param string $serialized A serialized sfValidatorError instance
- *
- */
- public function unserialize($serialized)
- {
- list($this->validator, $this->arguments, $this->code, $this->message) = unserialize($serialized);
- }
- }
|