Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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