PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/sbweb/sbweb_logica/lib/symfony/validator/sfValidatorErrorSchema.class.php

http://opac-sbweb.googlecode.com/
PHP | 311 lines | 162 code | 31 blank | 118 comment | 9 complexity | 306b38aeea4b854ee72fb401f82b6318 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorErrorSchema represents a validation schema error.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorErrorSchema.class.php 9100 2008-05-20 08:34:25Z fabien $
  16. */
  17. class sfValidatorErrorSchema extends sfValidatorError implements ArrayAccess, Iterator, Countable
  18. {
  19. protected
  20. $errors = array(),
  21. $globalErrors = array(),
  22. $namedErrors = array(),
  23. $count = 0;
  24. /**
  25. * Constructor.
  26. *
  27. * @param sfValidatorBase $validator An sfValidatorBase instance
  28. * @param array $errors An array of errors
  29. */
  30. public function __construct(sfValidatorBase $validator, $errors = array())
  31. {
  32. $this->validator = $validator;
  33. $this->arguments = array();
  34. // override default exception message and code
  35. $this->code = '';
  36. $this->message = '';
  37. $this->addErrors($errors);
  38. }
  39. /**
  40. * Adds an error.
  41. *
  42. * This method merges sfValidatorErrorSchema errors with the current instance.
  43. *
  44. * @param sfValidatorError $error An sfValidatorError instance
  45. * @param string $name The error name
  46. */
  47. public function addError(sfValidatorError $error, $name = null)
  48. {
  49. if (is_null($name) || is_integer($name))
  50. {
  51. if ($error instanceof sfValidatorErrorSchema)
  52. {
  53. $this->addErrors($error);
  54. }
  55. else
  56. {
  57. $this->globalErrors[] = $error;
  58. $this->errors[] = $error;
  59. }
  60. }
  61. else
  62. {
  63. if (!isset($this->namedErrors[$name]) && !$error instanceof sfValidatorErrorSchema)
  64. {
  65. $this->namedErrors[$name] = $error;
  66. $this->errors[$name] = $error;
  67. }
  68. else
  69. {
  70. if (!isset($this->namedErrors[$name]))
  71. {
  72. $this->namedErrors[$name] = new sfValidatorErrorSchema($error->getValidator());
  73. $this->errors[$name] = new sfValidatorErrorSchema($error->getValidator());
  74. }
  75. else if (!$this->namedErrors[$name] instanceof sfValidatorErrorSchema)
  76. {
  77. $current = $this->namedErrors[$name];
  78. $this->namedErrors[$name] = new sfValidatorErrorSchema($current->getValidator());
  79. $this->errors[$name] = new sfValidatorErrorSchema($current->getValidator());
  80. $method = $current instanceof sfValidatorErrorSchema ? 'addErrors' : 'addError';
  81. $this->namedErrors[$name]->$method($current);
  82. $this->errors[$name]->$method($current);
  83. }
  84. $method = $error instanceof sfValidatorErrorSchema ? 'addErrors' : 'addError';
  85. $this->namedErrors[$name]->$method($error);
  86. $this->errors[$name]->$method($error);
  87. }
  88. }
  89. $this->updateCode();
  90. $this->updateMessage();
  91. }
  92. /**
  93. * Adds an array of errors.
  94. *
  95. * @param array $errors An array of sfValidatorError instances
  96. */
  97. public function addErrors($errors)
  98. {
  99. if ($errors instanceof sfValidatorErrorSchema)
  100. {
  101. foreach ($errors->getGlobalErrors() as $error)
  102. {
  103. $this->addError($error);
  104. }
  105. foreach ($errors->getNamedErrors() as $name => $error)
  106. {
  107. $this->addError($error, (string) $name);
  108. }
  109. }
  110. else
  111. {
  112. foreach ($errors as $name => $error)
  113. {
  114. $this->addError($error, $name);
  115. }
  116. }
  117. }
  118. /**
  119. * Gets an array of all errors
  120. *
  121. * @return array An array of sfValidatorError instances
  122. */
  123. public function getErrors()
  124. {
  125. return $this->errors;
  126. }
  127. /**
  128. * Gets an array of all named errors
  129. *
  130. * @return array An array of sfValidatorError instances
  131. */
  132. public function getNamedErrors()
  133. {
  134. return $this->namedErrors;
  135. }
  136. /**
  137. * Gets an array of all global errors
  138. *
  139. * @return array An array of sfValidatorError instances
  140. */
  141. public function getGlobalErrors()
  142. {
  143. return $this->globalErrors;
  144. }
  145. /**
  146. * @see sfValidatorError
  147. */
  148. public function getValue()
  149. {
  150. return null;
  151. }
  152. /**
  153. * @see sfValidatorError
  154. */
  155. public function getArguments($raw = false)
  156. {
  157. return array();
  158. }
  159. /**
  160. * @see sfValidatorError
  161. */
  162. public function getMessageFormat()
  163. {
  164. return '';
  165. }
  166. /**
  167. * Returns the number of errors (implements the Countable interface).
  168. *
  169. * @return int The number of array
  170. */
  171. public function count()
  172. {
  173. return count($this->errors);
  174. }
  175. /**
  176. * Reset the error array to the beginning (implements the Iterator interface).
  177. */
  178. public function rewind()
  179. {
  180. reset($this->errors);
  181. $this->count = count($this->errors);
  182. }
  183. /**
  184. * Get the key associated with the current error (implements the Iterator interface).
  185. *
  186. * @return string The key
  187. */
  188. public function key()
  189. {
  190. return key($this->errors);
  191. }
  192. /**
  193. * Returns the current error (implements the Iterator interface).
  194. *
  195. * @return mixed The escaped value
  196. */
  197. public function current()
  198. {
  199. return current($this->errors);
  200. }
  201. /**
  202. * Moves to the next error (implements the Iterator interface).
  203. */
  204. public function next()
  205. {
  206. next($this->errors);
  207. --$this->count;
  208. }
  209. /**
  210. * Returns true if the current error is valid (implements the Iterator interface).
  211. *
  212. * @return boolean The validity of the current element; true if it is valid
  213. */
  214. public function valid()
  215. {
  216. return $this->count > 0;
  217. }
  218. /**
  219. * Returns true if the error exists (implements the ArrayAccess interface).
  220. *
  221. * @param string $name The name of the error
  222. *
  223. * @return bool true if the error exists, false otherwise
  224. */
  225. public function offsetExists($name)
  226. {
  227. return isset($this->errors[$name]);
  228. }
  229. /**
  230. * Returns the error associated with the name (implements the ArrayAccess interface).
  231. *
  232. * @param string $name The offset of the value to get
  233. *
  234. * @return sfValidatorError A sfValidatorError instance
  235. */
  236. public function offsetGet($name)
  237. {
  238. return isset($this->errors[$name]) ? $this->errors[$name] : null;
  239. }
  240. /**
  241. * Throws an exception saying that values cannot be set (implements the ArrayAccess interface).
  242. *
  243. * @param string $offset (ignored)
  244. * @param string $value (ignored)
  245. *
  246. * @throws LogicException
  247. */
  248. public function offsetSet($offset, $value)
  249. {
  250. throw new LogicException('Unable update an error.');
  251. }
  252. /**
  253. * Impossible to call because this is an exception!
  254. *
  255. * @param string $offset (ignored)
  256. */
  257. public function offsetUnset($offset)
  258. {
  259. }
  260. /**
  261. * Updates the exception error code according to the current errors.
  262. */
  263. protected function updateCode()
  264. {
  265. $this->code = implode(' ', array_merge(
  266. array_map(create_function('$e', 'return $e->getCode();'), $this->globalErrors),
  267. array_map(create_function('$n,$e', 'return $n.\' [\'.$e->getCode().\']\';'), array_keys($this->namedErrors), array_values($this->namedErrors))
  268. ));
  269. }
  270. /**
  271. * Updates the exception error message according to the current errors.
  272. */
  273. protected function updateMessage()
  274. {
  275. $this->message = implode(' ', array_merge(
  276. array_map(create_function('$e', 'return $e->getMessage();'), $this->globalErrors),
  277. array_map(create_function('$n,$e', 'return $n.\' [\'.$e->getMessage().\']\';'), array_keys($this->namedErrors), array_values($this->namedErrors))
  278. ));
  279. }
  280. }