|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@link OperationResult} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Application Utils |
|
6
|
|
|
* @subpackage Core |
|
7
|
|
|
* @see OperationResult |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace AppUtils; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Operation result container: can be used to store |
|
16
|
|
|
* details on the results of an operation, and its |
|
17
|
|
|
* status. It is intended to be used if the operation |
|
18
|
|
|
* failing is not critical (not worthy of an exception). |
|
19
|
|
|
* |
|
20
|
|
|
* For example, this can be used as return value of |
|
21
|
|
|
* a validation operation, or any other process that |
|
22
|
|
|
* can return error information. |
|
23
|
|
|
* |
|
24
|
|
|
* @package Application Utils |
|
25
|
|
|
* @subpackage Core |
|
26
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class OperationResult |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $message = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $valid = true; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var object |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $subject; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var integer |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $code = 0; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The subject being validated. |
|
52
|
|
|
* |
|
53
|
|
|
* @param object $subject |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(object $subject) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->subject = $subject; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Whether the validation was successful. |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function isValid() : bool |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->valid; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Retrieves the subject that was validated. |
|
72
|
|
|
* |
|
73
|
|
|
* @return object |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getSubject() : object |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->subject; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Makes the result a succes, with the specified message. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $message Should not contain a date, just the system specific info. |
|
84
|
|
|
* @return OperationResult |
|
85
|
|
|
*/ |
|
86
|
|
|
public function makeSuccess(string $message, int $code=0) : OperationResult |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->setMessage($message, $code, true); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Sets the result as an error. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $message Should be as detailed as possible. |
|
95
|
|
|
* @return OperationResult |
|
96
|
|
|
*/ |
|
97
|
|
|
public function makeError(string $message, int $code=0) : OperationResult |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->setMessage($message, $code, false); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function setMessage(string $message, int $code, bool $valid) : OperationResult |
|
103
|
|
|
{ |
|
104
|
|
|
$this->valid = $valid; |
|
105
|
|
|
$this->message = $message; |
|
106
|
|
|
$this->code = $code; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Retrieves the error message, if an error occurred. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string The error message, or an empty string if no error occurred. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getErrorMessage() : string |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->getMessage(false); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Retrieves the success message, if one has been provided. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getSuccessMessage() : string |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->getMessage(true); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function hasCode() : bool |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->code > 0; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getCode() : int |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->code; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
protected function getMessage(bool $valid) : string |
|
142
|
|
|
{ |
|
143
|
|
|
if($this->valid === $valid) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->message; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return ''; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|