PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/error.php

https://github.com/GlobeOfGeek/bedmath
PHP | 102 lines | 59 code | 10 blank | 33 comment | 8 complexity | c16b22470484ec162e55602bccaa041b MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * Class handles errors generated upon user interaction with the framework. Errors may be stored, logged, and then rendered for frontview display.
  4. */
  5. class Error {
  6. /**
  7. * Array list of error types that are allowed. Such as user errors, form errors, etc. . . .
  8. */
  9. private $errorTypes;
  10. /**
  11. * Multidimentional-Array containing errors to display.
  12. * Structure: mainArray[error[]];
  13. * The main array will hold all of the individual errors, which are also arrays.
  14. * The error array will hold elements:
  15. * (0 = 'error type(example: user error, form error', 1 = 'message', 2 = 'display type' < this is for css loading style.);
  16. */
  17. private $errorList;
  18. /**
  19. * This will be a multidimentional array where the index is the error type [form][user] gathered from variable $errorTypes.
  20. * The value it holds will be an array of errors under the set type.
  21. * These errors are generated from different parts of the site, and then displayed accordingly.
  22. * The index names are most commonly the objects in the framework, however some exceptions may occur.
  23. */
  24. private $errors;
  25. public function __construct() {
  26. $this->errorList = array();
  27. $this->errorTypes = array('form', 'user', 'points', 'question', 'answer', 'mail');
  28. $this->errors = array(
  29. 'form' => array(),
  30. 'user' => array(),
  31. 'points' => array(),
  32. 'question' => array(),
  33. 'answer' => array(),
  34. 'mail' => array()
  35. );
  36. }
  37. /**
  38. * Add a new error to process during page request.
  39. * @param $type the type of error being generated.
  40. * @param $text string value containing error message.
  41. */
  42. public function addError($errorType, $text, $displayType = 'default') {
  43. $newError = array($errorType, $text, $displayType);
  44. array_push($this->errorList, $newError);
  45. $cType = $this->getErrorTypes();
  46. if (in_array($type, $cType)) {
  47. array_push($this->errors[$errorType], $text);
  48. }
  49. }
  50. /**
  51. * Returns generated error count, allows specification of error type.
  52. */
  53. public function getErrorCount($type = 'all') {
  54. $count = 0;
  55. if ($type == 'all') {
  56. foreach ($this->errorTypes as $type) {
  57. for($c=0; $c<=sizeof($this->errorList); $c++) {
  58. if($this->errorList[$c][0]==$type) {
  59. $count++;
  60. }
  61. }
  62. }
  63. return $count;
  64. } else {
  65. foreach($this->errorList as $error) {
  66. if($error[0]==$type) {
  67. $count++;
  68. }
  69. }
  70. return $count;
  71. }
  72. }
  73. /**
  74. * Renders errors for display on frontend.
  75. */
  76. public function showErrors() {
  77. $output = '';
  78. $types = $this->getErrorTypes();
  79. $index = 0; //counting messages within each type.
  80. for($c=0; $c<sizeof($this->errorList); $c++) {
  81. $output.='<div class=error>'.$this->errorList[$c][0].' '.$this->errorList[$c][1].'</div>';
  82. }
  83. return $output;
  84. }
  85. /**
  86. * Returns the error types acceptable for generation.
  87. */
  88. public function getErrorTypes() {
  89. return $this->errorTypes;
  90. }
  91. }
  92. ?>