|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class for the flash message |
|
7
|
|
|
*/ |
|
8
|
|
|
class Flash |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var object |
|
12
|
|
|
*/ |
|
13
|
|
|
protected static $data = null; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Check if the flash is set. |
|
17
|
|
|
* |
|
18
|
|
|
* @return boolean |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public static function isIssued() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
2 |
|
return isset($_SESSION['flash']) || isset(static::$data); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Set the flash. |
|
27
|
|
|
* |
|
28
|
|
|
* @param mixed $type flash type, eg. 'error', 'notice' or 'success' |
|
29
|
|
|
* @param mixed $message flash message |
|
30
|
|
|
*/ |
|
31
|
4 |
|
public static function set($type, $message) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
4 |
|
if (!$type) { |
|
34
|
1 |
|
throw new \InvalidArgumentException("Type should not be empty"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
static::$data = (object)['type'=>$type, 'message'=>$message]; |
|
38
|
|
|
|
|
39
|
3 |
|
$_SESSION['flash'] = static::$data; |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the flash. |
|
44
|
|
|
* |
|
45
|
|
|
* @return object |
|
46
|
|
|
*/ |
|
47
|
3 |
View Code Duplication |
public static function get() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
3 |
|
if (!isset(static::$data) && isset($_SESSION['flash'])) { |
|
50
|
2 |
|
static::$data = (object)$_SESSION['flash']; |
|
51
|
2 |
|
unset($_SESSION['flash']); |
|
52
|
2 |
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
return static::$data; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Reissue the flash. |
|
59
|
|
|
*/ |
|
60
|
2 |
View Code Duplication |
public static function reissue() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
2 |
|
if (!isset(static::$data) && isset($_SESSION['flash'])) { |
|
63
|
2 |
|
static::$data = (object)$_SESSION['flash']; |
|
64
|
2 |
|
} else { |
|
65
|
2 |
|
$_SESSION['flash'] = static::$data; |
|
66
|
|
|
} |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Clear the flash. |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public static function clear() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
3 |
|
self::$data = null; |
|
75
|
3 |
|
unset($_SESSION['flash']); |
|
76
|
3 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the flash type |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public static function getType() |
|
84
|
|
|
{ |
|
85
|
2 |
|
$data = static::get(); |
|
86
|
2 |
|
return isset($data) ? $data->type : null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the flash message |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public static function getMessage() |
|
95
|
|
|
{ |
|
96
|
2 |
|
$data = static::get(); |
|
97
|
2 |
|
return isset($data) ? $data->message : null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Cast object to string |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public function __toString() |
|
106
|
|
|
{ |
|
107
|
2 |
|
return (string)$this->getMessage(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: