-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFlash.php
More file actions
73 lines (58 loc) · 1.62 KB
/
Copy pathFlash.php
File metadata and controls
73 lines (58 loc) · 1.62 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
<?php
namespace Odan\Session;
use ArrayAccess;
/**
* Flash messages.
*/
final class Flash implements FlashInterface
{
/**
* @var array<string, mixed>|ArrayAccess<string, mixed>
*/
private array|ArrayAccess $storage;
private string $storageKey;
/**
* @param array<string, mixed>|ArrayAccess<string, mixed> $storage
*/
public function __construct(array|ArrayAccess &$storage, string $storageKey = '_flash')
{
$this->storage = &$storage;
$this->storageKey = $storageKey;
}
public function add(string $key, string $message): void
{
// Create array for this key
if (!isset($this->storage[$this->storageKey][$key])) {
$this->storage[$this->storageKey][$key] = [];
}
// Push onto the array
$this->storage[$this->storageKey][$key][] = $message;
}
public function get(string $key): array
{
if (!$this->has($key)) {
return [];
}
$return = $this->storage[$this->storageKey][$key];
unset($this->storage[$this->storageKey][$key]);
return (array)$return;
}
public function has(string $key): bool
{
return isset($this->storage[$this->storageKey][$key]);
}
public function clear(): void
{
unset($this->storage[$this->storageKey]);
}
public function set(string $key, array $messages): void
{
$this->storage[$this->storageKey][$key] = $messages;
}
public function all(): array
{
$result = $this->storage[$this->storageKey] ?? [];
$this->clear();
return (array)$result;
}
}