Skip to content

Instantly share code, notes, and snippets.

@jm42
Created September 6, 2014 22:07
Show Gist options
  • Save jm42/e7e788d7e257ce1f93c7 to your computer and use it in GitHub Desktop.
Save jm42/e7e788d7e257ce1f93c7 to your computer and use it in GitHub Desktop.
Lazy Request
<?php
abstract class Lazy implements \ArrayAccess {
private $input;
private $cache = array();
public function __construct($input) {
static $inputs = array(
INPUT_POST, INPUT_GET, INPUT_COOKIE,
INPUT_ENV, INPUT_SERVER, INPUT_SESSION
);
if (!in_array($input, $inputs)) {
throw new \InvalidArgumentException('Invalid input');
}
$this->input = $input;
}
public function offsetExists($offset) {
if (array_key_exists($offset, $this->cache)) {
return true;
}
return filter_has_var($this->input, $this->transformName($offset));
}
public function offsetGet($offset) {
if (array_key_exists($offset, $this->cache)) {
return $this->cache[$offset];
}
$value = filter_input($this->input, $this->transformName($offset));
if (false === $value) {
throw new \LogicException('Error');
}
return $this->cache[$offset] = $this->transformValue($offset, $value);
}
public function offsetSet($offset, $value) {}
public function offsetUnset($offset) {}
protected function transformName($name) { return $name; }
protected function transformValue($name, $value) { return $value; }
}
class Query extends Lazy {
public function __construct() {
parent::__construct(INPUT_GET);
}
}
class Form extends Lazy {
public function __construct() {
parent::__construct(INPUT_POST);
}
}
class Headers extends Lazy {
static $headers = array(
'Accept' => 'Header',
'Accept-Charset' => 'Header',
'Accept-Language' => 'Header',
'Accept-Encoding' => 'Header',
'Connection' => 'Header',
'Cookie' => 'Header',
'Host' => 'Header',
'Referer' => 'Header',
'User-Agent' => 'Header',
'Content-Type' => 'Header',
'Content-Length' => 'Header',
);
public function __construct() {
parent::__construct(INPUT_SERVER);
}
protected function transformName($name) {
if (!array_key_exists($name, self::$headers)) {
throw new \InvalidArgumentException('Invalid header');
}
if (strpos($name, 'Content-') === 0) {
$key = substr($name, 8);
$key = 'CONTENT_' . strtoupper($key);
} else {
$key = 'HTTP_' . strtoupper(strtr($name, '-', '_'));
}
return $key;
}
protected function transformValue($name, $value) {
if (!array_key_exists($name, self::$headers)) {
throw new \InvalidArgumentException('Invalid header');
}
$class = self::$headers[$name];
$instance = new $class($name, $value);
return $instance;
}
}
class Header {
public $name;
public $value;
public function __construct($name, $value) {
$this->name = $name;
$this->value = $value;
}
}
class Request {
protected $query;
protected $form;
protected $headers;
public function __construct(Query $query, Form $form, Headers $headers) {
$this->query = $query;
$this->form = $form;
$this->headers = $headers;
}
public function __get($key) {
return $this->$key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment