-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_rest_input.php
More file actions
executable file
·36 lines (32 loc) · 916 Bytes
/
php_rest_input.php
File metadata and controls
executable file
·36 lines (32 loc) · 916 Bytes
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
<?php
// Facilitates input of data
class Input {
// Populates HTTP body
public static function decodeBody() {
$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody, true);
if (json_last_error() != JSON_ERROR_NONE) {
throw new BadRequestException("Body is not a valid JSON !");
}
else {
return $data;
}
}
// Parse date to timestamp
public static function parseDateTime($arg_name, $str_date, $format) {
if ($str_date == null) {
return null;
}
$result = DateTime::createFromFormat($format, $str_date, new DateTimeZone('UTC'));
if ($result == false) {
throw new BadRequestException($arg_name . " must be in the format " . $format);
}
return $result;
}
public static function guardNotNull($arg_name, $arg) {
if ($arg == null) {
throw new BadRequestException($arg_name . " must be specified !");
}
}
}
?>