1
|
|
|
<?php /** MicroFlashMessage */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Web; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* FlashMessage is a flash messenger. |
7
|
|
|
* |
8
|
|
|
* @author Oleg Lunegov <[email protected]> |
9
|
|
|
* @link https://github.com/linpax/microphp-framework |
10
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
11
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
12
|
|
|
* @package Micro |
13
|
|
|
* @subpackage Web |
14
|
|
|
* @version 1.0 |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class FlashMessage |
18
|
|
|
{ |
19
|
|
|
/** @var integer $TYPE_SUCCESS success */ |
20
|
|
|
const TYPE_SUCCESS = 1; |
21
|
|
|
/** @var integer $TYPE_NOTICE notice */ |
22
|
|
|
const TYPE_INFO = 2; |
23
|
|
|
/** @var integer $TYPE_WARNING warning */ |
24
|
|
|
const TYPE_WARNING = 3; |
25
|
|
|
/** @var integer TYPE_DANGER danger */ |
26
|
|
|
const TYPE_DANGER = 4; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** @var ISession $session Session component */ |
30
|
|
|
protected $session; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor messenger |
35
|
|
|
* |
36
|
|
|
* @access public |
37
|
|
|
* |
38
|
|
|
* @param ISession $session |
39
|
|
|
* |
40
|
|
|
* @result void |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ISession $session) |
43
|
|
|
{ |
44
|
|
|
$this->session = $session; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get label for type by id |
49
|
|
|
* |
50
|
|
|
* @access public |
51
|
|
|
* |
52
|
|
|
* @param int $type type id |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
* @static |
56
|
|
|
*/ |
57
|
|
|
public static function getTypeLabel($type = self::TYPE_SUCCESS) |
58
|
|
|
{ |
59
|
|
|
return self::getTypeLabels()[$type]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get labels for types |
64
|
|
|
* |
65
|
|
|
* @access public |
66
|
|
|
* @return string[] |
67
|
|
|
* @static |
68
|
|
|
*/ |
69
|
|
|
public static function getTypeLabels() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
self::TYPE_SUCCESS => 'success', |
73
|
|
|
self::TYPE_INFO => 'info', |
74
|
|
|
self::TYPE_WARNING => 'warning', |
75
|
|
|
self::TYPE_DANGER => 'danger' |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Push a new flash |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* |
84
|
|
|
* @param int $type type id |
85
|
|
|
* @param string $title title flash |
86
|
|
|
* @param string $description description flash |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function push($type = FlashMessage::TYPE_SUCCESS, $title = '', $description = '') |
91
|
|
|
{ |
92
|
|
|
$flashes = $this->session->flash; |
|
|
|
|
93
|
|
|
$flashes[] = [ |
94
|
|
|
'type' => $type, |
95
|
|
|
'title' => $title, |
96
|
|
|
'description' => $description |
97
|
|
|
]; |
98
|
|
|
$this->session->flash = $flashes; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Has flashes by type |
103
|
|
|
* |
104
|
|
|
* @access public |
105
|
|
|
* |
106
|
|
|
* @param int $type type of flash |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function has($type = FlashMessage::TYPE_SUCCESS) |
111
|
|
|
{ |
112
|
|
|
foreach ($this->session->flash AS $element) { |
|
|
|
|
113
|
|
|
if (!empty($element['type']) && $element['type'] === $type) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get flash by type |
123
|
|
|
* |
124
|
|
|
* @access public |
125
|
|
|
* |
126
|
|
|
* @param int $type type of flash |
127
|
|
|
* |
128
|
|
|
* @return array|bool |
129
|
|
|
*/ |
130
|
|
|
public function get($type = FlashMessage::TYPE_SUCCESS) |
131
|
|
|
{ |
132
|
|
|
foreach ($this->session->flash AS $key => $element) { |
|
|
|
|
133
|
|
|
if (!empty($element['type']) && $element['type'] === $type) { |
134
|
|
|
$result = $element; |
135
|
|
|
unset($this->session->flash[$key]); |
136
|
|
|
|
137
|
|
|
return $result; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get all flashes |
146
|
|
|
* |
147
|
|
|
* @access public |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
public function getAll() |
152
|
|
|
{ |
153
|
|
|
$result = $this->session->flash; |
|
|
|
|
154
|
|
|
$this->session->flash = []; |
|
|
|
|
155
|
|
|
|
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: