/
Input.php
95 lines (88 loc) · 3.29 KB
/
Input.php
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
<?php
class Input
{
public static function notEmpty($key)
{
if(isset($_REQUEST[$key]) && $_REQUEST[$key] != '') {
return true;
}
}
/**
* 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]);
// TODO: Fill in this function
}
/**
* 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 (Input::has($key)) {
return $_REQUEST[$key];
}
return $default;
// TODO: Fill in this function
}
public static function getString($key, $min = 1, $max = 250)
{
// Check if value is a string
if (!self::has($key)) {
throw new OutOfRangeException('This string field must be filled!');
} else if (!is_string($key)) {
throw new DomainException('This must be a string!!');
} else if (!is_numeric($min) or !is_numeric($max)) {
throw new InvalidArgumentException('The field must be filled with numbers.');
} else if (is_string($key) < is_numeric($min) or is_string($key) > is_numeric($max)) {
throw new LengthException('The string is shorter than the minimum or longer than the maximun.');
}
return ($key);
}
public static function getNumber($key, $min = 1, $max = 250)
{
// Check if value is a number
if (!self::has($key)) {
throw new InvalidArgumentException('This number field must be filled!');
} else if (!is_numeric($key)) {
throw new InvalidArgumentException('This must be a number!!');
} else if (($key) < is_numeric($min) or ($key) > is_numeric($max)) {
throw new RangeException('The number is less than the minimum or greater than the maximum.');
} else {
return ($key);
}
}
public static function getDate($key)
{
// Check for correct date and or time
$inputValue = self::get($key);
try
{
$dateTimeObject = New DateTime($inputValue);
} catch (Exception $e) {
throw new Exception("{$key} must be a valid date in the format of MM/DD/YYYY");
}
return ($dateTimeObject);
}
// public static function validateDate($date)
// {
// $date = DateTime::createFromFormat('Y-m-d', $date);
// return $date && $date->format('Y-m-d') == $date;
// }
// }
///////////////////////////////////////////////////////////////////////////
// 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() {}
}