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