Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
FlashBag.php
Go to the documentation of this file.
1<?php
2
4
9
19class FlashBag implements \Iterator {
20 const FLASH_BAG_KEY = '_flash_bag';
21 const VAR_VIEW_NAME='flashMessages';
22 private array $array;
23 private int $position = 0;
24 private bool $autoClear;
25
26 public function __construct(bool $autoClear=true) {
27 $this->array = USession::get ( self::FLASH_BAG_KEY, [ ] );
28 $this->autoClear=$autoClear;
29 EventsManager::addListener(ViewEvents::BEFORE_RENDER,function($_, &$data) {
31 });
32 EventsManager::addListener(ViewEvents::AFTER_RENDER,function(){
33 if($this->autoClear) {
34 $this->clear();
35 }
36 });
37 }
38
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 }
49
57 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 }
61
67 public function getMessages(string $type): array {
68 $result = [ ];
69 foreach ( $this->array as $msg ) {
70 if ($msg->getType () == $type) {
71 $result [] = $msg;
72 }
73 }
74 return $result;
75 }
76
81 public function getAll(): array {
82 return $this->array;
83 }
84
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
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
124 public function isAutoClear(): bool {
125 return $this->autoClear;
126 }
127
131 public function setAutoClear(bool $autoClear): void {
132 $this->autoClear = $autoClear;
133 }
134}
135
Base class for controllers.
View events constants.
Bag for Session Flash messages Ubiquity\utils\flash$FlashBag This class is part of Ubiquity.
Definition FlashBag.php:19
addMessage(string $content, string $title=NULL, string $type='info', string $icon=null)
Adds a temporary new message to the bag.
Definition FlashBag.php:46
setAutoClear(bool $autoClear)
Definition FlashBag.php:131
getMessages(string $type)
Returns all the message of a type in the bag.
Definition FlashBag.php:67
getAll()
Returns all the messages.
Definition FlashBag.php:81
addMessageAndSave(string $content, string $title=NULL, string $type='info', string $icon=null)
Adds and saves a message in the bag.
Definition FlashBag.php:57
__construct(bool $autoClear=true)
Definition FlashBag.php:26
Http Session utilities This class is part of Ubiquity.
Definition USession.php:16