PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/source/core/thirdparty/autoCrud/error.inc.php

https://bitbucket.org/detroitpro/taphp
PHP | 177 lines | 58 code | 24 blank | 95 comment | 13 complexity | 20bd7fe1420785434fbe0a16ea1dd295 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, GPL-3.0
  1. <?php
  2. /**
  3. * Error Class File
  4. *
  5. * This file contains all the error handling classes and functions
  6. *
  7. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  8. * @package autocrud
  9. */
  10. /**
  11. * Used to check if a variable hold an error object or not
  12. *
  13. * @access public
  14. * @param mixed $var
  15. * @return bool returns true when it's a error object, and false when it's not
  16. */
  17. function autocrud_is_error($var) {
  18. if (is_object($var) AND strtolower(get_class($var)) == 'autocrud_error') {
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. }
  24. /**
  25. * AutoCRUD Error Class
  26. *
  27. * This class is used to create a new error object, like this
  28. * <code>
  29. * $error = new AutoCRUD_Error;
  30. * </code>
  31. *
  32. * @package autocrud
  33. */
  34. Class AutoCRUD_Error {
  35. // Private vars:
  36. /**#@+
  37. * @access private
  38. */
  39. var $_codes = array();
  40. var $_message = array();
  41. /**#@-*/
  42. /**
  43. * The constructor will also immediately add the object to the error stack
  44. *
  45. * @access public
  46. */
  47. function AutoCRUD_Error() {
  48. $stack =& AutoCRUD_Error::stack();
  49. $stack[] =& $this;
  50. }
  51. /**
  52. * Used to set an error message
  53. *
  54. * @access public
  55. * @param string $msg the error message
  56. * @param integer $count the index number of the error message (if you want to use multiple error messages)
  57. */
  58. function setMessage($msg, $count=1) {
  59. if (!is_numeric($count)) { $count = 1; }
  60. $this->_messages[$count] = $msg;
  61. }
  62. /**
  63. * Used to set an error code
  64. *
  65. * @access public
  66. * @param string $code the error code
  67. * @param integer $count the index number of the error code (if you want to use multiple error codes)
  68. */
  69. function setCode($code, $count=1) {
  70. if (!is_numeric($count)) { $count = 1; }
  71. $this->_codes[$count] = $code;
  72. }
  73. /**
  74. * Used to retrieve an error message
  75. *
  76. * @access public
  77. * @param integer $count the index number of the error message
  78. * @return string the error message
  79. */
  80. function getMessage($which=1) {
  81. if (!is_numeric($which)) { $which = 1; }
  82. if (!isset($this->_messages[$which])) { return NULL; }
  83. return $this->_messages[$which];
  84. }
  85. /**
  86. * Used to retrieve an error code
  87. *
  88. * @access public
  89. * @param integer $count the index number of the error code
  90. * @return string the error code
  91. */
  92. function getCode($which=1) {
  93. if (!is_numeric($which)) { $which = 1; }
  94. if (!isset($this->_codes[$which])) { return NULL; }
  95. return $this->_codes[$which];
  96. }
  97. /**
  98. * Used to get the code of the last error that happened
  99. *
  100. * @static should only be called statically, i.e. AutoCRUD_Error::get_last_errno();
  101. * @param integer $count the index number of the error code
  102. * @return string the error code
  103. */
  104. function get_last_errno($which=1) {
  105. $last_error =& AutoCRUD_Error::get_last_error();
  106. if (!is_object($last_error)) { return false; }
  107. $errno = $last_error->getCode($which);
  108. return $errno;
  109. }
  110. /**
  111. * Alias of the get_last_errno() function
  112. *
  113. * @see AutoCRUD_Error::get_last_errno()
  114. */
  115. function get_last_errcode($which=1) { return AutoCRUD_Error::get_last_errno($which); }
  116. /**
  117. * Used to get the message of the last error that happened
  118. *
  119. * @static should only be called statically, i.e. AutoCRUD_Error::get_last_errmsg();
  120. * @param integer $count the index number of the error code
  121. * @return string the error message
  122. */
  123. function get_last_errmsg($which=1) {
  124. $last_error =& AutoCRUD_Error::get_last_error();
  125. if (!is_object($last_error)) { return false; }
  126. $errmsg = $last_error->getMessage($which);
  127. return $errmsg;
  128. }
  129. /**
  130. * Used to get the last error that happened
  131. *
  132. * @static should only be called statically, i.e. AutoCRUD_Error::get_last_error();
  133. * @return AutoCRUD_Error the last error
  134. */
  135. function &get_last_error() {
  136. $stack =& AutoCRUD_Error::stack();
  137. // No errors? return false
  138. if (count($stack) == 0) { $false = false; return $false; }
  139. $last_error =& $stack[count($stack)-1];
  140. return $last_error;
  141. }
  142. /**
  143. * Used to get an array of all the errors that have happened
  144. *
  145. * @static should only be called statically, i.e. AutoCRUD_Error::stack();
  146. * @return array the errors
  147. */
  148. function &stack() {
  149. static $stack = array();
  150. return $stack;
  151. }
  152. }
  153. ?>