Development

/branches/1.1/lib/validator/sfValidatorError.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/validator/sfValidatorError.class.php

Revision 15393, 3.8 kB (checked in by fabien, 5 years ago)

[1.1, 1.2, 1.3] fixed form serialization when PDO is used as a session handler (closes #5267)

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