1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Should be used for getting request data for processing pages. |
5
|
|
|
* For display pages, see SmrSession::getRequestVar. |
6
|
|
|
*/ |
7
|
|
|
class Request { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Returns true if index is set. |
11
|
|
|
* Note that this must be used for checkboxes, since the element is not |
12
|
|
|
* posted if a box is unchecked. |
13
|
|
|
*/ |
14
|
|
|
public static function has(string $index) : bool { |
15
|
|
|
return isset($_REQUEST[$index]); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns index value as an integer. |
20
|
|
|
*/ |
21
|
|
|
public static function getInt(string $index, int $default = null) : int { |
22
|
|
|
if (self::has($index)) { |
23
|
|
|
return (int)$_REQUEST[$index]; |
24
|
|
|
} elseif (!is_null($default)) { |
25
|
|
|
return $default; |
26
|
|
|
} |
27
|
|
|
throw new Exception('No request variable "' . $index . '"'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns index value as a float. |
32
|
|
|
*/ |
33
|
|
|
public static function getFloat(string $index, float $default = null) : float { |
34
|
|
|
if (self::has($index)) { |
35
|
|
|
return (float)$_REQUEST[$index]; |
36
|
|
|
} elseif (!is_null($default)) { |
37
|
|
|
return $default; |
38
|
|
|
} |
39
|
|
|
throw new Exception('No request variable "' . $index . '"'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns index value as an array of strings. |
44
|
|
|
*/ |
45
|
|
|
public static function getArray(string $index, array $default = null) : array { |
46
|
|
|
if (self::has($index)) { |
47
|
|
|
return $_REQUEST[$index]; |
48
|
|
|
} elseif (!is_null($default)) { |
49
|
|
|
return $default; |
50
|
|
|
} |
51
|
|
|
throw new Exception('No request variable "' . $index . '"'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns index value as an array of integers. |
56
|
|
|
*/ |
57
|
|
|
public static function getIntArray(string $index, array $default = null) : array { |
58
|
|
|
if (self::has($index)) { |
59
|
|
|
$result = []; |
60
|
|
|
foreach ($_REQUEST[$index] as $key => $value) { |
61
|
|
|
$result[$key] = (int)$value; |
62
|
|
|
} |
63
|
|
|
return $result; |
64
|
|
|
} elseif (!is_null($default)) { |
65
|
|
|
return $default; |
66
|
|
|
} |
67
|
|
|
throw new Exception('No request variable "' . $index . '"'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns index value as a string. |
72
|
|
|
*/ |
73
|
|
|
public static function get(string $index, string $default = null) : string { |
74
|
|
|
if (self::has($index)) { |
75
|
|
|
return $_REQUEST[$index]; |
76
|
|
|
} elseif (!is_null($default)) { |
77
|
|
|
return $default; |
78
|
|
|
} |
79
|
|
|
throw new Exception('No request variable "' . $index . '"'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns index value as a string from either $_REQUEST or $var. |
84
|
|
|
* This is useful for processing pages that need to handle data both from |
85
|
|
|
* posted form inputs and from container variables. |
86
|
|
|
* |
87
|
|
|
* Note that this does not save the result in $var (see SmrSession). |
88
|
|
|
*/ |
89
|
|
|
public static function getVar(string $index, string $default = null) : string { |
90
|
|
|
return self::getVarX($index, $default, 'get'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Like getVar, but returns an int instead of a string. |
95
|
|
|
*/ |
96
|
|
|
public static function getVarInt(string $index, int $default = null) : int { |
97
|
|
|
return self::getVarX($index, $default, 'getInt'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Like getVar, but returns an array of ints instead of a string. |
102
|
|
|
*/ |
103
|
|
|
public static function getVarIntArray(string $index, array $default = null) : array { |
104
|
|
|
return self::getVarX($index, $default, 'getIntArray'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Helper function to avoid code duplication in getVar* functions. |
109
|
|
|
*/ |
110
|
|
|
private static function getVarX($index, $default, $func) { |
111
|
|
|
global $var; |
112
|
|
|
if (isset($var[$index])) { |
113
|
|
|
// An index may be present in both var and request. This indicates |
114
|
|
|
// a logical error in the code, unless the values are the same, |
115
|
|
|
// which can occur if, e.g., player refreshes a page (this is OK). |
116
|
|
|
if (self::has($index) && $var[$index] !== self::$func($index, $default)) { |
117
|
|
|
throw new Exception('Index "' . $index . '" inconsistent between $var and $_REQUEST!'); |
118
|
|
|
} |
119
|
|
|
return $var[$index]; |
120
|
|
|
} |
121
|
|
|
return self::$func($index, $default); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|