Code style

This commit is contained in:
PJ Dietz 2014-07-27 16:52:59 -04:00
parent d1a77c5454
commit 7d3f4442b4
8 changed files with 106 additions and 107 deletions

View File

@ -30,7 +30,8 @@ class Client
*
* @param array $curlOpts Optional array of cURL options
*/
public function __construct(array $curlOpts = null) {
public function __construct(array $curlOpts = null)
{
if (is_array($curlOpts)) {
$this->curlOpts = $curlOpts;
} else {

View File

@ -12,8 +12,8 @@ namespace pjdietz\WellRESTed;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\HttpException;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
/**
* Responds to a request based on the HTTP method.

View File

@ -13,8 +13,8 @@ namespace pjdietz\WellRESTed\Interfaces;
/**
* Provides a mechanism for obtaining a response given a request.
*/
interface HandlerInterface {
interface HandlerInterface
{
/**
* Return the handled response.
*
@ -23,5 +23,4 @@ interface HandlerInterface {
* @return ResponseInterface The handled response.
*/
public function getResponse(RequestInterface $request, array $args = null);
}

View File

@ -81,5 +81,4 @@ interface RequestInterface
* @return string Request body
*/
public function getBody();
}

View File

@ -11,7 +11,6 @@
namespace pjdietz\WellRESTed;
use InvalidArgumentException;
use pjdietz\WellRESTed\Exceptions\CurlException;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use UnexpectedValueException;
@ -36,15 +35,15 @@ class Request extends Message implements RequestInterface
static protected $theRequest;
/** @var string HTTP method or verb for the request */
private $method = "GET";
/** @var string Scheme for the request (Must be "http" or "https" */
private $scheme;
/** @var string The Hostname for the request (e.g., www.google.com) */
private $hostname = "localhost";
/** @var string Scheme for the request (Must be "http" or "https" */
protected $scheme;
/** @var string Path component of the URI for the request */
private $path = '/';
private $path = "/";
/** @var array Array of fragments of the path, delimited by slashes */
private $pathParts;
/** @var int HTTP Port*/
/** @var int HTTP Port */
private $port = 80;
/** @var array Associative array of query parameters */
private $query;
@ -115,9 +114,82 @@ class Request extends Message implements RequestInterface
$this->headerLookup[strtolower($key)] = $key;
}
$this->setMethod($_SERVER['REQUEST_METHOD']);
$this->setUri($_SERVER['REQUEST_URI']);
$this->setHostname($_SERVER['HTTP_HOST']);
$this->setMethod($_SERVER["REQUEST_METHOD"]);
$this->setUri($_SERVER["REQUEST_URI"]);
$this->setHostname($_SERVER["HTTP_HOST"]);
}
/**
* Return the method (e.g., GET, POST, PUT, DELETE)
*
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* Assign the method (e.g., GET, POST, PUT, DELETE)
*
* @param string $method
*/
public function setMethod($method)
{
$this->method = $method;
}
/**
* Return the full URI includeing protocol, hostname, path, and query.
*
* @return array
*/
public function getUri()
{
$uri = $this->scheme . "://" . $this->hostname;
if ($this->port !== $this->getDefaultPort()) {
$uri .= ":" . $this->port;
}
if ($this->path !== "/") {
$uri .= $this->path;
}
if ($this->query) {
$uri .= "?" . http_build_query($this->query);
}
return $uri;
}
/**
* Set the URI for the Request. This sets the other members: hostname,
* path, port, and query.
*
* @param string $uri
*/
public function setUri($uri)
{
// Provide http and localhost if missing.
if ($uri[0] === "/") {
$uri = "http://localhost" . $uri;
} elseif (strpos($uri, "://") === false) {
$uri = "http://" . $uri;
}
$parsed = parse_url($uri);
$scheme = isset($parsed["scheme"]) ? $parsed["scheme"] : "http";
$this->setScheme($scheme);
$host = isset($parsed["host"]) ? $parsed["host"] : "localhost";
$this->setHostname($host);
$port = isset($parsed["port"]) ? (int) $parsed["port"] : $this->getDefaultPort();
$this->setPort($port);
$path = isset($parsed["path"]) ? $parsed["path"] : "/";
$this->setPath($path);
$query = isset($parsed["query"]) ? $parsed["query"] : "";
$this->setQuery($query);
}
/**
@ -141,23 +213,28 @@ class Request extends Message implements RequestInterface
}
/**
* Return the method (e.g., GET, POST, PUT, DELETE)
* Set the scheme for the request (either "http" or "https")
*
* @return string
* @param string $scheme
* @throws \UnexpectedValueException
*/
public function getMethod()
public function setScheme($scheme)
{
return $this->method;
$scheme = strtolower($scheme);
if (!in_array($scheme, array("http", "https"))) {
throw new UnexpectedValueException('Scheme must be "http" or "https".');
}
$this->scheme = $scheme;
}
/**
* Assign the method (e.g., GET, POST, PUT, DELETE)
* Return the scheme for the request (either "http" or "https")
*
* @param string $method
* @return string
*/
public function setMethod($method)
public function getScheme()
{
$this->method = $method;
return $this->scheme;
}
/**
@ -248,88 +325,10 @@ class Request extends Message implements RequestInterface
ksort($query);
$this->query = $query;
} else {
throw new InvalidArgumentException('Unable to parse query string.');
throw new InvalidArgumentException("Unable to parse query string.");
}
}
/**
* Set the scheme for the request (either "http" or "https")
*
* @param string $scheme
* @throws \UnexpectedValueException
*/
public function setScheme($scheme)
{
$scheme = strtolower($scheme);
if (!in_array($scheme, array("http","https"))) {
throw new UnexpectedValueException('Scheme must be "http" or "https".');
}
$this->scheme = $scheme;
}
/**
* Return the scheme for the request (either "http" or "https")
*
* @return string
*/
public function getScheme()
{
return $this->scheme;
}
/**
* Return the full URI includeing protocol, hostname, path, and query.
*
* @return array
*/
public function getUri()
{
$uri = $this->scheme . "://" . $this->hostname;
if ($this->port !== $this->getDefaultPort()) {
$uri .= ':' . $this->port;
}
if ($this->path !== "/") {
$uri .= $this->path;
}
if ($this->query) {
$uri .= '?' . http_build_query($this->query);
}
return $uri;
}
/**
* Set the URI for the Request. This sets the other members: hostname,
* path, port, and query.
*
* @param string $uri
*/
public function setUri($uri)
{
// Provide http and localhost if missing.
if ($uri[0] === "/") {
$uri = "http://localhost" . $uri;
} elseif (strpos($uri, "://") === false) {
$uri = "http://" . $uri;
}
$parsed = parse_url($uri);
$scheme = isset($parsed["scheme"]) ? $parsed["scheme"] : "http";
$this->setScheme($scheme);
$host = isset($parsed['host']) ? $parsed['host'] : 'localhost';
$this->setHostname($host);
$port = isset($parsed['port']) ? (int) $parsed['port'] : $this->getDefaultPort();
$this->setPort($port);
$path = isset($parsed['path']) ? $parsed['path'] : '/';
$this->setPath($path);
$query = isset($parsed['query']) ? $parsed['query'] : '';
$this->setQuery($query);
}
/**
* Return the form fields for this request.
*

View File

@ -33,7 +33,7 @@ class Response extends Message implements ResponseInterface
private $reasonPhrase;
/** @var int HTTP status code */
private $statusCode;
/** @var string HTTP protocol and version*/
/** @var string HTTP protocol and version */
private $protocol = "HTTP/1.1";
// -------------------------------------------------------------------------
@ -72,7 +72,7 @@ class Response extends Message implements ResponseInterface
* of the new body string.
*
* @param string $value
* @param bool $setContentLength Automatically add a Content-length header
* @param bool $setContentLength Automatically add a Content-length header
*/
public function setBody($value, $setContentLength = true)
{
@ -289,7 +289,7 @@ class Response extends Message implements ResponseInterface
/**
* Output the response to the client.
*
* @param bool $headersOnly Do not include the body, only the headers.
* @param bool $headersOnly Do not include the body, only the headers.
*/
public function respond($headersOnly = false)
{

View File

@ -11,8 +11,8 @@
namespace pjdietz\WellRESTed;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
/**
* Router
@ -78,7 +78,8 @@ class Router implements HandlerInterface
*
* Respond with a 404 Not Found if no route provides a response.
*/
public function respond() {
public function respond()
{
$request = Request::getRequest();
$response = $this->getResponse($request);
if (!$response) {

View File

@ -20,7 +20,7 @@ abstract class BaseRoute implements HandlerInterface
/** @var string Fully qualified name for the interface for handlers */
const HANDLER_INTERFACE = '\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface';
/** @var string Fully qualified classname of the HandlerInterface to dispatch*/
/** @var string Fully qualified classname of the HandlerInterface to dispatch */
private $targetClassName;
/**