Passed
Push — master ( fca16e...a09e23 )
by Alexander
04:10
created

Inputs::get()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 6
nc 4
nop 2
dl 0
loc 13
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2023 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Components\Http\Loaders;
24
25
use Syscodes\Components\Core\Http\Exceptions\BadRequestHttpException;
26
27
/**
28
 * Inputs is a container for user input values such as 
29
 * $_GET, $_POST, $_REQUEST, and $_COOKIE.
30
 */
31
final class Inputs extends Parameters
32
{
33
	/**
34
	 * Replaces the current parameters.
35
	 * 
36
	 * @param  array  $parameters
37
	 * 
38
	 * @return void
39
	 */
40
	public function replace(array $inputs = []): void
41
	{
42
		$this->parameters = [];
43
		$this->add($inputs);
44
	}
45
	
46
	/**
47
	 * Adds parameters.
48
	 * 
49
	 * @param  array  $parameters
50
	 * 
51
	 * @return void
52
	 */
53
	public function add(array $inputs = []): void
54
	{
55
		foreach ($inputs as $input => $file) {
56
			$this->set($input, $file);
57
		}
58
	}
59
	
60
	/**
61
	 * Gets a string input value by name.
62
	 * 
63
	 * @param  string  $key
64
	 * @param  mixed  $default  
65
	 * 
66
	 * @return string|int|float|bool|null
67
	 */
68
	public function get(string $key, mixed $default = null): string|int|float|bool|null
69
	{
70
		if (null !== $default && ! is_scalar($default) && ! method_exists($default, '__toString')) {
71
			throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__));
72
		}
73
		
74
		$value = parent::get($key, $this);
75
		
76
		if (null !== $value && $this !== $value && ! is_scalar($value) && ! method_exists($value, '__toString')) {
77
			throw new BadRequestHttpException(sprintf('Retrieving a non-string value from "%s()" is deprecated, and will throw a exception in Syscodes, use "%s::all($key)" instead', __METHOD__, __CLASS__));
78
		}
79
		
80
		return $this === $value ? $default : $value;
81
	}
82
	
83
	/**
84
	 * Sets an input by name.
85
	 * 
86
	 * @param  string  $key
87
	 * @param  mixed  $value
88
	 * 
89
	 * @return void
90
	 */
91
	public function set(string $key, mixed $value): void
92
	{
93
		if (null !== $value && ! is_scalar($value) && ! is_array($value) && ! method_exists($value, '__toString')) {
94
			throw new BadRequestHttpException(sprintf('Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string, array, or null instead', get_debug_type($value), __METHOD__));
95
		}
96
		
97
		$this->parameters[$key] = $value;
98
	}
99
}