hamburgscleanest /
data-tables
| 1 | <?php |
||
| 2 | |||
| 3 | namespace hamburgscleanest\DataTables\Helpers; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Class SessionHelper |
||
| 7 | * @package hamburgscleanest\DataTables\Helpers |
||
| 8 | */ |
||
| 9 | class SessionHelper { |
||
| 10 | |||
| 11 | const SESSION_STORAGE = 'data-tables.'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Save the state for the given key. |
||
| 15 | * |
||
| 16 | * @param string $key |
||
| 17 | * @param mixed $sessionValue |
||
| 18 | * @throws \RuntimeException |
||
| 19 | */ |
||
| 20 | 2 | public function saveState(string $key, $sessionValue) : void |
|
| 21 | { |
||
| 22 | 2 | \request()->session()->put($this->_getFormattedKey($key), $sessionValue); |
|
| 23 | 2 | } |
|
| 24 | |||
| 25 | /** |
||
| 26 | * @param string $key |
||
| 27 | * @return string |
||
| 28 | */ |
||
| 29 | 3 | private function _getFormattedKey(string $key) : string |
|
| 30 | { |
||
| 31 | return |
||
| 32 | 3 | self::SESSION_STORAGE . |
|
| 33 | 3 | \preg_replace( |
|
| 34 | 3 | '/\.|\//', |
|
| 35 | 3 | '_', |
|
| 36 | 3 | \preg_replace('/(http|https):\/\//', '', \request()->url()) |
|
| 37 | ) . |
||
| 38 | 3 | '.' . $key; |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get the state for the given key. |
||
| 43 | * |
||
| 44 | * @param string $key |
||
| 45 | * @param null $default |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 46 | * @return mixed |
||
| 47 | * @throws \RuntimeException |
||
| 48 | */ |
||
| 49 | 3 | public function getState(string $key, $default = null) |
|
| 50 | { |
||
| 51 | 3 | return \request()->session()->get($this->_getFormattedKey($key)) ?? $default; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Remove the state for the given key. |
||
| 56 | * |
||
| 57 | * @param string $key |
||
| 58 | * @throws \RuntimeException |
||
| 59 | */ |
||
| 60 | 1 | public function removeState(string $key) : void |
|
| 61 | { |
||
| 62 | 1 | \request()->session()->remove($this->_getFormattedKey($key)); |
|
| 63 | } |
||
| 64 | } |