summaryrefslogtreecommitdiffstats
path: root/src/Controller/Session/Flash.php
blob: be72e7c1940d6d7f13a040891d0317d19de72de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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();
    }
}