-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
56 lines (56 loc) · 1.96 KB
/
Input.php
File metadata and controls
56 lines (56 loc) · 1.96 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
<?php
class Input
{
public static function getString($key){
if(is_scalar($key)){
if(self::has($key)){
return strval(self::get($key));
}
throw new Exception("The key given is not set.");
}
throw new Exception("The given value could not be read.");
}
public static function getNumber($key){
if(is_scalar($key)){
if(self::has($key)){
return intval(self::get($key));
}
throw new Exception("The key given is not set.");
}
throw new Exception("The given value could not be read.");
}
/**
* 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)
{
$result = isset($_REQUEST[$key]) ? true : false;
return $result;
}
/**
* 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)
{
$result = (self::has($key)) ? $_REQUEST[$key] : $default;
return $result;
}
public static function escape($input)
{
return htmlspecialchars(strip_tags($input));
}
///////////////////////////////////////////////////////////////////////////
// DO NOT EDIT ANYTHING BELOW!! //
// The Input class should not ever be instantiated, so we prevent the //
// constructor method from being called. We will be covering private //
// later in the curriculum. //
///////////////////////////////////////////////////////////////////////////
private function __construct() {}
}