-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
82 lines (73 loc) · 2.85 KB
/
Input.php
File metadata and controls
82 lines (73 loc) · 2.85 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?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)
{
// TODO: Fill in this function
//if(isset($_REQUEST[$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 isPost()
{
return $_POST;
}
public static function get($key, $default = null)
{
// TODO: Fill in this function
if(self::has($key)){
return $_REQUEST[$key];
} else {
return $default;
}
}
public static function getString($key, $min = 0, $max = 100)
{
$string = self::get($key);
if(!is_string($string) || !is_numeric($min) || !is_numeric($max)){
throw new InvalidArgumentException("$string must be a string");
} elseif(is_numeric($string)){
throw new DomainException("{$string} is the wrong type!");
} elseif(strlen($string) > $max) {
throw new LengthException("{$string} is too long, less characters");
} elseif(strlen($string) < $min){
throw new LengthException("{$string} contains no characters, type something!");
}
return trim($string);
}
public static function getNumber($key, $min = 0, $max = 100 ){
$number = self::get($key);
if(!is_numeric($number) || !is_numeric($min) || !is_numeric($max)){
throw new InvalidArgumentException("{$number} We need integers!");
} elseif(is_string($number)){
throw new DomainException("{$number} is the wrong type");
}elseif($number < $min){
throw new RangeException("{$number} does not exist, please enter some values!");
} elseif($number > $max){
throw new RangeException("{$number} Your number exceeds the max!!");
} elseif(empty(self::get($key))){
throw new OutOfRangeException("{$number} hasn't been provided");
}
return floatval($number);
}
///////////////////////////////////////////////////////////////////////////
// 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() {}
}