-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
147 lines (92 loc) · 3.53 KB
/
Input.php
File metadata and controls
147 lines (92 loc) · 3.53 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?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)
{
if (self::has($key)) {
return $_REQUEST[$key];
} else {
return $default;
}
}
public static function escape($value)
{
$outString = htmlspecialchars(strip_tags($value));
return $outString;
}
protected static function argCheck($key, $min, $max) {
if (!is_string($key)) {
throw new InvalidArgumentException("$key used as the function argument is not a string");
}
if (!is_numeric($min)|| $min>$max) {
throw new InvalidArgumentException("the function argument $min must be numeric and less than $max");
}
if (!is_numeric($max)|| $max<$min) {
throw new InvalidArgumentException("the function argument $max must be numeric and greater than $min");
}
}
protected static function isNull($input) {
if (empty($input)) {
throw new OutOfRangeException("$input is not defined");
}
}
public static function getWordName($key, $min = 0, $max = 1000)
{
self::argCheck($key, $min, $max);
$input = self::get($key);
self::isNUll($input);
if (is_numeric($input)||is_array($input)) {
throw new DomainException("$input is not an alphanumeric word or name");
}
if ($input > $max || $input < $min) {
throw new LengthException("$input does not fit $min-$max length");
}
return $input;
}
public static function getnumber($key, $min = -2147483647, $max = 2147483647)
{
self::argCheck($key, $min, $max);
$input = self::get($key);
self::isNUll($input);
if (!is_numeric($input)||is_array($input)) {
throw new DomainException("$input is not a number");
}
if ($input > $max || $input < $min) {
throw new RangeException("$input is out of $min - $max range");
}
return $input;
}
public static function getdate($key)
{
$input = self::get($key);
$bigdate = date_create($input);
if (!$bigdate||empty($input)) {
throw new Exception("$input is not in a valid date format");
}
return $bigdate;
}
///////////////////////////////////////////////////////////////////////////
// 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() {}
}