-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
120 lines (89 loc) · 3.5 KB
/
Input.php
File metadata and controls
120 lines (89 loc) · 3.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?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 $key='';
// This array will hold error messages
public static function has($key)
{
// TODO: Fill in this function
if (isset($_REQUEST[$key])){
return true;
}else{
return false;
}
}
/**
* 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)
{
// TODO: Fill in this function
if (self::has($key)) {
return $_REQUEST[$key];
}else{
return $default;
}
}
// This function gets strings, the key has to be a string and uses the get request to putll it from $_REQUEST if it exists
public static function getString($key, $default = null, $min = 1, $max = 1000)
{
if (!is_string($key)||!is_numeric($min)||!is_numeric($max)) {
throw new InvalidArgumentException("Argument is invalid!");
}elseif (strlen($key)<$min||strlen($key)>$max) {
throw new LengthException("String not valid length!");
}else{
if (!self::has($key) && $default === null) {
throw new OutOfRangeException("$key does not exist");
} else if (is_string(self::get($key, $default)) && !is_numeric(self::get($key, $default))) {
return self::get($key, $default);
} else {
throw new DomainException($key . ' must be a string');
}
}
}
// This function gets numbers, the key has to be a number or the string of a number
// and uses the get request to putll it from $_REQUEST if it exists
public static function getNumber($key, $default = null, $min = 0, $max = 100000)
{
if (!is_numeric($min)||!is_numeric($max)) {
throw new InvalidArgumentException("Argument is invalid!");
}elseif ($key<$min||$key>$max) {
throw new RangeException("Number not within range!");
}else{
if (!self::has($key) && $default === null) {
throw new OutOfRangeException("$key does not exist");
}else if (is_numeric(self::get($key, $default))) {
return floatval(self::get($key, $default));
}else{
throw new DomainException($key . ' must be a number');
}
}
}
public static function getErrors(){
return self::e;
}
///////////////////////////////////////////////////////////////////////////
// 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() {}
}
// $errors = [];
// try {
// throw new Exception("This is an error.");
// }catch (Exception $e){
// $errors[] = $exception->getMessage();
// }
// var_dump($errors);