This repository was archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRequest.php
More file actions
117 lines (95 loc) · 2.72 KB
/
Request.php
File metadata and controls
117 lines (95 loc) · 2.72 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
116
117
<?php
namespace APIJet;
class Request
{
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
private static $inputData = null;
private $authorizationCallback;
private $defaultResponseLimit;
public function setAuthorizationCallback($authorizationCallback)
{
$this->authorizationCallback = $authorizationCallback;
}
public function setDefaultResponseLimit($defaultResponseLimit)
{
$this->defaultResponseLimit = $defaultResponseLimit;
}
public static function getCleanRequestUrl()
{
$rawRequestUrl = $_SERVER["REQUEST_URI"];
$clearnRequestUrl = strstr($rawRequestUrl, '?', true);
// if it doens't content any GET data
if ($clearnRequestUrl === false) {
$clearnRequestUrl = $rawRequestUrl;
}
// remote first slash
return substr($clearnRequestUrl, 1);
}
public static function getHeader($key)
{
return self::getallheaders()[$key];
}
public static function getMethod()
{
return $_SERVER['REQUEST_METHOD'];
}
public function getLimit()
{
if (isset($_GET['limit'])) {
$limit = (int) $_GET['limit'];
if ($limit > 0) {
return $limit;
}
}
return $this->defaultResponseLimit;
}
public static function getOffset()
{
if (isset($_GET['offset'])) {
$offset = (int) $_GET['offset'];
if ($offset > 0) {
return $offset;
}
}
return 0;
}
public function isАuthorized()
{
$authorizationCallback = $this->authorizationCallback;
if ($authorizationCallback === null) {
return true;
}
return (bool) $authorizationCallback();
}
public static function getInputData()
{
if (self::$inputData === null) {
$inputData = [];
$rawInput = file_get_contents('php://input');
if (!empty($rawInput)) {
mb_parse_str($rawInput, $inputData);
}
self::$inputData = $inputData;
}
return self::$inputData;
}
/**
* @desc Get all headers
* @return array
*/
private static function getallheaders()
{
$headers = array();
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}