|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Shared |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Error; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ErrorAwareTrait |
|
19
|
|
|
* |
|
20
|
|
|
* Implementation of ErrorAwareInterface |
|
21
|
|
|
* |
|
22
|
|
|
* @package Phossa2\Shared |
|
23
|
|
|
* @author Hong Zhang <[email protected]> |
|
24
|
|
|
* @see ErrorAwareInterface |
|
25
|
|
|
* @version 2.0.24 |
|
26
|
|
|
* @since 2.0.22 added |
|
27
|
|
|
* @since 2.0.24 added flushError() |
|
28
|
|
|
*/ |
|
29
|
|
|
trait ErrorAwareTrait |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* error message |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
* @access protected |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $error = ''; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* error code |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
* @access protected |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $error_code = ''; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* 0 or '0000' etc means no error, = '' |
|
49
|
|
|
* |
|
50
|
|
|
* @var bool |
|
51
|
|
|
* @access protected |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $zero_empty = true; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritDoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function hasError()/*# : bool */ |
|
59
|
|
|
{ |
|
60
|
|
|
return '' === $this->error_code; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritDoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getError()/*# : string */ |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->error; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getErrorCode()/*# : string */ |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->error_code; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritDoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setError( |
|
83
|
|
|
/*# string */ $message = '', |
|
84
|
|
|
/*# string */ $code = '' |
|
85
|
|
|
)/*# : bool */ { |
|
86
|
|
|
$this->error = (string) $message; |
|
87
|
|
|
|
|
88
|
|
|
// zero ? |
|
89
|
|
|
$zcode = (string) $code; |
|
90
|
|
|
if ($this->zero_empty && '' === str_replace('0', '', $zcode)) { |
|
91
|
|
|
$this->error_code = ''; |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->error_code = $zcode; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function copyError($obj) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($obj instanceof ErrorAwareInterface) { |
|
105
|
|
|
$this->setError($obj->getError(), $obj->getErrorCode()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritDoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function flushError()/*# : bool */ |
|
113
|
|
|
{ |
|
114
|
|
|
$this->setError(); |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|