-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
59 lines (58 loc) · 1.6 KB
/
Input.php
File metadata and controls
59 lines (58 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class Input
{
/**
* Check if a given value was passed in the request
*
* @param string $key index to look for in request
* @return boolean whether value exists in $_POST or $_GET
*/
public static function has($key)
{
return isset($_REQUEST[$key]);
}
/**
* Get a requested value from either $_POST or $_GET
*
* @param string $key index to look for in index
* @param mixed $default default value to return if key not found
* @return mixed value passed in request
*/
public static function get($key, $default = null)
{
return self::has($key) ? $_REQUEST[$key] : $default;
}
/**
* @return bool Returns true if the current request is a POST request
*/
public static function isPost()
{
return $_SERVER['REQUEST_METHOD'] === 'POST';
}
/**
* Prevent the creation of instances of this class
*/
private function __construct() {}
public static function getString($key){
$value = self::get($key);
if(!is_string($value)){
throw new InvalidEntry("Error: Input must be a string.");
}
$value = trim($value);
if(empty($value)){
throw new EmptyEntry("Error: Input is empty.");
}
return $value;
}
public static function getNumber($key){
$value = self::get($key);
if(!is_numeric($value)){
throw new InvalidEntry("Error: Input must be a number.");
}
if(empty($value)) {
throw new EmptyEntry("Error: Input is empty");
}
$value = floatval($value);
return $value;
}
}