1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2017-2019 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\Flash; |
11
|
|
|
|
12
|
|
|
use ArrayAccess; |
13
|
|
|
use WPEmerge\Exceptions\ConfigurationException; |
14
|
|
|
use WPEmerge\Helpers\MixedType; |
15
|
|
|
use WPEmerge\Support\Arr; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provide a way to flash data into the session for the next request. |
19
|
|
|
*/ |
20
|
|
|
class Flash { |
21
|
|
|
/** |
22
|
|
|
* Keys for different request contexts. |
23
|
|
|
*/ |
24
|
|
|
const CURRENT_KEY = 'current'; |
25
|
|
|
const NEXT_KEY = 'next'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Key to store flashed data in store with. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $store_key = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Root store array or object implementing ArrayAccess. |
36
|
|
|
* |
37
|
|
|
* @var array|ArrayAccess |
38
|
|
|
*/ |
39
|
|
|
protected $store = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Flash store array. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $flashed = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @codeCoverageIgnore |
52
|
|
|
* @param array|ArrayAccess $store |
53
|
|
|
* @param string $store_key |
54
|
|
|
*/ |
55
|
|
|
public function __construct( &$store, $store_key = '__wpemergeFlash' ) { |
56
|
|
|
$this->store_key = $store_key; |
57
|
|
|
$this->setStore( $store ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get whether a store object is valid. |
62
|
|
|
* |
63
|
|
|
* @param mixed $store |
64
|
|
|
* @return boolean |
65
|
|
|
*/ |
66
|
2 |
|
protected function isValidStore( $store ) { |
67
|
2 |
|
return ( is_array( $store ) || $store instanceof ArrayAccess ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Throw an exception if store is not valid. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
6 |
|
protected function validateStore() { |
76
|
6 |
|
if ( ! $this->isValidStore( $this->store ) ) { |
77
|
5 |
|
throw new ConfigurationException( |
78
|
|
|
'Attempted to use Flash without an active session. ' . |
79
|
5 |
|
'Did you miss to call session_start()?' |
80
|
|
|
); |
81
|
|
|
} |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the store for flash messages. |
86
|
|
|
* |
87
|
|
|
* @return array|ArrayAccess |
88
|
|
|
*/ |
89
|
1 |
|
public function getStore() { |
90
|
1 |
|
return $this->store; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the store for flash messages. |
95
|
|
|
* |
96
|
|
|
* @param array|ArrayAccess $store |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
2 |
|
public function setStore( &$store ) { |
100
|
2 |
|
if ( ! $this->isValidStore( $store ) ) { |
101
|
1 |
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
$this->store = &$store; |
105
|
|
|
|
106
|
1 |
|
if ( ! isset( $this->store[ $this->store_key ] ) ) { |
107
|
1 |
|
$this->store[ $this->store_key ] = [ |
108
|
1 |
|
static::CURRENT_KEY => [], |
109
|
1 |
|
static::NEXT_KEY => [], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
$this->flashed = $store[ $this->store_key ]; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get whether the flash service is enabled. |
118
|
|
|
* |
119
|
|
|
* @return boolean |
120
|
|
|
*/ |
121
|
1 |
|
public function enabled() { |
122
|
1 |
|
return $this->isValidStore( $this->store ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the entire store or the values for a key for a request. |
127
|
|
|
* |
128
|
|
|
* @param string $request_key |
129
|
|
|
* @param string|null $key |
130
|
|
|
* @param mixed $default |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
3 |
|
protected function getFromRequest( $request_key, $key = null, $default = [] ) { |
134
|
3 |
|
$this->validateStore(); |
135
|
|
|
|
136
|
2 |
|
if ( $key === null ) { |
137
|
2 |
|
return Arr::get( $this->flashed, $request_key, $default ); |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return Arr::get( $this->flashed[ $request_key ], $key, $default ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Add values for a key for a request. |
145
|
|
|
* |
146
|
|
|
* @param string $request_key |
147
|
|
|
* @param string $key |
148
|
|
|
* @param mixed $new_items |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
3 |
|
protected function addToRequest( $request_key, $key, $new_items ) { |
152
|
3 |
|
$this->validateStore(); |
153
|
|
|
|
154
|
2 |
|
$new_items = MixedType::toArray( $new_items ); |
155
|
2 |
|
$items = MixedType::toArray( $this->getFromRequest( $request_key, $key, [] ) ); |
156
|
2 |
|
$this->flashed[ $request_key ][ $key ] = array_merge( $items, $new_items ); |
157
|
2 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Remove all values or values for a key from a request. |
161
|
|
|
* |
162
|
|
|
* @param string $request_key |
163
|
|
|
* @param string|null $key |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
3 |
|
protected function clearFromRequest( $request_key, $key = null ) { |
167
|
3 |
|
$this->validateStore(); |
168
|
|
|
|
169
|
2 |
|
$keys = $key === null ? array_keys( $this->flashed[ $request_key ] ) : [$key]; |
170
|
2 |
|
foreach ( $keys as $k ) { |
171
|
2 |
|
unset( $this->flashed[ $request_key ][ $k ] ); |
172
|
|
|
} |
173
|
2 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Add values for a key for the next request. |
177
|
|
|
* |
178
|
|
|
* @param string $key |
179
|
|
|
* @param mixed $new_items |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
1 |
|
public function add( $key, $new_items ) { |
183
|
1 |
|
$this->addToRequest( static::NEXT_KEY, $key, $new_items ); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Add values for a key for the current request. |
188
|
|
|
* |
189
|
|
|
* @param string $key |
190
|
|
|
* @param mixed $new_items |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
1 |
|
public function addNow( $key, $new_items ) { |
194
|
1 |
|
$this->addToRequest( static::CURRENT_KEY, $key, $new_items ); |
195
|
1 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the entire store or the values for a key for the current request. |
199
|
|
|
* |
200
|
|
|
* @param string|null $key |
201
|
|
|
* @param mixed $default |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
1 |
|
public function get( $key = null, $default = [] ) { |
205
|
1 |
|
return $this->getFromRequest( static::CURRENT_KEY, $key, $default ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the entire store or the values for a key for the next request. |
210
|
|
|
* |
211
|
|
|
* @param string|null $key |
212
|
|
|
* @param mixed $default |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
1 |
|
public function getNext( $key = null, $default = [] ) { |
216
|
1 |
|
return $this->getFromRequest( static::NEXT_KEY, $key, $default ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Clear the entire store or the values for a key for the current request. |
221
|
|
|
* |
222
|
|
|
* @param string|null $key |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
1 |
|
public function clear( $key = null ) { |
226
|
1 |
|
$this->clearFromRequest( static::CURRENT_KEY, $key ); |
227
|
1 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Clear the entire store or the values for a key for the next request. |
231
|
|
|
* |
232
|
|
|
* @param string|null $key |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
1 |
|
public function clearNext( $key = null ) { |
236
|
1 |
|
$this->clearFromRequest( static::NEXT_KEY, $key ); |
237
|
1 |
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Shift current store and replace it with next store. |
241
|
|
|
* |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
2 |
|
public function shift() { |
245
|
2 |
|
$this->validateStore(); |
246
|
|
|
|
247
|
1 |
|
$this->flashed[ static::CURRENT_KEY ] = $this->flashed[ static::NEXT_KEY ]; |
248
|
1 |
|
$this->flashed[ static::NEXT_KEY ] = []; |
249
|
1 |
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Save flashed data to store. |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
2 |
|
public function save() { |
257
|
2 |
|
$this->validateStore(); |
258
|
|
|
|
259
|
1 |
|
$this->store[ $this->store_key ] = $this->flashed; |
260
|
1 |
|
} |
261
|
|
|
} |
262
|
|
|
|