Exception.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * ORM Validation exceptions.
  4. *
  5. * @package Kohana/ORM
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2012 Kohana Team
  8. * @license http://kohanaframework.org/license
  9. */
  10. class Kohana_ORM_Validation_Exception extends Kohana_Exception {
  11. /**
  12. * Array of validation objects
  13. * @var array
  14. */
  15. protected $_objects = array();
  16. /**
  17. * The alias of the main ORM model this exception was created for
  18. * @var string
  19. */
  20. protected $_alias = NULL;
  21. /**
  22. * Constructs a new exception for the specified model
  23. *
  24. * @param string $alias The alias to use when looking for error messages
  25. * @param Validation $object The Validation object of the model
  26. * @param string $message The error message
  27. * @param array $values The array of values for the error message
  28. * @param integer $code The error code for the exception
  29. * @return void
  30. */
  31. public function __construct($alias, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0, Exception $previous = NULL)
  32. {
  33. $this->_alias = $alias;
  34. $this->_objects['_object'] = $object;
  35. $this->_objects['_has_many'] = FALSE;
  36. parent::__construct($message, $values, $code, $previous);
  37. }
  38. /**
  39. * Adds a Validation object to this exception
  40. *
  41. * // The following will add a validation object for a profile model
  42. * // inside the exception for a user model.
  43. * $e->add_object('profile', $validation);
  44. * // The errors array will now look something like this
  45. * // array
  46. * // (
  47. * // 'username' => 'This field is required',
  48. * // 'profile' => array
  49. * // (
  50. * // 'first_name' => 'This field is required',
  51. * // ),
  52. * // );
  53. *
  54. * @param string $alias The relationship alias from the model
  55. * @param Validation $object The Validation object to merge
  56. * @param mixed $has_many The array key to use if this exception can be merged multiple times
  57. * @return ORM_Validation_Exception
  58. */
  59. public function add_object($alias, Validation $object, $has_many = FALSE)
  60. {
  61. // We will need this when generating errors
  62. $this->_objects[$alias]['_has_many'] = ($has_many !== FALSE);
  63. if ($has_many === TRUE)
  64. {
  65. // This is most likely a has_many relationship
  66. $this->_objects[$alias][]['_object'] = $object;
  67. }
  68. elseif ($has_many)
  69. {
  70. // This is most likely a has_many relationship
  71. $this->_objects[$alias][$has_many]['_object'] = $object;
  72. }
  73. else
  74. {
  75. $this->_objects[$alias]['_object'] = $object;
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Merges an ORM_Validation_Exception object into the current exception
  81. * Useful when you want to combine errors into one array
  82. *
  83. * @param ORM_Validation_Exception $object The exception to merge
  84. * @param mixed $has_many The array key to use if this exception can be merged multiple times
  85. * @return ORM_Validation_Exception
  86. */
  87. public function merge(ORM_Validation_Exception $object, $has_many = FALSE)
  88. {
  89. $alias = $object->alias();
  90. // We will need this when generating errors
  91. $this->_objects[$alias]['_has_many'] = ($has_many !== FALSE);
  92. if ($has_many === TRUE)
  93. {
  94. // This is most likely a has_many relationship
  95. $this->_objects[$alias][] = $object->objects();
  96. }
  97. elseif ($has_many)
  98. {
  99. // This is most likely a has_many relationship
  100. $this->_objects[$alias][$has_many] = $object->objects();
  101. }
  102. else
  103. {
  104. $this->_objects[$alias] = $object->objects();
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Returns a merged array of the errors from all the Validation objects in this exception
  110. *
  111. * // Will load Model_User errors from messages/orm-validation/user.php
  112. * $e->errors('orm-validation');
  113. *
  114. * @param string $directory Directory to load error messages from
  115. * @param mixed $translate Translate the message
  116. * @return array
  117. * @see generate_errors()
  118. */
  119. public function errors($directory = NULL, $translate = TRUE)
  120. {
  121. return $this->generate_errors($this->_alias, $this->_objects, $directory, $translate);
  122. }
  123. /**
  124. * Recursive method to fetch all the errors in this exception
  125. *
  126. * @param string $alias Alias to use for messages file
  127. * @param array $array Array of Validation objects to get errors from
  128. * @param string $directory Directory to load error messages from
  129. * @param mixed $translate Translate the message
  130. * @return array
  131. */
  132. protected function generate_errors($alias, array $array, $directory, $translate)
  133. {
  134. $errors = array();
  135. foreach ($array as $key => $object)
  136. {
  137. if (is_array($object))
  138. {
  139. $errors[$key] = ($key === '_external')
  140. // Search for errors in $alias/_external.php
  141. ? $this->generate_errors($alias.'/'.$key, $object, $directory, $translate)
  142. // Regular models get their own file not nested within $alias
  143. : $this->generate_errors($key, $object, $directory, $translate);
  144. }
  145. elseif ($object instanceof Validation)
  146. {
  147. if ($directory === NULL)
  148. {
  149. // Return the raw errors
  150. $file = NULL;
  151. }
  152. else
  153. {
  154. $file = trim($directory.'/'.$alias, '/');
  155. }
  156. // Merge in this array of errors
  157. $errors += $object->errors($file, $translate);
  158. }
  159. }
  160. return $errors;
  161. }
  162. /**
  163. * Returns the protected _objects property from this exception
  164. *
  165. * @return array
  166. */
  167. public function objects()
  168. {
  169. return $this->_objects;
  170. }
  171. /**
  172. * Returns the protected _alias property from this exception
  173. *
  174. * @return string
  175. */
  176. public function alias()
  177. {
  178. return $this->_alias;
  179. }
  180. } // End Kohana_ORM_Validation_Exception