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