* @copyright 2010 Luracast * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link http://luracast.com/products/restler/ * @version 3.0.0rc5 */ class Flash //implements \JsonSerializable { const SUCCESS = 'success'; const INFO = 'info'; const WARNING = 'warning'; const DANGER = 'danger'; /** * @var Flash */ private static $instance; private $usedOnce = false; /** * Flash a success message to user * * @param string $message * @param string $header * * @return Flash */ public static function success($message, $header = '') { return static::message($message, $header, Flash::SUCCESS); } /** * Flash a info message to user * * @param string $message * @param string $header * * @return Flash */ public static function info($message, $header = '') { return static::message($message, $header, Flash::INFO); } /** * Flash a warning message to user * * @param string $message * @param string $header * * @return Flash */ public static function warning($message, $header = '') { return static::message($message, $header, Flash::WARNING); } /** * Flash a error message to user * * @param string $message * @param string $header * * @return Flash */ public static function danger($message, $header = '') { return static::message($message, $header, Flash::DANGER); } /** * Flash a message to user * * @param string $text message text * @param string $header * @param string $type * * @return Flash */ public static function message($text, $header = '', $type = Flash::WARNING) { return static::set(array('message' => $text, 'header' => $header, 'type' => $type)); } /** * Set some data for one time use * * @param array $data array of key value pairs {@type associative} * * @return Flash */ public static function set(array $data) { if (!static::$instance) static::$instance = new Flash(); if (!isset($_SESSION['flash'])) $_SESSION['flash'] = array(); $_SESSION['flash'] += $data; HtmlFormat::$data['flash'] = static::$instance; return static::$instance; } public function __get($name) { $this->usedOnce = true; return Util::nestedValue($_SESSION, 'flash', $name); } public function __isset($name) { return !is_null(Util::nestedValue($_SESSION, 'flash', $name)); } public function __destruct() { if ($this->usedOnce) unset($_SESSION['flash']); } /** * Specify data which should be serialized to JSON * @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @return mixed data which can be serialized by json_encode, * which is a value of any type other than a resource. */ public function jsonSerialize() { $this->usedOnce = true; return isset($_SESSION['flash']) ? $_SESSION['flash'] : array(); } }