|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/mvc 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\Mvc\Http; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Filter\StaticFilter; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Session Flash Messages |
|
16
|
|
|
* |
|
17
|
|
|
* @package Slick\Mvc\Http |
|
18
|
|
|
* @author Filipe Silva <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class FlashMessages |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/**#@+ |
|
24
|
|
|
* @const string TYPE for message type constants |
|
25
|
|
|
*/ |
|
26
|
|
|
const TYPE_SUCCESS = 0; |
|
27
|
|
|
const TYPE_ERROR = 1; |
|
28
|
|
|
const TYPE_WARNING = 2; |
|
29
|
|
|
const TYPE_INFO = 3; |
|
30
|
|
|
/**#@-*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Uses session |
|
34
|
|
|
*/ |
|
35
|
|
|
use SessionAwareMethods; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array message type descriptions |
|
39
|
|
|
*/ |
|
40
|
|
|
public $classes = [ |
|
41
|
|
|
self::TYPE_SUCCESS => 'success', |
|
42
|
|
|
self::TYPE_WARNING => 'warning', |
|
43
|
|
|
self::TYPE_INFO => 'info', |
|
44
|
|
|
self::TYPE_ERROR => 'danger' |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected static $messages = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set a flash message of a give type |
|
54
|
|
|
* |
|
55
|
|
|
* @param int $type |
|
56
|
|
|
* @param string $message |
|
57
|
|
|
* |
|
58
|
|
|
* @return self |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function set($type, $message) |
|
61
|
|
|
{ |
|
62
|
2 |
|
$type = StaticFilter::filter('number', $type); |
|
63
|
2 |
|
if ($type < 0 || $type > 3) { |
|
64
|
2 |
|
$type = static::TYPE_INFO; |
|
65
|
1 |
|
} |
|
66
|
2 |
|
self::$messages[$type][] = $message; |
|
67
|
2 |
|
$this->getSessionDriver() |
|
68
|
2 |
|
->set('_messages_', self::$messages) |
|
69
|
|
|
; |
|
70
|
2 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Retrieve all messages and flushes them all |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function get() |
|
79
|
|
|
{ |
|
80
|
2 |
|
self::$messages = $this->getSessionDriver()->get('_messages_', []); |
|
|
|
|
|
|
81
|
2 |
|
$messages = self::$messages; |
|
82
|
2 |
|
$this->flush(); |
|
83
|
2 |
|
return $messages; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* clears all messages |
|
88
|
|
|
* |
|
89
|
|
|
* @return FlashMessages |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function flush() |
|
92
|
|
|
{ |
|
93
|
2 |
|
self::$messages = []; |
|
94
|
2 |
|
$this->getSessionDriver() |
|
95
|
2 |
|
->set('_messages_', self::$messages) |
|
96
|
|
|
; |
|
97
|
2 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
} |
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: