|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: Inhere |
| 5 | + * Date: 2018/6/1 0001 |
| 6 | + * Time: 21:19 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Toolkit\Web\Util; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class Flash |
| 13 | + * @package Toolkit\Web\Util |
| 14 | + * @method normal(string $key, string $message, array $opts = []) |
| 15 | + * @method info(string $key, string $message, array $opts = []) |
| 16 | + * @method error(string $key, string $message, array $opts = []) |
| 17 | + * @method danger(string $key, string $message, array $opts = []) |
| 18 | + * @method success(string $key, string $message, array $opts = []) |
| 19 | + * @method warning(string $key, string $message, array $opts = []) |
| 20 | + * @method dark(string $key, string $message, array $opts = []) |
| 21 | + */ |
| 22 | +class Flash |
| 23 | +{ |
| 24 | + // use BootstrapStyleMessageTrait; |
| 25 | + |
| 26 | + // alert style |
| 27 | + const LIGHT = 'light'; |
| 28 | + const DARK = 'dark'; |
| 29 | + const INFO = 'info'; |
| 30 | + const SUCCESS = 'success'; |
| 31 | + const PRIMARY = 'primary'; |
| 32 | + const WARN = 'warning'; |
| 33 | + const WARNING = 'warning'; |
| 34 | + const ERROR = 'danger'; |
| 35 | + const DANGER = 'danger'; |
| 36 | + const SECONDARY = 'secondary'; |
| 37 | + |
| 38 | + const SESS_DATA_KEY = '_user_flash_data'; |
| 39 | + |
| 40 | + const FLASH_STYLES = [ |
| 41 | + 'normal' => 'secondary', |
| 42 | + 'default' => 'secondary', |
| 43 | + 'primary' => 'primary', |
| 44 | + 'secondary' => 'secondary', |
| 45 | + 'success' => 'success', |
| 46 | + 'error' => 'danger', |
| 47 | + 'danger' => 'danger', |
| 48 | + 'warning' => 'warning', |
| 49 | + 'info' => 'info', |
| 50 | + 'light' => 'light', |
| 51 | + 'dark' => 'dark' |
| 52 | + ]; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var string |
| 56 | + */ |
| 57 | + private $storageKey = self::SESS_DATA_KEY; |
| 58 | + |
| 59 | + /** |
| 60 | + * Messages from previous request |
| 61 | + * @var string[] |
| 62 | + */ |
| 63 | + protected $previous = []; |
| 64 | + |
| 65 | + /** |
| 66 | + * Message storage |
| 67 | + * @var null|array|\ArrayAccess |
| 68 | + */ |
| 69 | + protected $storage; |
| 70 | + |
| 71 | + /** |
| 72 | + * Flash constructor. |
| 73 | + * @param null|array|\ArrayAccess $storage |
| 74 | + * @throws \RuntimeException |
| 75 | + * @throws \InvalidArgumentException |
| 76 | + */ |
| 77 | + public function __construct(&$storage = null) |
| 78 | + { |
| 79 | + // Set storage |
| 80 | + if (\is_array($storage) || $storage instanceof \ArrayAccess) { |
| 81 | + $this->storage = &$storage; |
| 82 | + } elseif (null === $storage) { |
| 83 | + if ($_SESSION === null) { |
| 84 | + throw new \RuntimeException('Flash messages middleware failed. Session not found.'); |
| 85 | + } |
| 86 | + |
| 87 | + $this->storage = &$_SESSION; |
| 88 | + } else { |
| 89 | + throw new \InvalidArgumentException('Flash messages storage must be an array or implement \ArrayAccess'); |
| 90 | + } |
| 91 | + |
| 92 | + // load previous request messages |
| 93 | + if ($previous = $this->storage[$this->storageKey] ?? null) { |
| 94 | + $this->previous = $previous; |
| 95 | + } |
| 96 | + |
| 97 | + // clear old, init for current request |
| 98 | + $this->storage[$this->storageKey] = []; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * flash message |
| 103 | + * @param string $key |
| 104 | + * @param string $message |
| 105 | + * @param string $style |
| 106 | + * @param array $opts |
| 107 | + * [ |
| 108 | + * 'title' => 'custom title', |
| 109 | + * 'closeable' => 1 |
| 110 | + * ] |
| 111 | + */ |
| 112 | + public function addMessage(string $key, string $message, string $style = 'info', array $opts = []) |
| 113 | + { |
| 114 | + if (!$key || !$message) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $body = \array_merge([ |
| 119 | + 'closeable' => 1, |
| 120 | + ], $opts, [ |
| 121 | + 'type' => $style, |
| 122 | + 'msg' => $message, |
| 123 | + ]); |
| 124 | + |
| 125 | + if (!isset($body['title'])) { |
| 126 | + $body['title'] = \ucfirst($style); |
| 127 | + } |
| 128 | + |
| 129 | + // add message |
| 130 | + $this->storage[$this->storageKey][$key] = \json_encode($body); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * flash old request data. |
| 135 | + * @param string $key |
| 136 | + * @param array $data |
| 137 | + */ |
| 138 | + public function save(string $key, array $data) |
| 139 | + { |
| 140 | + $this->storage[$this->storageKey][$key] = \json_encode($data); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @param string $key |
| 145 | + * @return null|array |
| 146 | + */ |
| 147 | + public function get(string $key) |
| 148 | + { |
| 149 | + if ($json = $this->previous[$key] ?? null) { |
| 150 | + return \json_decode($json, true); |
| 151 | + } |
| 152 | + |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @param string $key |
| 158 | + * @return string |
| 159 | + */ |
| 160 | + public function getMessage(string $key): string |
| 161 | + { |
| 162 | + if ($body = $this->get($key)) { |
| 163 | + return $body['msg'] ?? ''; |
| 164 | + } |
| 165 | + |
| 166 | + return ''; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @param string $name style name |
| 171 | + * @param array $args |
| 172 | + * @return self |
| 173 | + * @throws \InvalidArgumentException |
| 174 | + */ |
| 175 | + public function __call($name, array $args) |
| 176 | + { |
| 177 | + if ($style = self::FLASH_STYLES[$name] ?? null) { |
| 178 | + if (!isset($args[1])) { |
| 179 | + throw new \InvalidArgumentException('missing enough parameters'); |
| 180 | + } |
| 181 | + |
| 182 | + $this->addMessage($args[0], $args[1], $style, isset($args[2]) ? (array)$args[2] : []); |
| 183 | + } |
| 184 | + |
| 185 | + return $this; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @return array |
| 190 | + */ |
| 191 | + public function getMessages(): array |
| 192 | + { |
| 193 | + return $this->storage[$this->storageKey]; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * @return string |
| 198 | + */ |
| 199 | + public function getStorageKey(): string |
| 200 | + { |
| 201 | + return $this->storageKey; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @param string $storageKey |
| 206 | + */ |
| 207 | + public function setStorageKey(string $storageKey) |
| 208 | + { |
| 209 | + $this->storageKey = $storageKey; |
| 210 | + } |
| 211 | +} |
0 commit comments