From 7d3f4442b479361853bc03adfa67d41f188d3395 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 27 Jul 2014 16:52:59 -0400 Subject: [PATCH] Code style --- src/pjdietz/WellRESTed/Client.php | 3 +- src/pjdietz/WellRESTed/Handler.php | 2 +- .../Interfaces/HandlerInterface.php | 5 +- .../Interfaces/RequestInterface.php | 1 - src/pjdietz/WellRESTed/Request.php | 189 +++++++++--------- src/pjdietz/WellRESTed/Response.php | 6 +- src/pjdietz/WellRESTed/Router.php | 5 +- src/pjdietz/WellRESTed/Routes/BaseRoute.php | 2 +- 8 files changed, 106 insertions(+), 107 deletions(-) diff --git a/src/pjdietz/WellRESTed/Client.php b/src/pjdietz/WellRESTed/Client.php index c8416a1..82fa455 100644 --- a/src/pjdietz/WellRESTed/Client.php +++ b/src/pjdietz/WellRESTed/Client.php @@ -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 { diff --git a/src/pjdietz/WellRESTed/Handler.php b/src/pjdietz/WellRESTed/Handler.php index 1e41491..a1a81af 100644 --- a/src/pjdietz/WellRESTed/Handler.php +++ b/src/pjdietz/WellRESTed/Handler.php @@ -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. diff --git a/src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php b/src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php index ae170c3..e9aed1b 100644 --- a/src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php +++ b/src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php @@ -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); - } diff --git a/src/pjdietz/WellRESTed/Interfaces/RequestInterface.php b/src/pjdietz/WellRESTed/Interfaces/RequestInterface.php index e697232..edb1f4b 100644 --- a/src/pjdietz/WellRESTed/Interfaces/RequestInterface.php +++ b/src/pjdietz/WellRESTed/Interfaces/RequestInterface.php @@ -81,5 +81,4 @@ interface RequestInterface * @return string Request body */ public function getBody(); - } diff --git a/src/pjdietz/WellRESTed/Request.php b/src/pjdietz/WellRESTed/Request.php index 8518e86..034f344 100644 --- a/src/pjdietz/WellRESTed/Request.php +++ b/src/pjdietz/WellRESTed/Request.php @@ -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. * diff --git a/src/pjdietz/WellRESTed/Response.php b/src/pjdietz/WellRESTed/Response.php index 606c0ba..63194f9 100644 --- a/src/pjdietz/WellRESTed/Response.php +++ b/src/pjdietz/WellRESTed/Response.php @@ -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) { diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index 533b381..0bc8bb0 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -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) { diff --git a/src/pjdietz/WellRESTed/Routes/BaseRoute.php b/src/pjdietz/WellRESTed/Routes/BaseRoute.php index 1ece6d0..f943493 100644 --- a/src/pjdietz/WellRESTed/Routes/BaseRoute.php +++ b/src/pjdietz/WellRESTed/Routes/BaseRoute.php @@ -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; /**