| 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\Input; |
| 11 |
|
| 12 |
use WPEmerge\Flash\Flash; |
| 13 |
use WPEmerge\Support\Arr; |
| 14 |
|
| 15 |
/** |
| 16 |
* Provide a way to get values from the previous request. |
| 17 |
*/ |
| 18 |
class OldInput { |
| 19 |
/** |
| 20 |
* Flash service. |
| 21 |
* |
| 22 |
* @var Flash |
| 23 |
*/ |
| 24 |
protected $flash = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Key to store the flashed data with. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $flash_key = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @codeCoverageIgnore |
| 37 |
* @param Flash $flash |
| 38 |
* @param string $flash_key |
| 39 |
*/ |
| 40 |
public function __construct( Flash $flash, $flash_key = '__wpemergeOldInput' ) { |
| 41 |
$this->flash = $flash; |
| 42 |
$this->flash_key = $flash_key; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get whether the old input service is enabled. |
| 47 |
* |
| 48 |
* @return boolean |
| 49 |
*/ |
| 50 |
public function enabled() { |
| 51 |
return $this->flash->enabled(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get request value for key from the previous request. |
| 56 |
* |
| 57 |
* @param string $key |
| 58 |
* @param mixed $default |
| 59 |
* @return mixed |
| 60 |
*/ |
| 61 |
public function get( $key, $default = null ) { |
| 62 |
return Arr::get( $this->flash->get( $this->flash_key, [] ), $key, $default ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Set input for the next request. |
| 67 |
* |
| 68 |
* @param array $input |
| 69 |
*/ |
| 70 |
public function set( $input ) { |
| 71 |
$this->flash->add( $this->flash_key, $input ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Clear input for the next request. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function clear() { |
| 80 |
$this->flash->clear( $this->flash_key ); |
| 81 |
} |
| 82 |
} |
| 83 |
|