Skip to content

Instantly share code, notes, and snippets.

@jm42
Created September 15, 2014 15:49
Show Gist options
  • Save jm42/acb6d80f72b3c8d694de to your computer and use it in GitHub Desktop.
Save jm42/acb6d80f72b3c8d694de to your computer and use it in GitHub Desktop.
HTTP Interfaces Example Implementation
<?php
namespace Ex\HTTP;
use Aix\HTTP\Request as BaseRequest;
class Request implements BaseRequest
{
private $method;
private $uri;
private $headers;
private $content;
private $body;
protected $query;
protected $form;
public function __construct($method, $uri, $headers, $content=null) {
$this->method = $method;
$this->uri = $uri;
$this->headers = $headers;
$this->content = $content;
}
public function __toString() {
return $this->getMethod() . ' ' . $this->getURI() . 'HTTP/1.1' . "\r\n"
. $this->getHeaders()->__toString() . "\r\n"
. $this->getBody();
}
public function getMethod() {
return $this->method;
}
public function getURI() {
return $this->uri;
}
public function getHeaders() {
return $this->headers;
}
public function getBody() {
if (null === $this->body) {
$this->body = $this->content; // TODO
}
return $this->body;
}
public function getQuery() {
return $this->query;
}
public function getForm() {
return $this->form;
}
}
final class Headers implements \ArrayAccess
{
static private $map = array(
'Cookie' => '\\Ex\\HTTP\\Header\\Cookie',
);
private $haders = array();
public function offsetExists($offset) {
if (array_key_exists($offset, $this->haders)) {
return true;
}
return filter_has_var(INPUT_SERVER, $this->transformName($offset));
}
public function offsetGet($offset) {
if (array_key_exists($offset, $this->haders)) {
return $this->headers[$offset];
}
$value = filter_input(INPUT_SERVER, $this->transformName($offset));
if (false === $value) {
throw new \InvalidArgumentException('Header not found');
}
return $this->headers[$offset] = $this->transformValue($offset, $value);
}
public function offsetSet($offset, $value) {}
public function offsetUnset($offset) {}
private function transformName($name) {
if (strpos($name, 'Content-') === 0) {
$key = substr($name, 8);
$key = 'CONTENT_' . strtoupper($key);
} else {
$key = 'HTTP_' . strtoupper(strtr($name, '-', '_'));
}
return $key;
}
private function transformValue($name, $value) {
if (array_key_exists($name, self::$map)) {
$class = self::$map[$name];
} else {
$class = '\\Ex\\HTTP\\Header\\Generic';
}
return new $class($name, $value);
}
}
namespace Ex\HTTP\Header;
use Aix\HTTP\Header;
class Generic implements Header
{
private $name;
private $value;
public function __construct($name, $value) {
$this->name = $name;
$this->value = $value;
}
public function getName() {
return $this->name;
}
public function getValue() {
return $this->value;
}
}
class Cookie extends Generic implements Header
{
}
namespace Ex\HTTP\Request;
use Aix\HTTP\Request\Parameters as BaseParameters;
use Aix\HTTP\Request\Form as BaseForm;
class Parameters extends \ArrayObject implements BaseParameters
{
public function __construct(array $values=null) {
if (null === $values) {
$values = array();
}
parent::__construct($values);
}
}
class Form extends Parameters implements BaseForm
{
private $files;
public function __construct(array $values=null, array $files=null) {
parent::__construct($values);
if (null === $files) {
$files = array();
}
$this->files = $files;
}
public function getFiles() {
return $this->files;
}
}
namespace Ex\HTTP\PHP;
use Ex\HTTP\Request as BaseRequest;
use Ex\HTTP\Request\Parameters;
use Ex\HTTP\Request\Form;
use Ex\HTTP\Headers;
class Request extends BaseRequest
{
public function __construct() {
parent::__construct(
filter_input(INPUT_SERVER, 'REQUEST_METHOD'),
filter_input(INPUT_SERVER, 'REQUEST_URI'),
new Headers()
);
$this->query = new Parameters($_GET);
$this->form = new Form($_POST, $_FILES);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment