1 | <?php |
||
10 | class Flash { |
||
11 | /** |
||
12 | * Storage array or object implementing ArrayAccess |
||
13 | * @var array|\ArrayAccess |
||
14 | */ |
||
15 | protected $storage = null; |
||
16 | |||
17 | /** |
||
18 | * Key to store flashed data in storage with |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $storage_key = '__carbonFrameworkFlash'; |
||
23 | |||
24 | /** |
||
25 | * Constructor |
||
26 | * |
||
27 | * @param array|\ArrayAccess $storage |
||
28 | */ |
||
29 | public function __construct( &$storage ) { |
||
30 | if ( $this->isValidStorage( $storage ) ) { |
||
31 | if ( ! isset( $storage[ $this->storage_key ] ) ) { |
||
32 | $storage[ $this->storage_key ] = []; |
||
33 | } |
||
34 | $this->storage = &$storage[ $this->storage_key ]; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Return whether a storage object is valid |
||
40 | * |
||
41 | * @param mixed $storage |
||
42 | * @return boolean |
||
43 | */ |
||
44 | protected function isValidStorage( $storage ) { |
||
47 | |||
48 | /** |
||
49 | * Throw an exception if storage is not valid |
||
50 | * |
||
51 | * @throws Exception |
||
52 | * @return null |
||
53 | */ |
||
54 | protected function validateStorage() { |
||
59 | |||
60 | /** |
||
61 | * Return whether the flash service is enabled |
||
62 | * |
||
63 | * @return boolean |
||
64 | */ |
||
65 | 1 | public function enabled() { |
|
68 | |||
69 | /** |
||
70 | * Get the entire storage or the values for a key |
||
71 | * |
||
72 | * @param string|null $key |
||
73 | * @return array|\ArrayAccess |
||
74 | */ |
||
75 | 7 | public function peek( $key = null ) { |
|
88 | |||
89 | /** |
||
90 | * Get and clear the entire storage or the values for a key |
||
91 | * |
||
92 | * @param string|null $key |
||
93 | * @return array|\ArrayAccess |
||
94 | */ |
||
95 | 1 | public function get( $key = null ) { |
|
102 | |||
103 | /** |
||
104 | * Add values for a key |
||
105 | * |
||
106 | * @param string $key |
||
107 | * @param mixed $new_items |
||
108 | */ |
||
109 | 5 | public function add( $key, $new_items ) { |
|
123 | |||
124 | /** |
||
125 | * Clear the entire storage or the values for a key |
||
126 | * |
||
127 | * @param string|null $key |
||
128 | * @return null |
||
129 | */ |
||
130 | 2 | public function clear( $key = null ) { |
|
141 | } |
||
142 |