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