|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gvera\Helpers\http; |
|
4
|
|
|
|
|
5
|
|
|
use Gvera\Exceptions\NotFoundException; |
|
6
|
|
|
use Gvera\Helpers\fileSystem\File; |
|
7
|
|
|
use Gvera\Models\BasicAuthenticationDetails; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class HttpRequest |
|
11
|
|
|
* @package Gvera\Helpers\http |
|
12
|
|
|
* This is a request wrapper to manage params making an abstraction from the request type |
|
13
|
|
|
*/ |
|
14
|
|
|
class HttpRequest |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
private $requestType; |
|
18
|
|
|
private $requestParams = array(); |
|
19
|
|
|
private $fileManager; |
|
20
|
|
|
|
|
21
|
|
|
const GET = 'GET'; |
|
22
|
|
|
const POST = 'POST'; |
|
23
|
|
|
const PUT = 'PUT'; |
|
24
|
|
|
const PATCH = "PATCH"; |
|
25
|
|
|
const DELETE = 'DELETE'; |
|
26
|
|
|
const OPTIONS = 'OPTIONS'; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(FileManager $fileManager) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->fileManager = $fileManager; |
|
31
|
|
|
$this->requestType = $_SERVER['REQUEST_METHOD']; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $name |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getParameter($name) |
|
39
|
|
|
{ |
|
40
|
|
|
if (isset($this->requestParams[$name])) { |
|
41
|
|
|
return $this->requestParams[$name]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$req = strtolower($this->requestType); |
|
45
|
|
|
return $this->$req($name); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array|object |
|
50
|
|
|
*/ |
|
51
|
|
|
public function get($name = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$getArray = filter_input_array(INPUT_GET); |
|
54
|
|
|
return (null === $name || !isset($getArray[$name]) || false === $getArray) ? null : $getArray[$name]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $name |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function post($name = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$postArray = filter_input_array(INPUT_POST); |
|
64
|
|
|
return (null === $name || !isset($postArray[$name]) || false === $postArray) ? null : $postArray[$name]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $name |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public function patch($name = null) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->getFromStream($name); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param null $name |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function put($name = null) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->getFromStream($name); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param null $name |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function delete($name = null) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getFromStream($name); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $name |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
private function getFromStream(string $name) |
|
99
|
|
|
{ |
|
100
|
|
|
$streamContent = []; |
|
101
|
|
|
parse_str(file_get_contents("php://input"), $streamContent); |
|
102
|
|
|
|
|
103
|
|
|
if (!isset($streamContent[$name])) { |
|
104
|
|
|
throw new NotFoundException('parameter not found'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $streamContent[$name]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return boolean |
|
112
|
|
|
*/ |
|
113
|
|
|
public function isPost() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->requestType == self::POST; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return boolean |
|
120
|
|
|
*/ |
|
121
|
|
|
public function isGet() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->requestType == self::GET; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return boolean |
|
128
|
|
|
*/ |
|
129
|
|
|
public function isPut() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->requestType == self::PUT; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return boolean |
|
136
|
|
|
*/ |
|
137
|
|
|
public function isDelete() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->requestType == self::DELETE; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getRequestType() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->requestType; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function setParameter($key, $value) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->requestParams[$key] = $value; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return boolean |
|
157
|
|
|
*/ |
|
158
|
|
|
public function isAjax() |
|
159
|
|
|
{ |
|
160
|
|
|
return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
|
161
|
|
|
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param $directory |
|
166
|
|
|
* @param File $file |
|
167
|
|
|
* @return bool |
|
168
|
|
|
* @throws \Gvera\Exceptions\InvalidFileTypeException |
|
169
|
|
|
* @throws \Gvera\Exceptions\NotFoundException |
|
170
|
|
|
*/ |
|
171
|
|
|
public function moveFileToDirectory($directory, File $file) |
|
|
|
|
|
|
172
|
|
|
{ |
|
173
|
|
|
return $this->fileManager->saveToFileSystem($directory, $file); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param $propertyName |
|
178
|
|
|
* @param string|null $changedName |
|
179
|
|
|
* @return File |
|
180
|
|
|
* @throws \Gvera\Exceptions\NotFoundException |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getFileByPropertyName($propertyName, ?string $changedName = null):File |
|
183
|
|
|
{ |
|
184
|
|
|
$this->fileManager->buildFilesFromSource($_FILES, $changedName); |
|
185
|
|
|
return $this->fileManager->getByName($propertyName); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function getAuthDetails(): ?BasicAuthenticationDetails |
|
189
|
|
|
{ |
|
190
|
|
|
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) { |
|
191
|
|
|
return null; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return new BasicAuthenticationDetails($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function getIP():string |
|
198
|
|
|
{ |
|
199
|
|
|
return $_SERVER['REMOTE_ADDR']; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.