1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\Docweaver\VO; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
9
|
|
|
use stdClass; |
10
|
|
|
|
11
|
|
|
class Result implements Arrayable, Jsonable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private $success; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $error; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
private $messages; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var null|stdClass |
30
|
|
|
*/ |
31
|
|
|
private $data; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Result constructor. |
35
|
|
|
* |
36
|
|
|
* @param bool $success |
37
|
|
|
* @param string $error |
38
|
|
|
* @param string[] $messages |
39
|
|
|
* @param mixed $extra |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
bool $success = true, |
43
|
|
|
string $error = '', |
44
|
|
|
array $messages = [], |
45
|
|
|
$extra = null |
46
|
|
|
) { |
47
|
|
|
$this->success = $success; |
48
|
|
|
$this->error = $error; |
49
|
|
|
$this->messages = $messages; |
50
|
|
|
$this->data = $extra; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function isSuccess(): bool |
57
|
|
|
{ |
58
|
|
|
return $this->success; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param bool $success |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
|
|
public function setSuccess(bool $success): self |
67
|
|
|
{ |
68
|
|
|
$clone = clone $this; |
69
|
|
|
$clone->success = $success; |
70
|
|
|
|
71
|
|
|
return $clone; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getError(): string |
78
|
|
|
{ |
79
|
|
|
return $this->error; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $error |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
|
|
public function setError(string $error): self |
88
|
|
|
{ |
89
|
|
|
$clone = clone $this; |
90
|
|
|
$clone->error = $error; |
91
|
|
|
$clone->success = false; |
92
|
|
|
|
93
|
|
|
return $clone; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return null|stdClass |
98
|
|
|
*/ |
99
|
|
|
public function getData(): ?stdClass |
100
|
|
|
{ |
101
|
|
|
return $this->data; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param stdClass $data |
106
|
|
|
* |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function setData(stdClass $data): self |
110
|
|
|
{ |
111
|
|
|
$clone = clone $this; |
112
|
|
|
$clone->data = $data; |
113
|
|
|
|
114
|
|
|
return $clone; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getMessage(): string |
121
|
|
|
{ |
122
|
|
|
return empty($this->messages) ? '' : current($this->messages); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $message |
127
|
|
|
* |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
|
|
public function setMessage(string $message): self |
131
|
|
|
{ |
132
|
|
|
return $this->addMessage($message); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string[] |
137
|
|
|
*/ |
138
|
|
|
public function getMessages(): array |
139
|
|
|
{ |
140
|
|
|
return $this->messages; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string ...$messages |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
|
|
|
|
148
|
|
|
public function setMessages(string ...$messages): self |
149
|
|
|
{ |
150
|
|
|
$clone = clone $this; |
151
|
|
|
$clone->messages = $messages; |
152
|
|
|
|
153
|
|
|
return $clone; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $message |
158
|
|
|
* |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
|
|
public function addMessage(string $message): self |
162
|
|
|
{ |
163
|
|
|
$clone = clone $this; |
164
|
|
|
$clone->messages[] = $message; |
165
|
|
|
|
166
|
|
|
return $clone; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function toArray() |
175
|
|
|
{ |
176
|
|
|
return (array)$this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
* |
182
|
|
|
* @param int $options |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function toJson($options = 0) |
187
|
|
|
{ |
188
|
|
|
return json_encode($this->toArray()); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.