Development

/branches/1.2/lib/validator/sfValidatorErrorSchema.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/validator/sfValidatorErrorSchema.class.php

Revision 15393, 8.0 kB (checked in by fabien, 6 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  * sfValidatorErrorSchema represents a validation schema error.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorErrorSchema extends sfValidatorError implements ArrayAccess, Iterator, Countable
20 {
21   protected
22     $errors       = array(),
23     $globalErrors = array(),
24     $namedErrors  = array(),
25     $count        = 0;
26
27   /**
28    * Constructor.
29    *
30    * @param sfValidatorBase $validator  An sfValidatorBase instance
31    * @param array           $errors     An array of errors
32    */
33   public function __construct(sfValidatorBase $validator, $errors = array())
34   {
35     $this->validator = $validator;
36     $this->arguments = array();
37
38     // override default exception message and code
39     $this->code    = '';
40     $this->message = '';
41
42     $this->addErrors($errors);
43   }
44
45   /**
46    * Adds an error.
47    *
48    * This method merges sfValidatorErrorSchema errors with the current instance.
49    *
50    * @param sfValidatorError $error  An sfValidatorError instance
51    * @param string           $name   The error name
52    */
53   public function addError(sfValidatorError $error, $name = null)
54   {
55     if (is_null($name) || is_integer($name))
56     {
57       if ($error instanceof sfValidatorErrorSchema)
58       {
59         $this->addErrors($error);
60       }
61       else
62       {
63         $this->globalErrors[] = $error;
64         $this->errors[] = $error;
65       }
66     }
67     else
68     {
69       if (!isset($this->namedErrors[$name]) && !$error instanceof sfValidatorErrorSchema)
70       {
71         $this->namedErrors[$name] = $error;
72         $this->errors[$name] = $error;
73       }
74       else
75       {
76         if (!isset($this->namedErrors[$name]))
77         {
78           $this->namedErrors[$name] = new sfValidatorErrorSchema($error->getValidator());
79           $this->errors[$name] = new sfValidatorErrorSchema($error->getValidator());
80         }
81         else if (!$this->namedErrors[$name] instanceof sfValidatorErrorSchema)
82         {
83           $current = $this->namedErrors[$name];
84           $this->namedErrors[$name] = new sfValidatorErrorSchema($current->getValidator());
85           $this->errors[$name] = new sfValidatorErrorSchema($current->getValidator());
86
87           $method = $current instanceof sfValidatorErrorSchema ? 'addErrors' : 'addError';
88           $this->namedErrors[$name]->$method($current);
89           $this->errors[$name]->$method($current);
90         }
91
92         $method = $error instanceof sfValidatorErrorSchema ? 'addErrors' : 'addError';
93         $this->namedErrors[$name]->$method($error);
94         $this->errors[$name]->$method($error);
95       }
96     }
97
98     $this->updateCode();
99     $this->updateMessage();
100   }
101
102   /**
103    * Adds an array of errors.
104    *
105    * @param array $errors  An array of sfValidatorError instances
106    */
107   public function addErrors($errors)
108   {
109     if ($errors instanceof sfValidatorErrorSchema)
110     {
111       foreach ($errors->getGlobalErrors() as $error)
112       {
113         $this->addError($error);
114       }
115
116       foreach ($errors->getNamedErrors() as $name => $error)
117       {
118         $this->addError($error, (string) $name);
119       }
120     }
121     else
122     {
123       foreach ($errors as $name => $error)
124       {
125         $this->addError($error, $name);
126       }
127     }
128   }
129
130   /**
131    * Gets an array of all errors
132    *
133    * @return array An array of sfValidatorError instances
134    */
135   public function getErrors()
136   {
137     return $this->errors;
138   }
139
140   /**
141    * Gets an array of all named errors
142    *
143    * @return array An array of sfValidatorError instances
144    */
145   public function getNamedErrors()
146   {
147     return $this->namedErrors;
148   }
149
150   /**
151    * Gets an array of all global errors
152    *
153    * @return array An array of sfValidatorError instances
154    */
155   public function getGlobalErrors()
156   {
157     return $this->globalErrors;
158   }
159
160   /**
161    * @see sfValidatorError
162    */
163   public function getValue()
164   {
165     return null;
166   }
167
168   /**
169    * @see sfValidatorError
170    */
171   public function getArguments($raw = false)
172   {
173     return array();
174   }
175
176   /**
177    * @see sfValidatorError
178    */
179   public function getMessageFormat()
180   {
181     return '';
182   }
183
184   /**
185    * Returns the number of errors (implements the Countable interface).
186    *
187    * @return int The number of array
188    */
189   public function count()
190   {
191     return count($this->errors);
192   }
193
194   /**
195    * Reset the error array to the beginning (implements the Iterator interface).
196    */
197   public function rewind()
198   {
199     reset($this->errors);
200
201     $this->count = count($this->errors);
202   }
203
204   /**
205    * Get the key associated with the current error (implements the Iterator interface).
206    *
207    * @return string The key
208    */
209   public function key()
210   {
211     return key($this->errors);
212   }
213
214   /**
215    * Returns the current error (implements the Iterator interface).
216    *
217    * @return mixed The escaped value
218    */
219   public function current()
220   {
221     return current($this->errors);
222   }
223
224   /**
225    * Moves to the next error (implements the Iterator interface).
226    */
227   public function next()
228   {
229     next($this->errors);
230
231     --$this->count;
232   }
233
234   /**
235    * Returns true if the current error is valid (implements the Iterator interface).
236    *
237    * @return boolean The validity of the current element; true if it is valid
238    */
239   public function valid()
240   {
241     return $this->count > 0;
242   }
243
244   /**
245    * Returns true if the error exists (implements the ArrayAccess interface).
246    *
247    * @param  string $name  The name of the error
248    *
249    * @return bool true if the error exists, false otherwise
250    */
251   public function offsetExists($name)
252   {
253     return isset($this->errors[$name]);
254   }
255
256   /**
257    * Returns the error associated with the name (implements the ArrayAccess interface).
258    *
259    * @param  string $name  The offset of the value to get
260    *
261    * @return sfValidatorError A sfValidatorError instance
262    */
263   public function offsetGet($name)
264   {
265     return isset($this->errors[$name]) ? $this->errors[$name] : null;
266   }
267
268   /**
269    * Throws an exception saying that values cannot be set (implements the ArrayAccess interface).
270    *
271    * @param string $offset  (ignored)
272    * @param string $value   (ignored)
273    *
274    * @throws LogicException
275    */
276   public function offsetSet($offset, $value)
277   {
278     throw new LogicException('Unable update an error.');
279   }
280
281   /**
282    * Impossible to call because this is an exception!
283    *
284    * @param string $offset  (ignored)
285    */
286   public function offsetUnset($offset)
287   {
288   }
289
290   /**
291    * Updates the exception error code according to the current errors.
292    */
293   protected function updateCode()
294   {
295     $this->code = implode(' ', array_merge(
296       array_map(create_function('$e', 'return $e->getCode();'), $this->globalErrors),
297       array_map(create_function('$n,$e', 'return $n.\' [\'.$e->getCode().\']\';'), array_keys($this->namedErrors), array_values($this->namedErrors))
298     ));
299   }
300
301   /**
302    * Updates the exception error message according to the current errors.
303    */
304   protected function updateMessage()
305   {
306     $this->message = implode(' ', array_merge(
307       array_map(create_function('$e', 'return $e->getMessage();'), $this->globalErrors),
308       array_map(create_function('$n,$e', 'return $n.\' [\'.$e->getMessage().\']\';'), array_keys($this->namedErrors), array_values($this->namedErrors))
309     ));
310   }
311
312   /**
313    * Serializes the current instance.
314    *
315    * @return string The instance as a serialized string
316    */
317   public function serialize()
318   {
319     return serialize(array($this->validator, $this->arguments, $this->code, $this->message, $this->errors, $this->globalErrors, $this->namedErrors));
320   }
321
322   /**
323    * Unserializes a sfValidatorError instance.
324    *
325    * @param string $serialized  A serialized sfValidatorError instance
326    *
327    */
328   public function unserialize($serialized)
329   {
330     list($this->validator, $this->arguments, $this->code, $this->message, $this->errors, $this->globalErrors, $this->namedErrors) = unserialize($serialized);
331   }
332 }
333
Note: See TracBrowser for help on using the browser.