Skip to content

Commit eac9deb

Browse files
Request and Response objects added
* Request object can be used to fetch query params, headers, cookies, url and request method * Response object can be used to send a response by setting the response data which will be echoed and status * RedirectResponse can be used to redirect the page to another location * All the classes uses static methods that makes it simpler to obtain data without needing to initialize anything
1 parent 7e0895d commit eac9deb

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "encryptorcode/php-server-utils",
3+
"description": "A bunch of classes used for handling requests and response seamlessly...",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Abhay Jatin Doshi",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"psr-4": {
15+
"encryptorcode\\server\\": "src/"
16+
}
17+
}
18+
}

src/request/Request.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
namespace encryptorcode\server\request;
3+
4+
class Request{
5+
private $method;
6+
private $path;
7+
private $params;
8+
private $headers;
9+
private $cookies;
10+
11+
private function __construct(){
12+
$this->method = $_SERVER["REQUEST_METHOD"];
13+
$this->path = $_SERVER["REQUEST_URI"];
14+
$this->params = $_GET;
15+
$this->headers = array_change_key_case(getallheaders(),CASE_UPPER);
16+
$this->cookies = $_COOKIE;
17+
}
18+
19+
private static $request;
20+
private static function request() : Request{
21+
if(!isset(self::$request)){
22+
self::$request = new self();
23+
}
24+
return self::$request;
25+
}
26+
27+
public static function method() : string{
28+
return self::request()->method;
29+
}
30+
31+
public static function path() : string{
32+
return self::request()->path;
33+
}
34+
35+
public static function params() : array{
36+
return self::request()->params;
37+
}
38+
39+
public static function param($key) : ?string{
40+
$params = self::request()->params;
41+
if(isset($params[$key])){
42+
return $params[$key];
43+
} else {
44+
return null;
45+
}
46+
}
47+
48+
public static function headers() : array{
49+
return self::request()->headers;
50+
}
51+
52+
public static function header($key) : ?string{
53+
$headers = self::request()->headers;
54+
if(isset($headers[$key])){
55+
return $headers[$key];
56+
} else {
57+
return null;
58+
}
59+
}
60+
61+
public static function cookies() : array{
62+
return self::request()->cookies;
63+
}
64+
65+
public static function cookie($key) : ?string{
66+
$cookies = self::request()->cookies;
67+
if(isset($cookies[$key])){
68+
return $cookies[$key];
69+
} else {
70+
return null;
71+
}
72+
}
73+
}

src/response/RedirectResponse.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace encryptorcode\server\response;
3+
4+
class RedirectResponse extends Response{
5+
6+
private $redirectUrl;
7+
8+
public function __construct(?string $redirectUrl){
9+
$this->redirectUrl = $redirectUrl;
10+
}
11+
12+
public function respond() : void{
13+
header("Location: $this->redirectUrl");
14+
exit();
15+
}
16+
}

src/response/Response.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace encryptorcode\server\response;
3+
4+
abstract class Response{
5+
6+
private $data;
7+
private $status;
8+
9+
public function __construct(?string $data, $status=200){
10+
$this->data = $data;
11+
$this->status = $status;
12+
13+
if((!isset($data) || $data == "") && ($status == 200 || $status == 201)){
14+
$this->status = 204;
15+
}
16+
}
17+
18+
public function respond() : void{
19+
http_response_code($this->status);
20+
echo $this->data;
21+
}
22+
}

0 commit comments

Comments
 (0)