Flash.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Luracast\Restler;
  3. use Luracast\Restler\Format\HtmlFormat;
  4. use ArrayAccess;
  5. /**
  6. * Storing and retrieving a message or array of key value pairs for one time use using $_SESSION
  7. *
  8. * They are typically used in view templates when using HtmlFormat
  9. *
  10. * @category Framework
  11. * @package Restler
  12. * @author R.Arul Kumaran <arul@luracast.com>
  13. * @copyright 2010 Luracast
  14. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15. * @link http://luracast.com/products/restler/
  16. *
  17. */
  18. class Flash implements ArrayAccess
  19. {
  20. const SUCCESS = 'success';
  21. const INFO = 'info';
  22. const WARNING = 'warning';
  23. const DANGER = 'danger';
  24. /**
  25. * @var Flash
  26. */
  27. private static $instance;
  28. private $usedOnce = false;
  29. /**
  30. * Flash a success message to user
  31. *
  32. * @param string $message
  33. * @param string $header
  34. *
  35. * @return Flash
  36. */
  37. public static function success($message, $header = '')
  38. {
  39. return static::message($message, $header, Flash::SUCCESS);
  40. }
  41. /**
  42. * Flash a info message to user
  43. *
  44. * @param string $message
  45. * @param string $header
  46. *
  47. * @return Flash
  48. */
  49. public static function info($message, $header = '')
  50. {
  51. return static::message($message, $header, Flash::INFO);
  52. }
  53. /**
  54. * Flash a warning message to user
  55. *
  56. * @param string $message
  57. * @param string $header
  58. *
  59. * @return Flash
  60. */
  61. public static function warning($message, $header = '')
  62. {
  63. return static::message($message, $header, Flash::WARNING);
  64. }
  65. /**
  66. * Flash a error message to user
  67. *
  68. * @param string $message
  69. * @param string $header
  70. *
  71. * @return Flash
  72. */
  73. public static function danger($message, $header = '')
  74. {
  75. return static::message($message, $header, Flash::DANGER);
  76. }
  77. /**
  78. * Flash a message to user
  79. *
  80. * @param string $text message text
  81. * @param string $header
  82. * @param string $type
  83. *
  84. * @return Flash
  85. */
  86. public static function message($text, $header = '', $type = Flash::WARNING)
  87. {
  88. return static::set(array('message' => $text, 'header' => $header, 'type' => $type));
  89. }
  90. /**
  91. * Set some data for one time use
  92. *
  93. * @param array $data array of key value pairs {@type associative}
  94. *
  95. * @return Flash
  96. */
  97. public static function set(array $data)
  98. {
  99. if (!static::$instance) {
  100. static::$instance = new Flash();
  101. }
  102. if (!isset($_SESSION['flash'])) {
  103. $_SESSION['flash'] = array();
  104. }
  105. $_SESSION['flash'] += $data;
  106. HtmlFormat::$data['flash'] = static::$instance;
  107. return static::$instance;
  108. }
  109. public function __get($name)
  110. {
  111. $this->usedOnce = true;
  112. return Util::nestedValue($_SESSION, 'flash', $name);
  113. }
  114. public function __isset($name)
  115. {
  116. return !is_null(Util::nestedValue($_SESSION, 'flash', $name));
  117. }
  118. public function __destruct()
  119. {
  120. if ($this->usedOnce) {
  121. unset($_SESSION['flash']);
  122. }
  123. }
  124. /**
  125. * Specify data which should be serialized to JSON
  126. * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
  127. * @return mixed data which can be serialized by <b>json_encode</b>,
  128. * which is a value of any type other than a resource.
  129. */
  130. public function jsonSerialize()
  131. {
  132. $this->usedOnce = true;
  133. return isset($_SESSION['flash'])
  134. ? $_SESSION['flash']
  135. : array();
  136. }
  137. public function offsetExists($offset)
  138. {
  139. return $this->__isset($offset);
  140. }
  141. public function offsetGet($offset)
  142. {
  143. return $this->__get($offset);
  144. }
  145. public function offsetSet($offset, $value)
  146. {
  147. //not implemented
  148. }
  149. public function offsetUnset($offset)
  150. {
  151. //not implemented
  152. }
  153. }