|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/web_stack package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\WebStack\Service; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Filter\StaticFilter; |
|
13
|
|
|
use Slick\Http\SessionDriverInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* FlashMessages |
|
17
|
|
|
* |
|
18
|
|
|
* @package Slick\WebStack\Service |
|
19
|
|
|
* @author Filipe Silva <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class FlashMessages |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var SessionDriverInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $sessionDriver; |
|
27
|
|
|
|
|
28
|
|
|
/**#@+ |
|
29
|
|
|
* @const string TYPE for message type constants |
|
30
|
|
|
*/ |
|
31
|
|
|
const TYPE_SUCCESS = 'success'; |
|
32
|
|
|
const TYPE_ERROR = 'danger'; |
|
33
|
|
|
const TYPE_WARNING = 'warning'; |
|
34
|
|
|
const TYPE_INFO = 'info'; |
|
35
|
|
|
/**#@-*/ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $messages = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Creates a flash messages service |
|
44
|
|
|
* |
|
45
|
|
|
* @param SessionDriverInterface $sessionDriver |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(SessionDriverInterface $sessionDriver) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->sessionDriver = $sessionDriver; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set a flash message of a give type |
|
54
|
|
|
* |
|
55
|
|
|
* @param int $type |
|
56
|
|
|
* @param string $message |
|
57
|
|
|
* |
|
58
|
|
|
* @return FlashMessages |
|
59
|
|
|
*/ |
|
60
|
|
|
public function set($type, $message) |
|
61
|
|
|
{ |
|
62
|
|
|
$types = [ |
|
63
|
|
|
self::TYPE_ERROR, self::TYPE_INFO, self::TYPE_SUCCESS, |
|
64
|
|
|
self::TYPE_WARNING |
|
65
|
|
|
]; |
|
66
|
|
|
if (!in_array($type, $types)) { |
|
67
|
|
|
$type = self::TYPE_INFO; |
|
68
|
|
|
} |
|
69
|
|
|
self::$messages[$type][] = $message; |
|
70
|
|
|
$this->sessionDriver->set('_messages_', self::$messages); |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* clears all messages |
|
76
|
|
|
* |
|
77
|
|
|
* @return FlashMessages |
|
78
|
|
|
*/ |
|
79
|
|
|
public function flush() |
|
80
|
|
|
{ |
|
81
|
|
|
self::$messages = []; |
|
82
|
|
|
$this->sessionDriver->erase('_messages_'); |
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieve all messages and flushes them all |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function messages() |
|
92
|
|
|
{ |
|
93
|
|
|
$messages = $this->sessionDriver->get('_messages_', []); |
|
|
|
|
|
|
94
|
|
|
$this->flush(); |
|
95
|
|
|
return $messages; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Add an info flash message |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $message |
|
102
|
|
|
* @return self |
|
103
|
|
|
*/ |
|
104
|
|
|
public function addInfo($message) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->set(FlashMessages::TYPE_INFO, $message); |
|
107
|
|
|
} |
|
108
|
|
|
/** |
|
109
|
|
|
* Add a warning flash message |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $message |
|
112
|
|
|
* @return self |
|
113
|
|
|
*/ |
|
114
|
|
|
public function addWarning($message) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->set(FlashMessages::TYPE_WARNING, $message); |
|
117
|
|
|
} |
|
118
|
|
|
/** |
|
119
|
|
|
* Add an error flash message |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $message |
|
122
|
|
|
* @return self |
|
123
|
|
|
*/ |
|
124
|
|
|
public function addError($message) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->set(FlashMessages::TYPE_ERROR, $message); |
|
127
|
|
|
} |
|
128
|
|
|
/** |
|
129
|
|
|
* Add a success flash message |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $message |
|
132
|
|
|
* @return self |
|
133
|
|
|
*/ |
|
134
|
|
|
public function addSuccess($message) |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->set(FlashMessages::TYPE_SUCCESS, $message); |
|
137
|
|
|
} |
|
138
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: