|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of web-stack |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Slick\WebStack\Infrastructure\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Slick\Http\Session\SessionDriverInterface; |
|
15
|
|
|
use Slick\WebStack\Infrastructure\Http\FlashMessage\FlashMessageType; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* FlashMessageController |
|
19
|
|
|
* |
|
20
|
|
|
* @package Slick\WebStack\Infrastructure\Http |
|
21
|
|
|
*/ |
|
22
|
|
|
class FlashMessageStorage |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array<string, array<FlashMessageInterface>> |
|
26
|
|
|
*/ |
|
27
|
|
|
private array $flashMessages; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Creates a FlashMessageStorage |
|
31
|
|
|
* |
|
32
|
|
|
* @param SessionDriverInterface $session A session driver instance. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(private readonly SessionDriverInterface $session) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->flashMessages = unserialize($this->session->get('flash_messages', serialize([]))); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Adds a flash message to the array of flash messages. |
|
41
|
|
|
* |
|
42
|
|
|
* @param FlashMessageInterface $message The flash message to be added. |
|
43
|
|
|
* |
|
44
|
|
|
* @return self Returns an instance of the current class. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addMessage(FlashMessageInterface $message): self |
|
47
|
|
|
{ |
|
48
|
|
|
$this->flashMessages[$message->type()->value][] = $message; |
|
49
|
|
|
$this->saveInSession(); |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Retrieves the flash messages of a specific type or all the flash messages |
|
55
|
|
|
* if no type is specified. |
|
56
|
|
|
* |
|
57
|
|
|
* @param FlashMessageType|null $type The type of flash messages to retrieve. |
|
58
|
|
|
* If null, retrieves all flash messages. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array<FlashMessageInterface> The array of flash messages. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function consume(?FlashMessageType $type = null): array |
|
63
|
|
|
{ |
|
64
|
|
|
if (null === $type) { |
|
65
|
|
|
return $this->allTypes(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->withType($type); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Filters the flash messages by type and returns an array of consumed messages. |
|
73
|
|
|
* |
|
74
|
|
|
* @param FlashMessageType $type The type of flash messages to filter by. |
|
75
|
|
|
* |
|
76
|
|
|
* @return array<FlashMessageInterface> Returns an array of consumed flash messages. |
|
77
|
|
|
*/ |
|
78
|
|
|
private function withType(FlashMessageType $type): array |
|
79
|
|
|
{ |
|
80
|
|
|
if (array_key_exists($type->value, $this->flashMessages)) { |
|
81
|
|
|
$messages = array_map(fn ($message) => $message->consume(), $this->flashMessages[$type->value]); |
|
82
|
|
|
unset($this->flashMessages[$type->value]); |
|
83
|
|
|
$this->saveInSession(); |
|
84
|
|
|
return $messages; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return []; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Retrieves all flash message types and consumes them. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array<FlashMessageInterface> An array containing the consumed flash messages. |
|
94
|
|
|
*/ |
|
95
|
|
|
private function allTypes(): array |
|
96
|
|
|
{ |
|
97
|
|
|
$messages = []; |
|
98
|
|
|
array_walk_recursive( |
|
99
|
|
|
$this->flashMessages, |
|
100
|
|
|
function (FlashMessageInterface $message) use (&$messages) { |
|
101
|
|
|
$messages[] = $message->consume(); |
|
102
|
|
|
} |
|
103
|
|
|
); |
|
104
|
|
|
$this->flashMessages = []; |
|
105
|
|
|
$this->saveInSession(); |
|
106
|
|
|
return $messages; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Saves the flash messages in the session. |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
private function saveInSession(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->session->set('flash_messages', serialize($this->flashMessages)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|