I would like to get feedback on this, which are just simple Response
/Request
classes.
I tried to keep it simple, so I made two interfaces and two classes:
- RequestInterface.php
- Request.php
- ResponseInterface.php
- Response.php
The use Foo\Storage\ArrayHandler as access;
is just a class to access and set arrays, so I wouldn't have to use functions like array_key_exists
all over the place to check for keys, set, and unset arrays.
RequestInterface.php
namespace Foo\Http;
interface RequestInterface
{
public function get($key, $default = null);
public function post($key, $default = null);
public function server($key, $default = null);
public function getMethod();
}
ResponseInterface.php
namespace Foo\Http;
interface ResponseInterface
{
public function setStatusCode($statusCode);
public function addHeader($name, $value);
public function setContentType($contentType);
public function redirect($url);
}
Request.php
namespace Foo\Http;
use Foo\Storage\ArrayHandler as access;
class Request implements RequestInterface
{
protected $get;
protected $post;
protected $files;
protected $server;
protected $cookies;
public function __construct(
access $get,
access $post,
access $files,
access $server,
access $cookies
) {
$this->get = $get;
$this->post = $post;
$this->files = $files;
$this->server = $server;
$this->cookies = $cookies;
}
public function get($key, $defaultValue = null){
return $this->get->get($key, $defaultValue);
}
public function post($key, $defaultValue = null){
return $this->post->get($key, $defaultValue);
}
public function server($key, $defaultValue = null){
return $this->server->get($key, $defaultValue);
}
public function getMethod(){
return $this->server->get('REQUEST_METHOD');
}
}
Response.php
namespace Foo\Http;
class Response implements ResponseInterface
{
private $statusCode = 'HTTP/1.1 200 OK';
private $headers = [];
public function addHeader($name, $value){
$this->headers[$name][] = $value;
}
public function setHeader($name, $value){
$this->headers[$name] = [
(string) $value,
];
}
public function redirect($url){
$this->setHeader('Location', $url);
}
}
Response.php
, which you callRequest.php
; Each header can have multiple values? \$\endgroup\$RequestInterface
andResponseInterface
: composer package and original PSR \$\endgroup\$