1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jasny\Session\Flash; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Flash messages are stored in the session and cleared after they're used. |
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
class FlashBag implements \IteratorAggregate |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
protected \ArrayAccess $session; |
|
|
|
|
13
|
|
|
protected string $key; |
14
|
|
|
|
15
|
|
|
protected array $entries = []; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
|
|
|
|
18
|
|
|
* Class constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $key = 'flash') |
21
|
|
|
{ |
22
|
|
|
$this->key = $key; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get iterator for entries. |
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
public function getIterator(): \Traversable |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
return new \ArrayIterator($this->entries); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get all entries as array. |
36
|
|
|
*/ |
|
|
|
|
37
|
|
|
public function getArrayCopy(): array |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
return $this->entries; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* Get copy with session object. |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
|
|
public function withSession(\ArrayAccess $session): self |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
if (isset($this->session) && $this->session === $session) { |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$copy = clone $this; |
55
|
|
|
$copy->session = $session; |
56
|
|
|
|
57
|
|
|
$copy->initFromSession(); |
58
|
|
|
|
59
|
|
|
return $copy; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initialize the entries from the session. |
64
|
|
|
*/ |
|
|
|
|
65
|
|
|
protected function initFromSession(): void |
66
|
|
|
{ |
67
|
|
|
if (!isset($this->session[$this->key])) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$entries = $this->session[$this->key]; |
72
|
|
|
unset($this->session[$this->key]); |
73
|
|
|
|
74
|
|
|
if (!is_array($entries)) { |
75
|
|
|
trigger_error('Invalid flash session data', E_USER_WARNING); |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->initEntries($entries); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Initialize the entries. |
84
|
|
|
* |
85
|
|
|
* @param array $entries |
|
|
|
|
86
|
|
|
*/ |
|
|
|
|
87
|
|
|
protected function initEntries(array $entries): void |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$invalid = 0; |
90
|
|
|
|
91
|
|
|
foreach ($entries as $entry) { |
92
|
|
|
if (!is_array($entry) || !isset($entry['message'])) { |
93
|
|
|
$invalid++; |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->entries[] = $entry + ['type' => '', 'contentType' => 'text/plain']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($invalid > 0) { |
101
|
|
|
trigger_error( |
102
|
|
|
$invalid === 1 ? "Ignored invalid flash message" : "Ignoring $invalid invalid flash messages", |
103
|
|
|
E_USER_NOTICE, |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add a flash message. |
111
|
|
|
* |
112
|
|
|
* @param string $type flash type, eg. 'error', 'notice' or 'success' |
|
|
|
|
113
|
|
|
* @param string $message flash message |
|
|
|
|
114
|
|
|
* @param string $contentType mime, eg 'text/plain' or 'text/html' |
|
|
|
|
115
|
|
|
* @return $this |
|
|
|
|
116
|
|
|
*/ |
117
|
|
|
public function add(string $type, string $message, string $contentType = 'text/plain'): self |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$this->session[$this->key][] = ['type' => $type, 'message' => $message, 'contentType' => $contentType]; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Reissue the flash messages. |
126
|
|
|
* Get it now and it will remain in the session for next request. |
127
|
|
|
*/ |
|
|
|
|
128
|
|
|
public function reissue(): self |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$this->session[$this->key] = array_merge( |
131
|
|
|
$this->entries, |
132
|
|
|
$this->session[$this->key] ?? [], |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$this->entries = []; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Clear all flash messages. |
142
|
|
|
*/ |
|
|
|
|
143
|
|
|
public function clear(): self |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$this->entries = []; |
146
|
|
|
|
147
|
|
|
if (isset($this->session[$this->key])) { |
148
|
|
|
unset($this->session[$this->key]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|