Skip to content

Instantly share code, notes, and snippets.

@agusputra
Last active November 3, 2019 09:41
Show Gist options
  • Select an option

  • Save agusputra/c8be14b1e73886055d81 to your computer and use it in GitHub Desktop.

Select an option

Save agusputra/c8be14b1e73886055d81 to your computer and use it in GitHub Desktop.
Helper.php
===
<?php
class Helper
{
private static $instance;
public static $alert;
public function __construct()
{
self::$alert = new Alert();
}
public static function init()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
}
class Alert
{
public function __call($name, $arguments)
{
global $globalViewAlert;
switch ($name) {
case 'success': {
return $globalViewAlert = ['level' => 'success', 'msg' => $arguments[0]];
}
case 'info': {
return $globalViewAlert = ['level' => 'info', 'msg' => $arguments[0]];
}
case 'warning': {
return $globalViewAlert = ['level' => 'warning', 'msg' => $arguments[0]];
}
case 'error': {
return $globalViewAlert = ['level' => 'error', 'msg' => $arguments[0]];
}
}
}
}
(new Helper())->init();
===
==========
alert.php
===
<?php
if (!function_exists('renderAlert')) {
function renderAlert()
{
global $globalViewAlert;
$alert = $globalViewAlert;
if (isset($alert)) {
if (gettype($alert['msg']) === 'array') {
$msg = "<ul>";
foreach ($alert['msg'] as $m) {
$msg .= "<li>$m</li>";
}
$msg .= "</ul>";
} else {
$msg = $alert['msg'];
}
switch ($alert['level']) {
case 'success':
echo "<div class='alert alert-success'>{$msg}</div>";
break;
case 'info':
echo "<div class='alert alert-info'>{$msg}</div>";
break;
case 'warning':
echo "<div class='alert alert-warning'>{$msg}</div>";
break;
case 'error':
echo "<div class='alert alert-danger'>{$msg}</div>";
break;
}
}
}
}
renderAlert();
===
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment