1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\Docweaver; |
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 bool $success; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private string $error; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
private array $messages; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var null|stdClass |
30
|
|
|
*/ |
31
|
|
|
private $extra; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Result constructor. |
35
|
|
|
* |
36
|
|
|
* @param string[] $messages |
37
|
|
|
* @param mixed $extra |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
bool $success = true, |
41
|
|
|
string $error = '', |
42
|
|
|
array $messages = [], |
43
|
|
|
$extra = null |
44
|
|
|
) { |
45
|
|
|
$this->success = $success; |
46
|
|
|
$this->error = $error; |
47
|
|
|
$this->messages = $messages; |
48
|
|
|
$this->extra = $extra; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isSuccess(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->success; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setSuccess(bool $success): self |
57
|
|
|
{ |
58
|
|
|
$clone = clone $this; |
59
|
|
|
$clone->success = $success; |
60
|
|
|
|
61
|
|
|
return $clone; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getError(): string |
65
|
|
|
{ |
66
|
|
|
return $this->error; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setError(string $error): self |
70
|
|
|
{ |
71
|
|
|
$clone = clone $this; |
72
|
|
|
$clone->error = $error; |
73
|
|
|
$clone->success = false; |
74
|
|
|
|
75
|
|
|
return $clone; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getExtra(): ?stdClass |
79
|
|
|
{ |
80
|
|
|
return $this->extra; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function setExtra(stdClass $extra): self |
84
|
|
|
{ |
85
|
|
|
$clone = clone $this; |
86
|
|
|
$clone->extra = $extra; |
87
|
|
|
|
88
|
|
|
return $clone; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getMessage(): string |
92
|
|
|
{ |
93
|
|
|
return empty($this->messages) ? '' : current($this->messages); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setMessage(string $message): self |
97
|
|
|
{ |
98
|
|
|
return $this->addMessage($message); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string[] |
103
|
|
|
*/ |
104
|
|
|
public function getMessages(): array |
105
|
|
|
{ |
106
|
|
|
return $this->messages; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string ...$messages |
111
|
|
|
*/ |
112
|
|
|
public function setMessages(string ...$messages): self |
113
|
|
|
{ |
114
|
|
|
$clone = clone $this; |
115
|
|
|
$clone->messages = $messages; |
116
|
|
|
|
117
|
|
|
return $clone; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function addMessage(string $message): self |
121
|
|
|
{ |
122
|
|
|
$clone = clone $this; |
123
|
|
|
$clone->messages[] = $message; |
124
|
|
|
|
125
|
|
|
return $clone; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function toArray(): array |
132
|
|
|
{ |
133
|
|
|
return (array)$this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
* |
139
|
|
|
* @param int $options |
140
|
|
|
*/ |
141
|
|
|
public function toJson($options = 0): string |
142
|
|
|
{ |
143
|
|
|
return json_encode($this->toArray(), JSON_THROW_ON_ERROR); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|