|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tamtamchik\SimpleFlash\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Tamtamchik\SimpleFlash\TemplateInterface; |
|
6
|
|
|
|
|
7
|
|
|
class MessageManager extends SessionManager |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var TemplateManager |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $template; |
|
13
|
|
|
|
|
14
|
|
|
private $types = [ |
|
15
|
|
|
'error', |
|
16
|
|
|
'warning', |
|
17
|
|
|
'info', |
|
18
|
|
|
'success', |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(TemplateInterface $template) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct(); |
|
24
|
|
|
|
|
25
|
|
|
$this->template = new TemplateManager($template); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Add message to Session. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $message - message text |
|
32
|
|
|
* @param string $type - message type: success, info, warning, error |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function appendMessage(string $message = '', string $type = 'info'): void |
|
35
|
|
|
{ |
|
36
|
|
|
$session = $this->getSession(); |
|
37
|
|
|
|
|
38
|
|
|
$type = strip_tags($type); |
|
39
|
|
|
|
|
40
|
|
|
if (empty($message) || ! in_array($type, $this->types)) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ( ! array_key_exists($type, $session)) { |
|
45
|
|
|
$session[$type] = []; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$session[$type][] = $message; |
|
49
|
|
|
|
|
50
|
|
|
$this->setSession($session); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Builds messages for a single type. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $flashes - array of messages to show |
|
57
|
|
|
* @param string $type - message type: success, info, warning, error |
|
58
|
|
|
* |
|
59
|
|
|
* @return string - HTML with flash messages |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function compileMessages(array $flashes, string $type): string |
|
62
|
|
|
{ |
|
63
|
|
|
$messages = ''; |
|
64
|
|
|
foreach ($flashes as $msg) { |
|
65
|
|
|
$messages .= $this->template->getTemplate()->wrapMessage($msg); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->template->getTemplate()->wrapMessages($messages, $type); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns all messages from container. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string|null $type - message type: success, info, warning, error |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function hasMessageType(string $type): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return in_array($type, $this->types); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns if there are any messages in container. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string|null $type - message type: success, info, warning, error |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function hasMessage(string $type = null): bool |
|
91
|
|
|
{ |
|
92
|
|
|
$session = $this->getSession(); |
|
93
|
|
|
|
|
94
|
|
|
if ( ! is_null($type)) { |
|
95
|
|
|
return ! empty($session[$type]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
foreach ($this->types as $type) { |
|
99
|
|
|
if ( ! empty($session[$type])) { |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Clears messages from session store. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string|null $type - message type: success, info, warning, error |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function clearMessages(string $type = null): void |
|
113
|
|
|
{ |
|
114
|
|
|
if (is_null($type)) { |
|
115
|
|
|
$this->setSession([]); |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$session = $this->getSession(); |
|
120
|
|
|
if (array_key_exists($type, $session)) { |
|
121
|
|
|
unset($session[$type]); |
|
122
|
|
|
} |
|
123
|
|
|
$this->setSession($session); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|