| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2019 Atanas Angelov |
| 6 |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 |
* @link https://wpemerge.com/ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEmerge\Flash; |
| 11 |
|
| 12 |
use ArrayAccess; |
| 13 |
use WPEmerge\Exceptions\ConfigurationException; |
| 14 |
use WPEmerge\Helpers\MixedType; |
| 15 |
use WPEmerge\Support\Arr; |
| 16 |
|
| 17 |
/** |
| 18 |
* Provide a way to flash data into the session for the next request. |
| 19 |
*/ |
| 20 |
class Flash { |
| 21 |
/** |
| 22 |
* Keys for different request contexts. |
| 23 |
*/ |
| 24 |
const CURRENT_KEY = 'current'; |
| 25 |
const NEXT_KEY = 'next'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Key to store flashed data in store with. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $store_key = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Root store array or object implementing ArrayAccess. |
| 36 |
* |
| 37 |
* @var array|ArrayAccess |
| 38 |
*/ |
| 39 |
protected $store = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Flash store array. |
| 43 |
* |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
protected $flashed = []; |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructor. |
| 50 |
* |
| 51 |
* @codeCoverageIgnore |
| 52 |
* @param array|ArrayAccess $store |
| 53 |
* @param string $store_key |
| 54 |
*/ |
| 55 |
public function __construct( &$store, $store_key = '__wpemergeFlash' ) { |
| 56 |
$this->store_key = $store_key; |
| 57 |
$this->setStore( $store ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get whether a store object is valid. |
| 62 |
* |
| 63 |
* @param mixed $store |
| 64 |
* @return boolean |
| 65 |
*/ |
| 66 |
protected function isValidStore( $store ) { |
| 67 |
return ( is_array( $store ) || $store instanceof ArrayAccess ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Throw an exception if store is not valid. |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
protected function validateStore() { |
| 76 |
if ( ! $this->isValidStore( $this->store ) ) { |
| 77 |
throw new ConfigurationException( |
| 78 |
'Attempted to use Flash without an active session. ' . |
| 79 |
'Did you miss to call session_start()?' |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the store for flash messages. |
| 86 |
* |
| 87 |
* @return array|ArrayAccess |
| 88 |
*/ |
| 89 |
public function getStore() { |
| 90 |
return $this->store; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Set the store for flash messages. |
| 95 |
* |
| 96 |
* @param array|ArrayAccess $store |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function setStore( &$store ) { |
| 100 |
if ( ! $this->isValidStore( $store ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$this->store = &$store; |
| 105 |
|
| 106 |
if ( ! isset( $this->store[ $this->store_key ] ) ) { |
| 107 |
$this->store[ $this->store_key ] = [ |
| 108 |
static::CURRENT_KEY => [], |
| 109 |
static::NEXT_KEY => [], |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
$this->flashed = $store[ $this->store_key ]; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get whether the flash service is enabled. |
| 118 |
* |
| 119 |
* @return boolean |
| 120 |
*/ |
| 121 |
public function enabled() { |
| 122 |
return $this->isValidStore( $this->store ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get the entire store or the values for a key for a request. |
| 127 |
* |
| 128 |
* @param string $request_key |
| 129 |
* @param string|null $key |
| 130 |
* @param mixed $default |
| 131 |
* @return mixed |
| 132 |
*/ |
| 133 |
protected function getFromRequest( $request_key, $key = null, $default = [] ) { |
| 134 |
$this->validateStore(); |
| 135 |
|
| 136 |
if ( $key === null ) { |
| 137 |
return Arr::get( $this->flashed, $request_key, $default ); |
| 138 |
} |
| 139 |
|
| 140 |
return Arr::get( $this->flashed[ $request_key ], $key, $default ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Add values for a key for a request. |
| 145 |
* |
| 146 |
* @param string $request_key |
| 147 |
* @param string $key |
| 148 |
* @param mixed $new_items |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
protected function addToRequest( $request_key, $key, $new_items ) { |
| 152 |
$this->validateStore(); |
| 153 |
|
| 154 |
$new_items = MixedType::toArray( $new_items ); |
| 155 |
$items = MixedType::toArray( $this->getFromRequest( $request_key, $key, [] ) ); |
| 156 |
$this->flashed[ $request_key ][ $key ] = array_merge( $items, $new_items ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Remove all values or values for a key from a request. |
| 161 |
* |
| 162 |
* @param string $request_key |
| 163 |
* @param string|null $key |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
protected function clearFromRequest( $request_key, $key = null ) { |
| 167 |
$this->validateStore(); |
| 168 |
|
| 169 |
$keys = $key === null ? array_keys( $this->flashed[ $request_key ] ) : [$key]; |
| 170 |
foreach ( $keys as $k ) { |
| 171 |
unset( $this->flashed[ $request_key ][ $k ] ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Add values for a key for the next request. |
| 177 |
* |
| 178 |
* @param string $key |
| 179 |
* @param mixed $new_items |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public function add( $key, $new_items ) { |
| 183 |
$this->addToRequest( static::NEXT_KEY, $key, $new_items ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Add values for a key for the current request. |
| 188 |
* |
| 189 |
* @param string $key |
| 190 |
* @param mixed $new_items |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public function addNow( $key, $new_items ) { |
| 194 |
$this->addToRequest( static::CURRENT_KEY, $key, $new_items ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get the entire store or the values for a key for the current request. |
| 199 |
* |
| 200 |
* @param string|null $key |
| 201 |
* @param mixed $default |
| 202 |
* @return mixed |
| 203 |
*/ |
| 204 |
public function get( $key = null, $default = [] ) { |
| 205 |
return $this->getFromRequest( static::CURRENT_KEY, $key, $default ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get the entire store or the values for a key for the next request. |
| 210 |
* |
| 211 |
* @param string|null $key |
| 212 |
* @param mixed $default |
| 213 |
* @return mixed |
| 214 |
*/ |
| 215 |
public function getNext( $key = null, $default = [] ) { |
| 216 |
return $this->getFromRequest( static::NEXT_KEY, $key, $default ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Clear the entire store or the values for a key for the current request. |
| 221 |
* |
| 222 |
* @param string|null $key |
| 223 |
* @return void |
| 224 |
*/ |
| 225 |
public function clear( $key = null ) { |
| 226 |
$this->clearFromRequest( static::CURRENT_KEY, $key ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Clear the entire store or the values for a key for the next request. |
| 231 |
* |
| 232 |
* @param string|null $key |
| 233 |
* @return void |
| 234 |
*/ |
| 235 |
public function clearNext( $key = null ) { |
| 236 |
$this->clearFromRequest( static::NEXT_KEY, $key ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Shift current store and replace it with next store. |
| 241 |
* |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
public function shift() { |
| 245 |
$this->validateStore(); |
| 246 |
|
| 247 |
$this->flashed[ static::CURRENT_KEY ] = $this->flashed[ static::NEXT_KEY ]; |
| 248 |
$this->flashed[ static::NEXT_KEY ] = []; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Save flashed data to store. |
| 253 |
* |
| 254 |
* @return void |
| 255 |
*/ |
| 256 |
public function save() { |
| 257 |
$this->validateStore(); |
| 258 |
|
| 259 |
$this->store[ $this->store_key ] = $this->flashed; |
| 260 |
} |
| 261 |
} |
| 262 |
|