Created
September 6, 2017 12:19
-
-
Save JayPHP/3b1b53f59608826c8f69cbc20ce8305b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Flash Messages | |
* | |
* @author James Byrne <jamesbwebdev@gmail.com> | |
*/ | |
namespace Jay\System\Components; | |
use Jay\System\Flash; | |
class FlashMessage implements Flash | |
{ | |
public function __construct() | |
{ | |
session_start(); | |
if (!isset($_SESSION['flash'])) { | |
$_SESSION['flash'] = []; | |
} | |
} | |
public function info($message) | |
{ | |
$this->add($message, 'info'); | |
} | |
public function warning($message) | |
{ | |
$this->add($message, 'warning'); | |
} | |
public function error($message) | |
{ | |
$this->add($message, 'error'); | |
} | |
public function success($message) | |
{ | |
$this->add($message, 'success'); | |
} | |
public function add($message, $type) | |
{ | |
$_SESSION['flash'][$type][] = $message; | |
} | |
public function render() | |
{ | |
if (empty($_SESSION['flash'])) { | |
return false; | |
} | |
$button = '<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button>'; | |
foreach ($_SESSION['flash'] as $type => $messages) { | |
$title = ucfirst($type); | |
foreach ($messages as $message) { | |
echo "<div class='alert alert-dismissible alert-$type' role='alert'> | |
$button | |
<h4>$title!</h4> $message | |
</div>"; | |
} | |
} | |
unset($_SESSION['flash']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment