-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
107 lines (81 loc) · 2.44 KB
/
Copy pathInput.php
File metadata and controls
107 lines (81 loc) · 2.44 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
<?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)
{
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)
{
if (self::has($key) == true) {
return $_REQUEST[$key];
} else {
return $default;
}
}
///////////////////////////////////////////////////////////////////////////
// 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() {}
public static function getString($key)
{
$value = self::get($key);
if(($value == null || is_resource($value)) ||
(is_numeric($value) || is_bool($value)) ||
is_array($value)) {
throw new Exception("THE VALUE $key NEEDS TO BE A STRING!!!!");
} else {
return $value;
}
}
// public static function getString($key)
// {
// $value = self::get($key);
// if(ctype_digit($value) || $value == null) {
// throw new Exception('The value needs to be a string');
// }
// else if (!ctype_alnum($value))
// {
// throw new Exception('The value needs to be a string');
// }
// return $value;
// }
public static function getNumber($key)
{
$value = self::get($key, $max, $min);
if(!is_numeric($value) || $value == null) {
throw new Exception("THE VALUE $key NEEDS TO BE NUMERIC!!!");
}
return (float) $value;
}
public static function getDate($key)
{
$value = self::get($key);
$validDate = date_create($value);
if($validDate) {
return $validDate;
} else {
throw new Exception('The value DATE needs a valid Date!');
}
}
}