1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Error Handler Class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace projectcleverweb\color; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Error Handler Class |
10
|
|
|
*/ |
11
|
|
|
class error { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Error Type Constants |
15
|
|
|
* ==================== |
16
|
|
|
* These constants map to descriptive exception classes |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
const GENERAL_ERROR = 0; |
20
|
|
|
const INVALID_CONFIG = 1; |
21
|
|
|
const INVALID_VALUE = 2; |
22
|
|
|
const INVALID_ARGUMENT = 4; |
23
|
|
|
const INVALID_COLOR = 8; |
24
|
|
|
const OUT_OF_RANGE = 16; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Controls whether or not error reporting is active. |
28
|
|
|
* @var boolean |
29
|
|
|
*/ |
30
|
|
|
protected static $active = TRUE; |
31
|
92 |
|
|
32
|
92 |
|
/** |
33
|
92 |
|
* Controls whether or not to use trigger_error() or to throw exceptions. |
34
|
|
|
* @var boolean |
35
|
92 |
|
*/ |
36
|
|
|
protected static $use_exceptions = TRUE; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Maps each constant to a readable type string |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected static $type_map = array( |
44
|
11 |
|
self::GENERAL_ERROR => 'GENERAL', |
45
|
11 |
|
self::INVALID_CONFIG => 'INVALID_CONFIG', |
46
|
4 |
|
self::INVALID_VALUE => 'INVALID_VALUE', |
47
|
8 |
|
self::INVALID_ARGUMENT => 'INVALID_ARGUMENT', |
48
|
1 |
|
self::INVALID_COLOR => 'INVALID_COLOR', |
49
|
|
|
self::OUT_OF_RANGE => 'OUT_OF_RANGE', |
50
|
7 |
|
); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Maps each constant to a descriptive exception class |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected static $exception_map = array( |
58
|
4 |
|
self::GENERAL_ERROR => __NAMESPACE__.'\\exceptions\\general_error', |
59
|
4 |
|
self::INVALID_CONFIG => __NAMESPACE__.'\\exceptions\\invalid_config', |
60
|
|
|
self::INVALID_VALUE => __NAMESPACE__.'\\exceptions\\invalid_value', |
61
|
|
|
self::INVALID_ARGUMENT => __NAMESPACE__.'\\exceptions\\invalid_argument', |
62
|
|
|
self::INVALID_COLOR => __NAMESPACE__.'\\exceptions\\invalid_color', |
63
|
|
|
self::OUT_OF_RANGE => __NAMESPACE__.'\\exceptions\\out_of_range', |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Allows the modifying of configuration vars for this class. |
68
|
1 |
|
* |
69
|
1 |
|
* @param string $var The variable to modify |
70
|
|
|
* @param bool $value The value to set the variable to |
71
|
|
|
*/ |
72
|
|
|
public static function set(string $var, bool $value) { |
73
|
|
|
if (isset(self::$$var)) { |
74
|
|
|
static::$$var = $value; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* This function chooses the method to report the error if static::$active is |
80
|
|
|
* equal to TRUE. |
81
|
|
|
* |
82
|
|
|
* @param int $code The error code constant that was passed |
83
|
|
|
* @param string $message The message describing the error |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public static function trigger(int $code, string $message) { |
87
|
|
|
if (!static::$active) { |
88
|
|
|
return; |
89
|
|
|
} elseif(static::$use_exceptions) { |
90
|
|
|
static::exception($message, $code); |
91
|
|
|
} |
92
|
|
|
static::standard($message, $code); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Throws an exception with $message |
97
|
|
|
* |
98
|
|
|
* @param string $message The message describing the error |
99
|
|
|
* @return void |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
protected static function exception(string $message, int $code = 0) { |
102
|
|
|
if (!isset(static::$exception_map[$code])) { |
103
|
|
|
throw new exceptions\general_error('Unknown error type for error: '.$message, $code); |
104
|
|
|
$code = 0; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
throw new static::$exception_map[$code]($message, $code); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Triggers a E_USER_WARNING error with $message |
111
|
|
|
* |
112
|
|
|
* @param string $message The message describing the error |
113
|
|
|
* @return void |
|
|
|
|
114
|
|
|
*/ |
115
|
|
|
protected static function standard(string $message, int $code = 0) { |
116
|
|
|
if (!isset(static::$type_map[$code])) { |
117
|
|
|
trigger_error('Unknown error type for error: '.$message); |
118
|
|
|
$code = 0; |
119
|
|
|
} |
120
|
|
|
return trigger_error(sprintf('[%s] %s', static::$type_map[$code], $message), E_USER_WARNING); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.