sfValidatorErrorSchema.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 15393 2009-02-10 12:58:49Z 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. /**
  281. * Serializes the current instance.
  282. *
  283. * @return string The instance as a serialized string
  284. */
  285. public function serialize()
  286. {
  287. return serialize(array($this->validator, $this->arguments, $this->code, $this->message, $this->errors, $this->globalErrors, $this->namedErrors));
  288. }
  289. /**
  290. * Unserializes a sfValidatorError instance.
  291. *
  292. * @param string $serialized A serialized sfValidatorError instance
  293. *
  294. */
  295. public function unserialize($serialized)
  296. {
  297. list($this->validator, $this->arguments, $this->code, $this->message, $this->errors, $this->globalErrors, $this->namedErrors) = unserialize($serialized);
  298. }
  299. }