|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Leaf; |
| 4 | + |
| 5 | +/** |
| 6 | + * Leaf Flash |
| 7 | + * ----- |
| 8 | + * Simple flash messages for your leaf apps |
| 9 | + * |
| 10 | + * @author Michael Darko <[email protected]> |
| 11 | + * @since 2.5.0 |
| 12 | + */ |
| 13 | +class Flash |
| 14 | +{ |
| 15 | + private static $config = [ |
| 16 | + "key" => "leaf.flash", |
| 17 | + "default" => "message", |
| 18 | + "saved" => "leaf.flash.saved", |
| 19 | + ]; |
| 20 | + |
| 21 | + /** |
| 22 | + * Configure leaf flash |
| 23 | + * |
| 24 | + * @param array $config Configuration for leaf flash |
| 25 | + */ |
| 26 | + public static function config(array $config) |
| 27 | + { |
| 28 | + static::$config = array_merge(static::$config, $config); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Set a new flash message |
| 33 | + * |
| 34 | + * @param string $message The flash message to set |
| 35 | + * @param string $key The key to save message |
| 36 | + */ |
| 37 | + public static function set(string $message, string $key = "default") |
| 38 | + { |
| 39 | + static::session(); |
| 40 | + |
| 41 | + if ($key === "default") { |
| 42 | + $key = static::$config["default"]; |
| 43 | + } |
| 44 | + |
| 45 | + $_SESSION[static::$config["key"]][$key] = $message; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Remove a flash message |
| 50 | + * |
| 51 | + * @param string|null $key The key of message to remove |
| 52 | + */ |
| 53 | + public static function unset(string $key = null) |
| 54 | + { |
| 55 | + static::session(); |
| 56 | + |
| 57 | + if (!$key) { |
| 58 | + Http\Session::unset(static::$config["key"]); |
| 59 | + } else { |
| 60 | + if ($key === "default") { |
| 61 | + $key = static::$config["default"]; |
| 62 | + } |
| 63 | + |
| 64 | + $_SESSION[static::$config["key"]][$key] = null; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the flash array |
| 70 | + * |
| 71 | + * @param string|null $key The key of message to get |
| 72 | + * @return string|array |
| 73 | + */ |
| 74 | + private static function get(string $key = null) |
| 75 | + { |
| 76 | + static::session(); |
| 77 | + |
| 78 | + if (!$key) { |
| 79 | + return Http\Session::get(static::$config["key"]); |
| 80 | + } |
| 81 | + |
| 82 | + if ($key === "default") { |
| 83 | + $key = static::$config["default"]; |
| 84 | + } |
| 85 | + |
| 86 | + $item = null; |
| 87 | + $items = Http\Session::get(static::$config["key"], false); |
| 88 | + |
| 89 | + if (isset($items[$key])) { |
| 90 | + $item = $items[$key]; |
| 91 | + } |
| 92 | + |
| 93 | + if ($key) { |
| 94 | + static::unset($key); |
| 95 | + } |
| 96 | + |
| 97 | + return $item; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Display a flash message |
| 102 | + * |
| 103 | + * @param string $key The key of message to display |
| 104 | + * @return string |
| 105 | + */ |
| 106 | + public static function display(string $key = "default") |
| 107 | + { |
| 108 | + static::session(); |
| 109 | + |
| 110 | + return static::get($key); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Save a flash message (won't delete after view). |
| 115 | + * You can save only one message at a time. |
| 116 | + * |
| 117 | + * @param string $message The flash message to save |
| 118 | + */ |
| 119 | + public static function save(string $message) |
| 120 | + { |
| 121 | + static::session(); |
| 122 | + |
| 123 | + Http\Session::set(static::$config["saved"], $message); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Clear the saved flash message |
| 128 | + */ |
| 129 | + public static function clearSaved() |
| 130 | + { |
| 131 | + static::session(); |
| 132 | + |
| 133 | + Http\Session::set(static::$config["saved"], null); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Display the saved flash message |
| 138 | + */ |
| 139 | + public static function displaySaved() |
| 140 | + { |
| 141 | + static::session(); |
| 142 | + |
| 143 | + return Http\Session::get(static::$config["saved"]); |
| 144 | + } |
| 145 | + |
| 146 | + private static function session() |
| 147 | + { |
| 148 | + if (!session_id()) { |
| 149 | + session_start(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments