-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.php
98 lines (91 loc) · 3.62 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
96
97
98
<?php
class DateRangeException extends Exception {}
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
$answer = (isset($_REQUEST[$key]) ? 'true' : 'false');
return $answer;
}
/**
* 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
$answer = (isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default);
return $answer;
}
public static function getString($key, $min = 0, $max = 0) {
if (!is_string($key) || !is_numeric($min) || !is_numeric($max)) {
throw new InvalidArgumentException("{$key} is not a string or {$min} or {$max} are not numbers");
}
$string = (static::has($key) ? static::get($key) : "");
if (is_null($string)) {
throw new OutOfRangeException("{$key} does not exist");
}
if (!is_string($string)) {
throw new DomainException("{$string} is not a string");
}
if (strlen($string) < $min || strlen($string) > $max) {
throw new LengthException("{$string} is not the right length.");
}
return $string;
}
public static function getNumber($key, $min = 0, $max = 0) {
if (!is_string($key) || !is_numeric($min) || !is_numeric($max)) {
throw new InvalidArgumentException("{$key} is not a string or {$min} or {$max} are not numbers");
}
$num = (static::has($key) ? static::get($key) : "");
if (is_null($num)) {
throw new OutOfRangeException("{$key} does not exist");
}
if (!is_numeric($num)) {
throw new DomainException("{$num} is not a number");
}
if ($num < $min || $num > $max) {
throw new RangeException("{$num} is smaller than {$min} or bigger than {$max}");
}
return (float)$num;
}
public static function getDate($key, $min = "1970-01-01", $max = "2222-12-31") {
if (!is_string($key)) {
throw new InvalidArgumentException("{$key} is not a string");
}
if (!DateTime::createFromFormat('Y-m-d', $min) || !DateTime::createFromFormat('Y-m-d', $max)) {
throw new InvalidArgumentException("{$min} or {$max} are not valid dates.");
};
$date = (static::has($key) ? static::get($key) : "");
if (is_null($date)) {
throw new OutOfRangeException("{$key} does not exist");
}
if (!(bool)strtotime($date)) {
throw new DomainException("{$date} is not a date.");
}
$minDate = new DateTime($min);
$maxDate = new DateTime($max);
$dt = new DateTime($date);
if ($dt < $minDate || $dt > $maxDate) {
throw new DateRangeException("{$date} is not between {$min} and {$max}");
}
return $dt;
}
///////////////////////////////////////////////////////////////////////////
// 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() {}
}