1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Drone\Error; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ErrorTrait trait |
15
|
|
|
* |
16
|
|
|
* Standard error management for some classes |
17
|
|
|
*/ |
18
|
|
|
trait ErrorTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Common errors |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $standardErrors = [ |
26
|
|
|
|
27
|
|
|
# File errros |
28
|
|
|
1 => 'Failed to open stream \'%file%\', Permission Denied!', |
29
|
|
|
2 => 'No such file or directory \'%file%\'', |
30
|
|
|
3 => 'File exists \'%file%\'', |
31
|
|
|
4 => 'Stream \'%file%\' is Not a directory', |
32
|
|
|
|
33
|
|
|
# JSON errors |
34
|
|
|
10 => 'Failed to decode JSON file \'%file%\'', |
35
|
|
|
11 => 'Failed to encode JSON file \'%file%\'', |
36
|
|
|
|
37
|
|
|
# Database related errors |
38
|
|
|
20 => 'The transaction was already started', |
39
|
|
|
21 => 'The transaction has not been started', |
40
|
|
|
22 => 'Transaction cannot be empty', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Failure messages |
45
|
|
|
* |
46
|
|
|
* This member stores all failure messages as an array of key/value pairs. |
47
|
|
|
* |
48
|
|
|
* key: The the code of the error (usually in Errno class). Also the ERROR_CONSTANT. |
49
|
|
|
* value: The error message. |
50
|
|
|
* |
51
|
|
|
* If the key is not an integer, the error is not in Errno class. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $errors = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns an array with all failure messages |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
3 |
|
public function getErrors() |
63
|
|
|
{ |
64
|
3 |
|
return $this->errors; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns true if there is at least one error or false if not |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
1 |
|
public function hasErrors() |
73
|
|
|
{ |
74
|
1 |
|
return (bool) count($this->errors); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Adds an error |
79
|
|
|
* |
80
|
|
|
* When the error is standard (i.e. exists in Errno class) the behavior is as follow: |
81
|
|
|
* - _error(int ERROR_CONSTANT): Adds a standard error without replace the wildcard |
82
|
|
|
* - _error(int ERROR_CONSTANT, string $message): Adds a standard error replacing the wildcard |
83
|
|
|
* |
84
|
|
|
* When the error is non-standard (i.e. is not a member of Errno class) the behavior is as follow: |
85
|
|
|
* - _error(int $code, string $message): Adds a non-standard error |
86
|
|
|
* - _error(string $message): Adds a non-standard error creating a generated base64 code |
87
|
|
|
* |
88
|
|
|
* @param integer|string $code |
89
|
|
|
* @param string $message |
90
|
|
|
* |
91
|
|
|
* @return null |
92
|
|
|
*/ |
93
|
12 |
|
private function errorCall($code, $message = null) |
94
|
|
|
{ |
95
|
12 |
|
if (!is_null($code) && !is_integer($code) && !is_string($code)) { |
|
|
|
|
96
|
|
|
throw new \InvalidArgumentException("Invalid type given. Integer or string expected"); |
97
|
|
|
} |
98
|
|
|
|
99
|
12 |
|
if (is_null($code)) { |
|
|
|
|
100
|
1 |
|
$code = preg_replace('/=|\/|\+/', "", base64_encode($message)); |
101
|
|
|
} else { |
102
|
11 |
|
if (!array_key_exists($code, $this->standardErrors) && empty($message)) { |
103
|
|
|
/* |
104
|
|
|
* Non-standard errors must have a message to describe the error, make sure |
105
|
|
|
* you execute the error() method with a message as the second parameter. |
106
|
|
|
*/ |
107
|
1 |
|
throw new \LogicException('The message does not be empty in non-standard errors!'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
11 |
|
if (!array_key_exists($code, $this->errors)) { |
112
|
11 |
|
$this->errors[$code] = (array_key_exists($code, $this->standardErrors)) |
113
|
|
|
? |
114
|
3 |
|
is_null($message) |
115
|
1 |
|
? preg_replace('/\s\'%[a-zA-Z]*%\'/', $message, $this->standardErrors[$code]) |
116
|
|
|
# if $message is not null it will replace the %file% wildcard |
117
|
3 |
|
: preg_replace('/%[a-zA-Z]*%/', $message, $this->standardErrors[$code]) |
118
|
8 |
|
: $message; |
119
|
|
|
} |
120
|
11 |
|
} |
121
|
|
|
|
122
|
12 |
|
public function __call($method, $arguments) |
123
|
|
|
{ |
124
|
12 |
|
if ($method == 'error') { |
125
|
12 |
|
switch (count($arguments)) { |
126
|
12 |
|
case 1: |
127
|
3 |
|
if (is_integer($arguments[0])) { |
128
|
2 |
|
return call_user_func([$this, 'errorCall'], array_shift($arguments)); |
129
|
|
|
} else { |
130
|
1 |
|
return call_user_func([$this, 'errorCall'], null, array_shift($arguments)); |
131
|
|
|
} |
132
|
|
|
break; |
133
|
10 |
|
case 2: |
134
|
10 |
|
return call_user_func([$this, 'errorCall'], $arguments[0], $arguments[1]); |
135
|
|
|
break; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|