Name migration
This commit is contained in:
Executable
+117
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
namespace RR;
|
||||
|
||||
class Flash{
|
||||
|
||||
private static $_instance;
|
||||
|
||||
// Message types and shortcuts
|
||||
const INFO = 'i';
|
||||
const SUCCESS = 's';
|
||||
const WARNING = 'w';
|
||||
const ERROR = 'e';
|
||||
|
||||
const SESSION_NAME = 'flashes';
|
||||
|
||||
const defaultType = self::INFO;
|
||||
|
||||
protected $msgTypes = [
|
||||
self::ERROR => 'error',
|
||||
self::WARNING => 'warning',
|
||||
self::SUCCESS => 'success',
|
||||
self::INFO => 'info',
|
||||
];
|
||||
|
||||
public $old_messages = Array();
|
||||
public $new_messages = Array();
|
||||
public $_flashes = Array();
|
||||
|
||||
//SINGLETON==============================================
|
||||
|
||||
private function __construct(){
|
||||
if(session_id() == '' || !isset($_SESSION)) {
|
||||
session_start();
|
||||
}
|
||||
if( isset($_SESSION['flashes']) ){
|
||||
$this->old_messages = $_SESSION['flashes'];
|
||||
}
|
||||
unset( $_SESSION[self::SESSION_NAME] );
|
||||
}
|
||||
|
||||
private static function newObj(){
|
||||
if (!isset( self::$_instance )) {
|
||||
self::$_instance = new Flash();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
public static function getInstance(){
|
||||
if (!isset(self::$_instance)) {
|
||||
return self::newObj();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
//=======================================================
|
||||
|
||||
public function merge(){
|
||||
$this->_flashes = array_merge_recursive($this->new_messages, $this->old_messages);
|
||||
}
|
||||
|
||||
public function load_to_session(){
|
||||
$_SESSION[self::SESSION_NAME] = $this->new_messages;
|
||||
}
|
||||
|
||||
public static function add_message($type = self::defaultType, $message){
|
||||
$instance = self::getInstance();
|
||||
$instance->new_messages[$type] = $message;
|
||||
$instance->load_to_session();
|
||||
}
|
||||
|
||||
public static function add_info($message){
|
||||
$instance = self::getInstance();
|
||||
$instance->new_messages[self::INFO] = $message;
|
||||
$instance->load_to_session();
|
||||
}
|
||||
|
||||
public static function add_error($message){
|
||||
$instance = self::getInstance();
|
||||
$instance->new_messages[self::ERROR] = $message;
|
||||
$instance->load_to_session();
|
||||
}
|
||||
|
||||
public static function get_errors(){
|
||||
if(!self::has_errors()){
|
||||
return Array();
|
||||
}
|
||||
return self::getInstance()->_flashes[self::ERROR];
|
||||
}
|
||||
|
||||
public static function get_infos(){
|
||||
if(!self::has_info()){
|
||||
return Array();
|
||||
}
|
||||
return self::getInstance()->_flashes[self::INFO];
|
||||
}
|
||||
|
||||
public static function has_errors(){
|
||||
$instance = self::getInstance();
|
||||
$instance->merge();
|
||||
if( array_key_exists(self::ERROR, $instance->_flashes ) ){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function has_info(){
|
||||
$instance = self::getInstance();
|
||||
$instance->merge();
|
||||
if( array_key_exists(self::INFO, $instance->_flashes ) ){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user