|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coderello\Laraflash; |
|
4
|
|
|
|
|
5
|
|
|
use Throwable; |
|
6
|
|
|
use Coderello\Laraflash\Contracts\FlashMessage; |
|
|
|
|
|
|
7
|
|
|
use Coderello\Laraflash\Exceptions\InvalidArgumentException; |
|
8
|
|
|
use Coderello\Laraflash\Contracts\FlashMessagesBag as FlashMessagesBagContract; |
|
9
|
|
|
|
|
10
|
|
|
class FlashMessagesBag implements FlashMessagesBagContract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* An array for storing bag items. |
|
14
|
|
|
* |
|
15
|
|
|
* @var FlashMessage[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $messages = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create a new FlashMessage instance and put it in the bag. |
|
21
|
|
|
* |
|
22
|
|
|
* @param FlashMessage|null $message |
|
23
|
|
|
* |
|
24
|
|
|
* @return FlashMessage |
|
25
|
|
|
*/ |
|
26
|
18 |
|
public function add(?FlashMessage $message = null): FlashMessage |
|
27
|
|
|
{ |
|
28
|
18 |
|
if (is_null($message)) { |
|
29
|
17 |
|
$message = app(FlashMessage::class); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
18 |
|
$this->messages[] = $message; |
|
33
|
|
|
|
|
34
|
18 |
|
return $message; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Delete all instances of FlashMessage from the bag. |
|
39
|
|
|
* |
|
40
|
|
|
* @return FlashMessagesBagContract |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function clear(): FlashMessagesBagContract |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this->messages = []; |
|
45
|
|
|
|
|
46
|
1 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Add one hop to each message. |
|
51
|
|
|
* |
|
52
|
|
|
* @return FlashMessagesBagContract |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function keep(): FlashMessagesBagContract |
|
55
|
|
|
{ |
|
56
|
1 |
|
foreach ($this->messages as $message) { |
|
57
|
1 |
|
$message->keep(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get all messages from the bag. |
|
65
|
|
|
* |
|
66
|
|
|
* @return FlashMessage[] |
|
67
|
|
|
*/ |
|
68
|
7 |
|
public function all(): array |
|
69
|
|
|
{ |
|
70
|
7 |
|
return $this->messages; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get messages that should be displayed during the current request from the bag. |
|
75
|
|
|
* |
|
76
|
|
|
* @return FlashMessage[] |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public function ready(): array |
|
79
|
|
|
{ |
|
80
|
|
|
return array_filter($this->messages, function (FlashMessage $message) { |
|
81
|
5 |
|
return $message->toArray()['delay'] === 0; |
|
82
|
6 |
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Prepare the bag before use (decrement amount of hops and delay, delete expired messages). |
|
87
|
|
|
* |
|
88
|
|
|
* @return FlashMessagesBagContract |
|
89
|
|
|
*/ |
|
90
|
12 |
|
public function prepare(): FlashMessagesBagContract |
|
91
|
|
|
{ |
|
92
|
12 |
|
foreach ($this->messages as $key => $message) { |
|
93
|
9 |
|
if ($message['hops'] <= 1 && $message['delay'] === 0) { |
|
94
|
1 |
|
unset($this->messages[$key]); |
|
95
|
|
|
|
|
96
|
1 |
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
8 |
|
if ($message['hops'] > 1 && $message['delay'] === 0) { |
|
100
|
2 |
|
$message->hops($message['hops'] - 1); |
|
101
|
|
|
|
|
102
|
2 |
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
6 |
|
if ($message['delay'] > 0) { |
|
106
|
6 |
|
$message->delay($message['delay'] - 1); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
12 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the evaluated contents of the object. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
* |
|
118
|
|
|
* @throws Throwable |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function render(): string |
|
121
|
|
|
{ |
|
122
|
|
|
return implode(config('laraflash.separator'), array_map(function (FlashMessage $message) { |
|
123
|
1 |
|
return $message->render(); |
|
124
|
1 |
|
}, $this->ready())); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Data which should be serialized to JSON. |
|
129
|
|
|
* |
|
130
|
|
|
* @return array|mixed |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function jsonSerialize() |
|
133
|
|
|
{ |
|
134
|
2 |
|
return $this->toArray(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Convert the object to its JSON representation. |
|
139
|
|
|
* |
|
140
|
|
|
* @param int $options |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function toJson($options = 0): string |
|
145
|
|
|
{ |
|
146
|
1 |
|
return json_encode($this, $options); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the instance as an array. |
|
151
|
|
|
* |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
3 |
|
public function toArray(): array |
|
155
|
|
|
{ |
|
156
|
|
|
return array_values(array_map(function (FlashMessage $message) { |
|
157
|
3 |
|
return $message->toArray(); |
|
158
|
3 |
|
}, $this->ready())); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Whether a offset exists. |
|
163
|
|
|
* |
|
164
|
|
|
* @param mixed $offset |
|
165
|
|
|
* |
|
166
|
|
|
* @return bool |
|
167
|
|
|
*/ |
|
168
|
2 |
|
public function offsetExists($offset) |
|
169
|
|
|
{ |
|
170
|
2 |
|
return isset($this->messages[$offset]); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Offset to retrieve. |
|
175
|
|
|
* |
|
176
|
|
|
* @param mixed $offset |
|
177
|
|
|
* |
|
178
|
|
|
* @return FlashMessage |
|
179
|
|
|
*/ |
|
180
|
7 |
|
public function offsetGet($offset) |
|
181
|
|
|
{ |
|
182
|
7 |
|
return $this->messages[$offset]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Offset to set. |
|
187
|
|
|
* |
|
188
|
|
|
* @param mixed $offset |
|
189
|
|
|
* @param FlashMessage $value |
|
190
|
|
|
* |
|
191
|
|
|
* @return void |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function offsetSet($offset, $value) |
|
194
|
|
|
{ |
|
195
|
1 |
|
if (! $value instanceof FlashMessage) { |
|
|
|
|
|
|
196
|
1 |
|
throw new InvalidArgumentException; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
1 |
|
$this->messages[$offset] = $value; |
|
200
|
1 |
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Offset to unset. |
|
204
|
|
|
* |
|
205
|
|
|
* @param mixed $offset |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function offsetUnset($offset) |
|
208
|
|
|
{ |
|
209
|
1 |
|
unset($this->messages[$offset]); |
|
210
|
1 |
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: