-
Notifications
You must be signed in to change notification settings - Fork 2
/
flash.php
executable file
·105 lines (93 loc) · 2.04 KB
/
flash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Flash - A Flash Messages Plugin for the PHP Fat-Free Framework
*
* The contents of this file are subject to the terms of the GNU General
* Public License Version 3.0. You may not use this file except in
* compliance with the license. Any of the license terms and conditions
* can be waived if you get permission from the copyright holder.
*
* Copyright (c) 2020 ~ ikkez
* Christian Knuth <ikkez0n3@gmail.com>
* https://github.com/ikkez/F3-Sugar/
*
* @version 1.0.2
* @date: 13.12.2020
* @since: 21.01.2015
**/
class Flash extends \Prefab {
/** @var \Base */
protected $f3;
/** @var array */
protected $msg;
/** @var array */
protected $key;
/**
* Flash constructor.
* @param string $key
*/
public function __construct($key='flash') {
$this->f3 = \Base::instance();
$this->msg = &$this->f3->ref('SESSION.'.$key.'.msg');
$this->key = &$this->f3->ref('SESSION.'.$key.'.key');
}
/**
* add a message to the stack
* @param $text
* @param string $status
*/
public function addMessage($text,$status='info') {
$this->msg[] = ['text'=>$text,'status'=>$status];
}
/**
* dump all messages and clear them
* @return array
*/
public function getMessages() {
$out = $this->msg;
$this->clearMessages();
return $out ?: [];
}
/**
* reset message stack
*/
public function clearMessages() {
$this->msg = [];
}
/**
* check if there messages in the stack
* @return bool
*/
public function hasMessages() {
return !empty($this->msg);
}
/**
* set a flash key
* @param $key
* @param bool $val
*/
public function setKey($key,$val=TRUE) {
$this->key[$key] = $val;
}
/**
* get and clear a flash key, if it's existing
* @param $key
* @return mixed|null
*/
public function getKey($key) {
$out = NULL;
if ($this->hasKey($key)) {
$out = $this->key[$key];
unset($this->key[$key]);
}
return $out;
}
/**
* check if there's a flash key existing
* @param $key
* @return bool
*/
public function hasKey($key) {
return ($this->key && array_key_exists($key, $this->key));
}
}