Code style fixes

This commit is contained in:
Dmitry Khomutov 2016-04-20 21:39:48 +06:00
commit 0868eb9c69
63 changed files with 1413 additions and 1494 deletions

View file

@ -5,18 +5,18 @@ namespace b8\Http;
class Request
{
/**
* @var array
*/
protected $params = array();
* @var array
*/
protected $params = [];
/**
* Request data.
*/
protected $data = array();
* Request data.
*/
protected $data = [];
/**
* Set up the request.
*/
* Set up the request.
*/
public function __construct()
{
$this->parseInput();
@ -40,7 +40,7 @@ class Request
}
// Remove index.php from the URL if it is present:
$path = str_replace(array('/index.php', 'index.php'), '', $path);
$path = str_replace(['/index.php', 'index.php'], '', $path);
// Also cut out the query string:
$path = explode('?', $path);
@ -50,22 +50,20 @@ class Request
}
/**
* Parse incoming variables, incl. $_GET, $_POST and also reads php://input for PUT/DELETE.
*/
* Parse incoming variables, incl. $_GET, $_POST and also reads php://input for PUT/DELETE.
*/
protected function parseInput()
{
$params = $_REQUEST;
if(!isset($_SERVER['REQUEST_METHOD']) || in_array($_SERVER['REQUEST_METHOD'], array('PUT', 'DELETE')))
{
if (!isset($_SERVER['REQUEST_METHOD']) || in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'DELETE'])) {
$vars = file_get_contents('php://input');
if(!is_string($vars) || strlen(trim($vars)) === 0)
{
if (!is_string($vars) || strlen(trim($vars)) === 0) {
$vars = '';
}
$inputData = array();
$inputData = [];
parse_str($vars, $inputData);
$params = array_merge($params, $inputData);
@ -75,17 +73,17 @@ class Request
}
/**
* Returns all request parameters.
* @return array
*/
* Returns all request parameters.
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Return a specific request parameter, or a default value if not set.
*/
* Return a specific request parameter, or a default value if not set.
*/
public function getParam($key, $default = null)
{
if (isset($this->params[$key])) {
@ -96,24 +94,24 @@ class Request
}
/**
* Set or override a request parameter.
*/
* Set or override a request parameter.
*/
public function setParam($key, $value = null)
{
$this->params[$key] = $value;
}
/**
* Set an array of request parameters.
*/
* Set an array of request parameters.
*/
public function setParams(array $params)
{
$this->params = array_merge($this->params, $params);
}
/**
* Un-set a specific parameter.
*/
* Un-set a specific parameter.
*/
public function unsetParam($key)
{
unset($this->params[$key]);

View file

@ -4,129 +4,128 @@ namespace b8\Http;
class Response
{
protected $data = array();
protected $data = [];
public function __construct(Response $createFrom = null)
{
if (!is_null($createFrom)) {
$this->data = $createFrom->getData();
}
}
public function __construct(Response $createFrom = null)
{
if (!is_null($createFrom)) {
$this->data = $createFrom->getData();
}
}
public function hasLayout()
{
return !isset($this->data['layout']) ? true : $this->data['layout'];
}
public function hasLayout()
{
return !isset($this->data['layout']) ? true : $this->data['layout'];
}
public function disableLayout()
{
$this->data['layout'] = false;
}
public function disableLayout()
{
$this->data['layout'] = false;
}
public function enableLayout()
{
$this->data['layout'] = true;
}
public function enableLayout()
{
$this->data['layout'] = true;
}
public function getData()
{
return $this->data;
}
public function getData()
{
return $this->data;
}
public function setResponseCode($code)
{
$this->data['code'] = (int)$code;
}
public function setResponseCode($code)
{
$this->data['code'] = (int)$code;
}
public function setHeader($key, $val)
{
$this->data['headers'][$key] = $val;
}
public function setHeader($key, $val)
{
$this->data['headers'][$key] = $val;
}
public function clearHeaders()
{
$this->data['headers'] = array();
}
public function clearHeaders()
{
$this->data['headers'] = [];
}
public function setContent($content)
{
$this->data['body'] = $content;
}
public function setContent($content)
{
$this->data['body'] = $content;
}
public function getContent()
{
return $this->data['body'];
}
public function getContent()
{
return $this->data['body'];
}
public function flush()
{
$this->sendResponseCode();
public function flush()
{
$this->sendResponseCode();
if (isset($this->data['headers'])) {
foreach ($this->data['headers'] as $header => $val) {
header($header . ': ' . $val, true);
}
}
return $this->flushBody();
}
if (isset($this->data['headers'])) {
foreach ($this->data['headers'] as $header => $val) {
header($header . ': ' . $val, true);
}
}
protected function sendResponseCode()
{
if (!isset($this->data['code'])) {
$this->data['code'] = 200;
}
return $this->flushBody();
}
switch ($this->data['code'])
{
// 300 class
case 301:
$text = 'Moved Permanently';
break;
case 302:
$text = 'Moved Temporarily';
break;
protected function sendResponseCode()
{
if (!isset($this->data['code'])) {
$this->data['code'] = 200;
}
// 400 class errors
case 400:
$text = 'Bad Request';
break;
case 401:
$text = 'Not Authorized';
break;
case 403:
$text = 'Forbidden';
break;
case 404:
$text = 'Not Found';
break;
switch ($this->data['code']) {
// 300 class
case 301:
$text = 'Moved Permanently';
break;
case 302:
$text = 'Moved Temporarily';
break;
// 500 class errors
case 500:
$text = 'Internal Server Error';
break;
// 400 class errors
case 400:
$text = 'Bad Request';
break;
case 401:
$text = 'Not Authorized';
break;
case 403:
$text = 'Forbidden';
break;
case 404:
$text = 'Not Found';
break;
// OK
case 200:
default:
$text = 'OK';
break;
}
// 500 class errors
case 500:
$text = 'Internal Server Error';
break;
header('HTTP/1.1 ' . $this->data['code'] . ' ' . $text, true, $this->data['code']);
}
// OK
case 200:
default:
$text = 'OK';
break;
}
protected function flushBody()
{
if (isset($this->data['body'])) {
return $this->data['body'];
}
header('HTTP/1.1 ' . $this->data['code'] . ' ' . $text, true, $this->data['code']);
}
return '';
}
protected function flushBody()
{
if (isset($this->data['body'])) {
return $this->data['body'];
}
public function __toString()
{
return $this->flush();
}
return '';
}
public function __toString()
{
return $this->flush();
}
}

View file

@ -6,25 +6,25 @@ use b8\Http\Response;
class JsonResponse extends Response
{
public function __construct(Response $createFrom = null)
{
parent::__construct($createFrom);
public function __construct(Response $createFrom = null)
{
parent::__construct($createFrom);
$this->setContent(array());
$this->setHeader('Content-Type', 'application/json');
}
$this->setContent([]);
$this->setHeader('Content-Type', 'application/json');
}
public function hasLayout()
{
return false;
}
public function hasLayout()
{
return false;
}
protected function flushBody()
{
if (isset($this->data['body'])) {
return json_encode($this->data['body']);
}
protected function flushBody()
{
if (isset($this->data['body'])) {
return json_encode($this->data['body']);
}
return json_encode(null);
}
return json_encode(null);
}
}

View file

@ -26,7 +26,7 @@ class Router
/**
* @var array
*/
protected $routes = array(array('route' => '/:controller/:action', 'callback' => null, 'defaults' => array()));
protected $routes = [['route' => '/:controller/:action', 'callback' => null, 'defaults' => []]];
public function __construct(Application $application, Request $request, Config $config)
{
@ -37,7 +37,7 @@ class Router
public function clearRoutes()
{
$this->routes = array();
$this->routes = [];
}
/**
@ -46,13 +46,13 @@ class Router
* @param callable $callback
* @throws \InvalidArgumentException
*/
public function register($route, $options = array(), $callback = null)
public function register($route, $options = [], $callback = null)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('$callback must be callable.');
}
array_unshift($this->routes, array('route' => $route, 'callback' => $callback, 'defaults' => $options));
array_unshift($this->routes, ['route' => $route, 'callback' => $callback, 'defaults' => $options]);
}
public function dispatch()
@ -110,7 +110,13 @@ class Router
$thisArgs = $pathParts;
if ($routeMatches) {
$route = array('namespace' => $thisNamespace, 'controller' => $thisController, 'action' => $thisAction, 'args' => $thisArgs, 'callback' => $route['callback']);
$route = [
'namespace' => $thisNamespace,
'controller' => $thisController,
'action' => $thisAction,
'args' => $thisArgs,
'callback' => $route['callback']
];
if ($this->application->isValidRoute($route)) {
return $route;