OldInput::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 2
c 0
b 0
f 0
cc 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
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 1
	public function enabled() {
51 1
		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 1
	public function get( $key, $default = null ) {
62 1
		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 1
	public function set( $input ) {
71 1
		$this->flash->add( $this->flash_key, $input );
72 1
	}
73
74
	/**
75
	 * Clear input for the next request.
76
	 *
77
	 * @return void
78
	 */
79 1
	public function clear() {
80 1
		$this->flash->clear( $this->flash_key );
81 1
	}
82
}
83