Skip to content

Commit 0cb4d81

Browse files
committed
create request and response class
1 parent 1fa9108 commit 0cb4d81

File tree

2 files changed

+353
-0
lines changed

2 files changed

+353
-0
lines changed

System/Http/Request.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of simple-mvc-rest-api for PHP.
6+
*
7+
*/
8+
namespace Http;
9+
10+
/**
11+
* Class Request an http request
12+
*
13+
* @author Mohammad Rahmani <rto1680@gmail.com>
14+
*
15+
* @package Http
16+
*/
17+
class Request {
18+
19+
/**
20+
* Get COOKIE Super Global
21+
* @var
22+
*/
23+
public $cookie;
24+
25+
/**
26+
* Get REQUEST Super Global
27+
* @var
28+
*/
29+
public $request;
30+
31+
/**
32+
* Get FILES Super Global
33+
* @var
34+
*/
35+
public $files;
36+
37+
/**
38+
* Request constructor.
39+
*/
40+
public function __construct() {
41+
$this->request = ($_REQUEST);
42+
$this->cookie = $this->clean($_COOKIE);
43+
$this->files = $this->clean($_FILES);
44+
}
45+
46+
/**
47+
* Get $_GET parameter
48+
*
49+
* @param String $key
50+
* @return string
51+
*/
52+
public function get(String $key = '') {
53+
if ($key != '')
54+
return isset($_GET[$key]) ? $this->clean($_GET[$key]) : null;
55+
56+
return $this->clean($_GET);
57+
}
58+
59+
/**
60+
* Get POST parameter
61+
*
62+
* @param String $key
63+
* @return string
64+
*/
65+
public function input($key) {
66+
if ($key != '') {
67+
$postdata = file_get_contents("php://input");
68+
$request = json_decode($postdata, true);
69+
70+
return isset($request[$key]) ? $this->clean($request[$key]) : null;
71+
}
72+
73+
return $this->clean($request);
74+
}
75+
76+
/**
77+
* Get value for server super global var
78+
*
79+
* @param String $key
80+
* @return string
81+
*/
82+
public function server(String $key = '') {
83+
return isset($_SERVER[strtoupper($key)]) ? $this->clean($_SERVER[strtoupper($key)]) : $this->clean($_SERVER);
84+
}
85+
86+
/**
87+
* Get Method
88+
*
89+
* @return string
90+
*/
91+
public function getMethod() {
92+
return strtoupper($this->server('REQUEST_METHOD'));
93+
}
94+
95+
/**
96+
* Returns the client IP addresses.
97+
*
98+
* @return string
99+
*/
100+
public function getClientIp() {
101+
return $this->server('REMOTE_ADDR');
102+
}
103+
104+
/**
105+
* Script Name
106+
*
107+
* @return string
108+
*/
109+
public function getUrl() {
110+
return $this->server('QUERY_STRING');
111+
}
112+
113+
/**
114+
* Clean Data
115+
*
116+
* @param $data
117+
* @return string
118+
*/
119+
private function clean($data) {
120+
if (is_array($data)) {
121+
foreach ($data as $key => $value) {
122+
123+
// Delete key
124+
unset($data[$key]);
125+
126+
// Set new clean key
127+
$data[$this->clean($key)] = $this->clean($value);
128+
}
129+
} else {
130+
$data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
131+
}
132+
133+
return $data;
134+
}
135+
}

System/Http/Response.php

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of simple-mvc-rest-api for PHP.
6+
*
7+
*/
8+
namespace Http;
9+
10+
/**
11+
* Class Response an Http response
12+
*
13+
* @author Mohammad Rahmani <rto1680@gmail.com>
14+
*
15+
* @package Http
16+
*/
17+
class Response {
18+
19+
/**
20+
* @var array
21+
*/
22+
protected $headers = [];
23+
24+
/**
25+
* @var array
26+
*/
27+
protected $statusTexts = [
28+
// INFORMATIONAL CODES
29+
100 => 'Continue',
30+
101 => 'Switching Protocols',
31+
102 => 'Processing',
32+
// SUCCESS CODES
33+
200 => 'OK',
34+
201 => 'Created',
35+
202 => 'Accepted',
36+
203 => 'Non-Authoritative Information',
37+
204 => 'No Content',
38+
205 => 'Reset Content',
39+
206 => 'Partial Content',
40+
207 => 'Multi-status',
41+
208 => 'Already Reported',
42+
// REDIRECTION CODES
43+
300 => 'Multiple Choices',
44+
301 => 'Moved Permanently',
45+
302 => 'Found',
46+
303 => 'See Other',
47+
304 => 'Not Modified',
48+
305 => 'Use Proxy',
49+
306 => 'Switch Proxy', // Deprecated
50+
307 => 'Temporary Redirect',
51+
// CLIENT ERROR
52+
400 => 'Bad Request',
53+
401 => 'Unauthorized',
54+
402 => 'Payment Required',
55+
403 => 'Forbidden',
56+
404 => 'Not Found',
57+
405 => 'Method Not Allowed',
58+
406 => 'Not Acceptable',
59+
407 => 'Proxy Authentication Required',
60+
408 => 'Request Time-out',
61+
409 => 'Conflict',
62+
410 => 'Gone',
63+
411 => 'Length Required',
64+
412 => 'Precondition Failed',
65+
413 => 'Request Entity Too Large',
66+
414 => 'Request-URI Too Long',
67+
415 => 'Unsupported Media Type',
68+
416 => 'Requested range not satisfiable',
69+
417 => 'Expectation Failed',
70+
418 => 'I\'m a teapot',
71+
422 => 'Unprocessable Entity',
72+
423 => 'Locked',
73+
424 => 'Failed Dependency',
74+
425 => 'Unordered Collection',
75+
426 => 'Upgrade Required',
76+
428 => 'Precondition Required',
77+
429 => 'Too Many Requests',
78+
431 => 'Request Header Fields Too Large',
79+
// SERVER ERROR
80+
500 => 'Internal Server Error',
81+
501 => 'Not Implemented',
82+
502 => 'Bad Gateway',
83+
503 => 'Service Unavailable',
84+
504 => 'Gateway Time-out',
85+
505 => 'HTTP Version not supported',
86+
506 => 'Variant Also Negotiates',
87+
507 => 'Insufficient Storage',
88+
508 => 'Loop Detected',
89+
511 => 'Network Authentication Required',
90+
];
91+
92+
/**
93+
* @var
94+
*/
95+
protected $version;
96+
97+
/**
98+
* @var
99+
*/
100+
protected $content;
101+
102+
/**
103+
* Response constructor.
104+
*/
105+
public function __construct () {
106+
$this->setVersion('1.1');
107+
}
108+
109+
/**
110+
* Set the Http protocol version
111+
*
112+
* @param string $version
113+
*/
114+
public function setVersion(string $version) {
115+
$this->version = $version;
116+
}
117+
118+
/**
119+
* Get the Http protocol version
120+
*
121+
* @return string
122+
*/
123+
public function getVersion() : string {
124+
return $this->version;
125+
}
126+
127+
/**
128+
* Get the status code text.
129+
*
130+
* @param int $code
131+
* @return string
132+
*/
133+
public function getStatusCodeText(int $code) : string {
134+
return (string) isset($this->statusTexts[$code]) ? $this->statusTexts[$code] : 'unknown status';
135+
}
136+
137+
/**
138+
* Set the response Headers.
139+
*
140+
* @param $header
141+
*/
142+
public function setHeader(String $header) {
143+
$this->headers[] = $header;
144+
}
145+
146+
/**
147+
* Get the response Headers.
148+
*
149+
* @return array
150+
*/
151+
public function getHeader() {
152+
return $this->headers;
153+
}
154+
155+
/**
156+
* Set content response.
157+
*
158+
* @param $content
159+
*/
160+
public function setContent($content) {
161+
$this->content = json_encode($content);
162+
}
163+
164+
/**
165+
* Get content response.
166+
*
167+
* @return mixed
168+
*/
169+
public function getContent() {
170+
return $this->content;
171+
}
172+
173+
/**
174+
* @param $url
175+
*/
176+
public function redirect($url) {
177+
if (empty($url)) {
178+
trigger_error('Cannot redirect to an empty URL.');
179+
exit;
180+
}
181+
182+
header('Location: ' . str_replace(array('&amp;', "\n", "\r"), array('&','', ''), $url), true, 302);
183+
exit();
184+
}
185+
186+
/**
187+
* check status code is invalid
188+
*
189+
* @return bool
190+
*/
191+
public function isInvalid(int $statusCode) : bool {
192+
return $statusCode < 100 || $statusCode >= 600;
193+
}
194+
195+
public function sendStatus($code) {
196+
if (!$this->isInvalid($code)) {
197+
$this->setHeader(sprintf('HTTP/1.1 ' . $code . ' %s' , $this->getStatusCodeText($code)));
198+
}
199+
}
200+
201+
/**
202+
* Render Output
203+
*/
204+
public function render() {
205+
if ($this->content) {
206+
$output = $this->content;
207+
208+
// Headers
209+
if (!headers_sent()) {
210+
foreach ($this->headers as $header) {
211+
header($header, true);
212+
}
213+
}
214+
215+
echo $output;
216+
}
217+
}
218+
}

0 commit comments

Comments
 (0)