1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coderello\Laraflash; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use Coderello\Laraflash\Exceptions\InvalidDelayException; |
7
|
|
|
use Coderello\Laraflash\Exceptions\InvalidArgumentException; |
8
|
|
|
use Coderello\Laraflash\Exceptions\InvalidHopsAmountException; |
9
|
|
|
use Coderello\Laraflash\Contracts\FlashMessage as FlashMessageContract; |
10
|
|
|
|
11
|
|
|
class FlashMessage implements FlashMessageContract |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
const MUTABLE_PROPERTIES = ['title', 'content', 'type', 'hops', 'delay', 'important']; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string|null |
20
|
|
|
*/ |
21
|
|
|
protected $title; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
protected $content; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
protected $type; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int|null |
35
|
|
|
*/ |
36
|
|
|
protected $hops; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int|null |
40
|
|
|
*/ |
41
|
|
|
protected $delay; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool|null |
45
|
|
|
*/ |
46
|
|
|
protected $important; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* FlashMessage constructor. |
50
|
|
|
*/ |
51
|
41 |
|
public function __construct() |
52
|
|
|
{ |
53
|
41 |
|
$this->hops(1); |
54
|
|
|
|
55
|
41 |
|
$this->delay(1); |
56
|
|
|
|
57
|
41 |
|
$this->important(false); |
58
|
41 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the title for the current FlashMessage instance. |
62
|
|
|
* |
63
|
|
|
* @param string $title |
64
|
|
|
* |
65
|
|
|
* @return FlashMessage |
66
|
|
|
*/ |
67
|
8 |
|
public function title(string $title): FlashMessageContract |
68
|
|
|
{ |
69
|
8 |
|
$this->title = $title; |
70
|
|
|
|
71
|
8 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the content for the current FlashMessage instance. |
76
|
|
|
* |
77
|
|
|
* @param string $content |
78
|
|
|
* |
79
|
|
|
* @return FlashMessage |
80
|
|
|
*/ |
81
|
3 |
|
public function content(string $content): FlashMessageContract |
82
|
|
|
{ |
83
|
3 |
|
$this->content = $content; |
84
|
|
|
|
85
|
3 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the type for the current FlashMessage instance. |
90
|
|
|
* |
91
|
|
|
* @param string $type |
92
|
|
|
* |
93
|
|
|
* @return FlashMessage |
94
|
|
|
*/ |
95
|
20 |
|
public function type(string $type): FlashMessageContract |
96
|
|
|
{ |
97
|
20 |
|
$this->type = $type; |
98
|
|
|
|
99
|
20 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the hops amount for the current FlashMessage instance. |
104
|
|
|
* |
105
|
|
|
* @param int $hops |
106
|
|
|
* |
107
|
|
|
* @throws InvalidHopsAmountException |
108
|
|
|
* |
109
|
|
|
* @return FlashMessage |
110
|
|
|
*/ |
111
|
41 |
|
public function hops(int $hops): FlashMessageContract |
112
|
|
|
{ |
113
|
41 |
|
if ($hops < 1) { |
114
|
1 |
|
throw new InvalidHopsAmountException; |
115
|
|
|
} |
116
|
|
|
|
117
|
41 |
|
$this->hops = $hops; |
118
|
|
|
|
119
|
41 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the delay for the current FlashMessage instance. |
124
|
|
|
* |
125
|
|
|
* @param int $delay |
126
|
|
|
* |
127
|
|
|
* @throws InvalidDelayException |
128
|
|
|
* |
129
|
|
|
* @return FlashMessage |
130
|
|
|
*/ |
131
|
41 |
|
public function delay(int $delay): FlashMessageContract |
132
|
|
|
{ |
133
|
41 |
|
if ($delay < 0) { |
134
|
1 |
|
throw new InvalidDelayException; |
135
|
|
|
} |
136
|
|
|
|
137
|
41 |
|
$this->delay = $delay; |
138
|
|
|
|
139
|
41 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the important flag for the current FlashMessage instance. |
144
|
|
|
* |
145
|
|
|
* @param bool $important |
146
|
|
|
* |
147
|
|
|
* @return FlashMessage |
148
|
|
|
*/ |
149
|
41 |
|
public function important(bool $important = true): FlashMessageContract |
150
|
|
|
{ |
151
|
41 |
|
$this->important = $important; |
152
|
|
|
|
153
|
41 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Show the message during the current request. |
158
|
|
|
* |
159
|
|
|
* @return FlashMessage |
160
|
|
|
*/ |
161
|
2 |
|
public function now(): FlashMessageContract |
162
|
|
|
{ |
163
|
2 |
|
$this->delay(0); |
164
|
|
|
|
165
|
2 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Keep the message for one more request. |
170
|
|
|
* |
171
|
|
|
* @return FlashMessage |
172
|
|
|
*/ |
173
|
2 |
|
public function keep(): FlashMessageContract |
174
|
|
|
{ |
175
|
2 |
|
$this->hops++; |
176
|
|
|
|
177
|
2 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the evaluated contents of the object. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
* |
185
|
|
|
* @throws Throwable |
186
|
|
|
*/ |
187
|
2 |
|
public function render(): string |
188
|
|
|
{ |
189
|
2 |
|
return view(config('laraflash.skin'), $this->toArray())->render(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Data which should be serialized to JSON. |
194
|
|
|
* |
195
|
|
|
* @return array|mixed |
196
|
|
|
*/ |
197
|
2 |
|
public function jsonSerialize() |
198
|
|
|
{ |
199
|
2 |
|
return $this->toArray(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Convert the object to its JSON representation. |
204
|
|
|
* |
205
|
|
|
* @param int $options |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
1 |
|
public function toJson($options = 0): string |
210
|
|
|
{ |
211
|
1 |
|
return json_encode($this, $options); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get the instance as an array. |
216
|
|
|
* |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
22 |
|
public function toArray(): array |
220
|
|
|
{ |
221
|
|
|
return array_reduce(self::MUTABLE_PROPERTIES, function (array $accumulator, string $property) { |
222
|
22 |
|
$accumulator[$property] = $this->{$property}; |
223
|
|
|
|
224
|
22 |
|
return $accumulator; |
225
|
22 |
|
}, []); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Whether a offset exists. |
230
|
|
|
* |
231
|
|
|
* @param mixed $offset |
232
|
|
|
* |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
1 |
|
public function offsetExists($offset) |
236
|
|
|
{ |
237
|
1 |
|
return $this->isMutableProperty($offset); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Offset to retrieve. |
242
|
|
|
* |
243
|
|
|
* @param mixed $offset |
244
|
|
|
* |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
14 |
|
public function offsetGet($offset) |
248
|
|
|
{ |
249
|
14 |
|
if (! $this->isMutableProperty($offset)) { |
250
|
1 |
|
throw new InvalidArgumentException; |
251
|
|
|
} |
252
|
|
|
|
253
|
13 |
|
return $this->{$offset}; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Offset to set. |
258
|
|
|
* |
259
|
|
|
* @param mixed $offset |
260
|
|
|
* @param mixed $value |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
19 |
|
public function offsetSet($offset, $value) |
265
|
|
|
{ |
266
|
19 |
|
if (! $this->isMutableProperty($offset)) { |
267
|
1 |
|
throw new InvalidArgumentException; |
268
|
|
|
} |
269
|
|
|
|
270
|
18 |
|
$this->{$offset}($value); |
271
|
18 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Offset to unset. |
275
|
|
|
* |
276
|
|
|
* @param mixed $offset |
277
|
|
|
* |
278
|
|
|
* @return void |
279
|
|
|
*/ |
280
|
1 |
|
public function offsetUnset($offset) |
281
|
|
|
{ |
282
|
|
|
// |
283
|
1 |
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Whether a property is mutable. |
287
|
|
|
* |
288
|
|
|
* @param string $property |
289
|
|
|
* |
290
|
|
|
* @return bool |
291
|
|
|
*/ |
292
|
23 |
|
protected function isMutableProperty(string $property): bool |
293
|
|
|
{ |
294
|
23 |
|
return in_array($property, self::MUTABLE_PROPERTIES); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|