1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Vperyod Session Handler |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category FlashMessenger |
13
|
|
|
* @package Vperyod\SessionHandler |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Vperyod\SessionHandler; |
21
|
|
|
|
22
|
|
|
use Aura\Session\SegmentInterface as Segment; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Messenger |
26
|
|
|
* |
27
|
|
|
* @category FlashMessenger |
28
|
|
|
* @package Vperyod\SessionHandler |
29
|
|
|
* @author Jake Johns <[email protected]> |
30
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
31
|
|
|
* @link https://github.com/vperyod/vperyod.session-handler |
32
|
|
|
*/ |
33
|
|
|
class Messenger |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Default message levels |
37
|
|
|
*/ |
38
|
|
|
const LVL_SUCCESS = 'success'; |
39
|
|
|
const LVL_INFO = 'info'; |
40
|
|
|
const LVL_WARNING = 'warning'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Session Segment |
44
|
|
|
* |
45
|
|
|
* @var Segment |
46
|
|
|
* |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
protected $segment; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a flash messenger |
53
|
|
|
* |
54
|
|
|
* @param Segment $segment session storage |
55
|
|
|
* |
56
|
|
|
* @access public |
57
|
|
|
*/ |
58
|
7 |
|
public function __construct(Segment $segment) |
59
|
|
|
{ |
60
|
7 |
|
$this->segment = $segment; |
61
|
7 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add a message |
65
|
|
|
* |
66
|
|
|
* @param string $message Message text |
67
|
|
|
* @param string $level Message level |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
*/ |
73
|
5 |
|
public function add($message, $level = self::LVL_INFO) |
74
|
|
|
{ |
75
|
5 |
|
$current = $this->segment->getFlash('messages', []); |
76
|
|
|
|
77
|
5 |
|
$current[] = [ |
78
|
5 |
|
'message' => $message, |
79
|
5 |
|
'level' => $level |
80
|
|
|
]; |
81
|
|
|
|
82
|
5 |
|
$this->segment->setFlash('messages', $current); |
83
|
|
|
|
84
|
5 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get current messages |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
* |
92
|
|
|
* @access public |
93
|
|
|
*/ |
94
|
1 |
|
public function getMessages() |
95
|
|
|
{ |
96
|
1 |
|
return $this->segment->getFlash('messages'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set a success message |
101
|
|
|
* |
102
|
|
|
* @param string $message Message text |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
* |
106
|
|
|
* @access public |
107
|
|
|
*/ |
108
|
1 |
|
public function success($message) |
109
|
|
|
{ |
110
|
1 |
|
return $this->add($message, self::LVL_SUCCESS); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set an info message |
115
|
|
|
* |
116
|
|
|
* @param string $message Message text |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
*/ |
122
|
1 |
|
public function info($message) |
123
|
|
|
{ |
124
|
1 |
|
return $this->add($message, self::LVL_INFO); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set a warning message |
129
|
|
|
* |
130
|
|
|
* @param string $message Message text |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
* |
134
|
|
|
* @access public |
135
|
|
|
*/ |
136
|
1 |
|
public function warning($message) |
137
|
|
|
{ |
138
|
1 |
|
return $this->add($message, self::LVL_WARNING); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|