summaryrefslogtreecommitdiffstats
path: root/src/Controller/Session/Flash.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Controller/Session/Flash.php')
-rw-r--r--src/Controller/Session/Flash.php129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/Controller/Session/Flash.php b/src/Controller/Session/Flash.php
new file mode 100644
index 0000000..be72e7c
--- /dev/null
+++ b/src/Controller/Session/Flash.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace Jasny\Controller\Session;
+
+/**
+ * Class for the flash message
+ */
+class Flash
+{
+ /**
+ * @var array
+ */
+ protected $data;
+
+ /**
+ * @var array|\ArrayObject
+ */
+ protected $session;
+
+ /**
+ * Session key for flash
+ * @var string
+ */
+ protected $key = 'flash';
+
+
+ /**
+ * Class constructor
+ *
+ * @param array|\ArrayObject $session
+ */
+ public function __construct(&$session)
+ {
+ $this->session =& $session;
+ }
+
+ /**
+ * Check if the flash is set.
+ *
+ * @return boolean
+ */
+ public function isIssued()
+ {
+ return isset($this->session[$this->key]);
+ }
+
+ /**
+ * Set the flash.
+ *
+ * @param string $type flash type, eg. 'error', 'notice' or 'success'
+ * @param mixed $message flash message
+ */
+ public function set($type, $message)
+ {
+ if (!$type) {
+ throw new \InvalidArgumentException("Type should not be empty");
+ }
+
+ $this->session[$this->key] = compact('type', 'message');
+ }
+
+ /**
+ * Get the flash.
+ *
+ * @return object
+ */
+ public function get()
+ {
+ if (!isset($this->data) && isset($this->session[$this->key])) {
+ $this->data = $this->session[$this->key];
+ unset($this->session[$this->key]);
+ }
+
+ return (object)$this->data;
+ }
+
+ /**
+ * Reissue the flash.
+ */
+ public function reissue()
+ {
+ if (!isset($this->data) && isset($this->session[$this->key])) {
+ $this->data = $this->session[$this->key];
+ } else {
+ $this->session[$this->key] = $this->data;
+ }
+ }
+
+ /**
+ * Clear the flash.
+ */
+ public function clear()
+ {
+ $this->data = null;
+ unset($this->session[$this->key]);
+ }
+
+ /**
+ * Get the flash type
+ *
+ * @return string
+ */
+ public function getType()
+ {
+ $data = $this->get();
+ return isset($data) ? $data->type : null;
+ }
+
+ /**
+ * Get the flash message
+ *
+ * @return string
+ */
+ public function getMessage()
+ {
+ $data = $this->get();
+ return isset($data) ? $data->message : null;
+ }
+
+ /**
+ * Cast object to string
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return (string)$this->getMessage();
+ }
+}