|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\flash; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Controller; |
|
6
|
|
|
use Ubiquity\events\EventsManager; |
|
7
|
|
|
use Ubiquity\events\ViewEvents; |
|
8
|
|
|
use Ubiquity\utils\http\USession; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Bag for Session Flash messages |
|
12
|
|
|
* Ubiquity\utils\flash$FlashBag |
|
13
|
|
|
* This class is part of Ubiquity |
|
14
|
|
|
* |
|
15
|
|
|
* @author jcheron <[email protected]> |
|
16
|
|
|
* @version 1.0.2 |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
class FlashBag implements \Iterator { |
|
20
|
|
|
const FLASH_BAG_KEY = '_flash_bag'; |
|
21
|
7 |
|
const VAR_VIEW_NAME='flashMessages'; |
|
22
|
7 |
|
private array $array; |
|
23
|
7 |
|
private int $position = 0; |
|
24
|
|
|
private bool $autoClear; |
|
25
|
|
|
|
|
26
|
7 |
|
public function __construct(bool $autoClear=true) { |
|
27
|
7 |
|
$this->array = USession::get ( self::FLASH_BAG_KEY, [ ] ); |
|
28
|
|
|
$this->autoClear=$autoClear; |
|
29
|
|
|
EventsManager::addListener(ViewEvents::BEFORE_RENDER,function($_, &$data) { |
|
30
|
1 |
|
$data[self::VAR_VIEW_NAME]=$this->array; |
|
31
|
1 |
|
}); |
|
32
|
1 |
|
EventsManager::addListener(ViewEvents::AFTER_RENDER,function(){ |
|
33
|
1 |
|
if($this->autoClear) { |
|
34
|
1 |
|
$this->clear(); |
|
35
|
|
|
} |
|
36
|
1 |
|
}); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
/** |
|
40
|
5 |
|
* Adds a temporary new message to the bag. |
|
41
|
|
|
* @param string $content |
|
42
|
|
|
* @param string|null $title |
|
43
|
1 |
|
* @param string $type |
|
44
|
1 |
|
* @param string|null $icon |
|
45
|
1 |
|
*/ |
|
46
|
|
|
public function addMessage(string $content, string $title = NULL, string $type = 'info', string $icon = null): void { |
|
47
|
|
|
$this->array [] = new FlashMessage ( $content, $title, $type, $icon ); |
|
48
|
1 |
|
} |
|
49
|
1 |
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Adds and saves a message in the bag. |
|
52
|
|
|
* @param string $content |
|
53
|
|
|
* @param string|null $title |
|
54
|
|
|
* @param string $type |
|
55
|
|
|
* @param string|null $icon |
|
56
|
2 |
|
*/ |
|
57
|
2 |
|
public function addMessageAndSave(string $content, string $title = NULL, string $type = 'info', string $icon = null): void { |
|
58
|
|
|
$this->addMessage($content,$title,$type,$icon); |
|
59
|
|
|
USession::set ( self::FLASH_BAG_KEY, $this->array ); |
|
60
|
1 |
|
} |
|
61
|
1 |
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns all the message of a type in the bag. |
|
64
|
2 |
|
* @param string $type |
|
65
|
2 |
|
* @return FlashMessage[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getMessages(string $type): array { |
|
68
|
1 |
|
$result = [ ]; |
|
69
|
1 |
|
foreach ( $this->array as $msg ) { |
|
70
|
|
|
if ($msg->getType () == $type) { |
|
71
|
|
|
$result [] = $msg; |
|
72
|
1 |
|
} |
|
73
|
1 |
|
} |
|
74
|
|
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns all the messages. |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getAll(): array { |
|
82
|
|
|
return $this->array; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Clears the bag. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function clear(): void { |
|
89
|
|
|
$this->array = [ ]; |
|
90
|
|
|
USession::delete ( self::FLASH_BAG_KEY ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function rewind(): void { |
|
94
|
|
|
$this->position = 0; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* |
|
99
|
|
|
* @return FlashMessage |
|
100
|
|
|
*/ |
|
101
|
|
|
public function current(): FlashMessage { |
|
102
|
|
|
return $this->array [$this->position]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function key(): int { |
|
106
|
|
|
return $this->position; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function next(): void { |
|
110
|
|
|
++ $this->position; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function valid(): bool { |
|
114
|
|
|
return isset ( $this->array [$this->position] ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function save(): void { |
|
118
|
|
|
$this->array = USession::set ( self::FLASH_BAG_KEY, $this->array ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isAutoClear(): bool { |
|
125
|
|
|
return $this->autoClear; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param bool $autoClear |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setAutoClear(bool $autoClear): void { |
|
132
|
|
|
$this->autoClear = $autoClear; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|