-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.php
More file actions
115 lines (90 loc) · 3.45 KB
/
Input.php
File metadata and controls
115 lines (90 loc) · 3.45 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
<?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)
{
// TODO: Fill in this function
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)
{
// TODO: Fill in this function
return (Input::has($key)) ? $_REQUEST[$key] : null;
}
public static function getString($key, $min = 0, $max = 50)
{
$string = self::get($key);
if (!is_string($string) || is_numeric($string)) {
throw new InvalidArgumentException("$string must be a string!");
throw new DomainException("$string is the wrong data type!");
}elseif (empty($string)) {
throw new OutofRangeException("You must enter a value");
}elseif (strlen($string) > $max) {
throw new LengthException("You've run out of characters");
}elseif (strlen($string) < $min) {
throw new LengthException("You need to type more characters");
}
return $string;
}
public static function getNumber($key, $min = 0, $max = 50)
{
$number = self::get($key);
if (is_numeric($number)) {
$number = (int)$number;
}elseif (!is_numeric($number)) {
throw new DomainException("$number is the wrong data type!");
}else {
throw new InvalidArgumentException("$number must be a number!");
}
if (empty($number)) {
throw new OutofRangeException("You must enter a value");
}
if (!is_numeric($min)) {
throw new InvalidArgumentException("Your argument is invalid");
}
if (!is_numeric($max)) {
throw new InvalidArgumentException("Your argument is invalid");
}
if ($number < $min) {
throw new RangeException("Your number is too small");
}elseif ($number > $max) {
throw new RangeException("Your number is too big");
}
return $number;
}
public static function getDate($key)
{
$date = self::get($key);
$newDate = date_create($date);
if (!($newDate instanceof DateTime)) {
throw new Exception("$date must be a date!");
throw new DomainException("$date is the wrong data type!");
}elseif (empty($date)) {
throw new OutofRangeException("You must enter a value");
}
return $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() {}
}
/**
*
*/