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

Debug toolbar