1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\Scavenger; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
9
|
|
|
use JsonException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Result. |
13
|
|
|
*/ |
14
|
|
|
final class Result implements Arrayable, Jsonable |
15
|
|
|
{ |
16
|
|
|
private bool $success; |
17
|
|
|
private string $message; |
18
|
|
|
private $data; |
19
|
|
|
private $extra; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
private array $errors; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Result constructor. |
28
|
|
|
* |
29
|
|
|
* @param string[] $errors |
30
|
|
|
* @param null|mixed $data |
31
|
|
|
* @param null|mixed $extra |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
bool $success = false, |
35
|
|
|
array $errors = [], |
36
|
|
|
string $message = '', |
37
|
|
|
$data = null, |
38
|
|
|
$extra = null |
39
|
|
|
) { |
40
|
|
|
$this->success = $success; |
41
|
|
|
$this->errors = $errors; |
42
|
|
|
$this->message = $message; |
43
|
|
|
$this->data = $data; |
44
|
|
|
$this->extra = $extra; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isSuccess(): bool |
48
|
|
|
{ |
49
|
|
|
return $this->success; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Result |
54
|
|
|
*/ |
55
|
|
|
public function setSuccess(bool $success): self |
56
|
|
|
{ |
57
|
|
|
$clone = clone $this; |
58
|
|
|
$clone->success = $success; |
59
|
|
|
|
60
|
|
|
return $clone; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string[] |
65
|
|
|
*/ |
66
|
|
|
public function getErrors(): array |
67
|
|
|
{ |
68
|
|
|
return $this->errors; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Result |
73
|
|
|
*/ |
74
|
|
|
public function addError(string $error): self |
75
|
|
|
{ |
76
|
|
|
$clone = clone $this; |
77
|
|
|
$clone->errors[] = $error; |
78
|
|
|
|
79
|
|
|
return $clone; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string[] $errors |
84
|
|
|
* |
85
|
|
|
* @return Result |
86
|
|
|
*/ |
87
|
|
|
public function addErrors(array $errors): self |
88
|
|
|
{ |
89
|
|
|
$clone = clone $this; |
90
|
|
|
$clone->errors = array_merge($clone->errors, $errors); |
91
|
|
|
|
92
|
|
|
return $clone; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function hasErrors(): bool |
96
|
|
|
{ |
97
|
|
|
return !empty($this->getErrors()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getExtra() |
101
|
|
|
{ |
102
|
|
|
return $this->extra; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setExtra($extra): self |
106
|
|
|
{ |
107
|
|
|
$clone = clone $this; |
108
|
|
|
$clone->extra = $extra; |
109
|
|
|
|
110
|
|
|
return $clone; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getMessage(): string |
114
|
|
|
{ |
115
|
|
|
return $this->message; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setMessage(string $message): self |
119
|
|
|
{ |
120
|
|
|
$clone = clone $this; |
121
|
|
|
$clone->message = $message; |
122
|
|
|
|
123
|
|
|
return $clone; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getData() |
127
|
|
|
{ |
128
|
|
|
return $this->data; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setData($data): self |
132
|
|
|
{ |
133
|
|
|
$clone = clone $this; |
134
|
|
|
$clone->data = $data; |
135
|
|
|
|
136
|
|
|
return $clone; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function toArray(): array |
143
|
|
|
{ |
144
|
|
|
return (array)$this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
* |
150
|
|
|
* @param int $options |
151
|
|
|
* |
152
|
|
|
* @throws JsonException |
153
|
|
|
*/ |
154
|
|
|
public function toJson($options = 0): string |
155
|
|
|
{ |
156
|
|
|
return json_encode($this->toArray(), JSON_THROW_ON_ERROR); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|