minor refactorings

pull/439/head
Masroor Ehsan 4 years ago
parent 5b8e743e3d
commit a2c5dd5ede

@ -143,7 +143,7 @@ final class Request
'method' => self::getMethod(), 'method' => self::getMethod(),
'referrer' => self::getVar('HTTP_REFERER'), 'referrer' => self::getVar('HTTP_REFERER'),
'ip' => self::getVar('REMOTE_ADDR'), 'ip' => self::getVar('REMOTE_ADDR'),
'ajax' => 'XMLHttpRequest' == self::getVar('HTTP_X_REQUESTED_WITH'), 'ajax' => 'XMLHttpRequest' === self::getVar('HTTP_X_REQUESTED_WITH'),
'scheme' => self::getScheme(), 'scheme' => self::getScheme(),
'user_agent' => self::getVar('HTTP_USER_AGENT'), 'user_agent' => self::getVar('HTTP_USER_AGENT'),
'type' => self::getVar('CONTENT_TYPE'), 'type' => self::getVar('CONTENT_TYPE'),
@ -152,7 +152,7 @@ final class Request
'data' => new Collection($_POST), 'data' => new Collection($_POST),
'cookies' => new Collection($_COOKIE), 'cookies' => new Collection($_COOKIE),
'files' => new Collection($_FILES), 'files' => new Collection($_FILES),
'secure' => 'https' == self::getScheme(), 'secure' => 'https' === self::getScheme(),
'accept' => self::getVar('HTTP_ACCEPT'), 'accept' => self::getVar('HTTP_ACCEPT'),
'proxy_ip' => self::getProxyIpAddress(), 'proxy_ip' => self::getProxyIpAddress(),
'host' => self::getVar('HTTP_HOST'), 'host' => self::getVar('HTTP_HOST'),
@ -175,27 +175,26 @@ final class Request
} }
// Get the requested URL without the base directory // Get the requested URL without the base directory
if ('/' != $this->base && \strlen($this->base) > 0 && 0 === strpos($this->url, $this->base)) { if ('/' !== $this->base && '' !== $this->base && 0 === strpos($this->url, $this->base)) {
$this->url = substr($this->url, \strlen($this->base)); $this->url = substr($this->url, \strlen($this->base));
} }
// Default url // Default url
if (empty($this->url)) { if (empty($this->url)) {
$this->url = '/'; $this->url = '/';
} } else {
// Merge URL query parameters with $_GET // Merge URL query parameters with $_GET
else { $_GET = array_merge($_GET, self::parseQuery($this->url));
$_GET += self::parseQuery($this->url);
$this->query->setData($_GET); $this->query->setData($_GET);
} }
// Check for JSON input // Check for JSON input
if (0 === strpos($this->type, 'application/json')) { if (0 === strpos($this->type, 'application/json')) {
$body = $this->getBody(); $body = self::getBody();
if ('' != $body) { if ('' !== $body) {
$data = json_decode($body, true); $data = json_decode($body, true);
if (null != $data) { if (null !== $data) {
$this->data->setData($data); $this->data->setData($data);
} }
} }

@ -10,6 +10,8 @@ declare(strict_types=1);
namespace flight\net; namespace flight\net;
use Exception;
/** /**
* The Response class represents an HTTP response. The object * The Response class represents an HTTP response. The object
* contains the response headers, HTTP status code, and response * contains the response headers, HTTP status code, and response
@ -19,15 +21,13 @@ class Response
{ {
/** /**
* header Content-Length. * header Content-Length.
*
* @var bool
*/ */
public $content_length = true; public bool $content_length = true;
/** /**
* @var array HTTP status codes * @var array HTTP status codes
*/ */
public static $codes = [ public static array $codes = [
100 => 'Continue', 100 => 'Continue',
101 => 'Switching Protocols', 101 => 'Switching Protocols',
102 => 'Processing', 102 => 'Processing',
@ -100,33 +100,33 @@ class Response
/** /**
* @var int HTTP status * @var int HTTP status
*/ */
protected $status = 200; protected int $status = 200;
/** /**
* @var array HTTP headers * @var array HTTP headers
*/ */
protected $headers = []; protected array $headers = [];
/** /**
* @var string HTTP response body * @var string HTTP response body
*/ */
protected $body; protected string $body;
/** /**
* @var bool HTTP response sent * @var bool HTTP response sent
*/ */
protected $sent = false; protected bool $sent = false;
/** /**
* Sets the HTTP status of the response. * Sets the HTTP status of the response.
* *
* @param int $code HTTP status code. * @param int|null $code HTTP status code.
* *
* @throws \Exception If invalid status code * @throws Exception If invalid status code
* *
* @return int|object Self reference * @return int|object Self reference
*/ */
public function status($code = null) public function status(?int $code = null)
{ {
if (null === $code) { if (null === $code) {
return $this->status; return $this->status;
@ -135,7 +135,7 @@ class Response
if (\array_key_exists($code, self::$codes)) { if (\array_key_exists($code, self::$codes)) {
$this->status = $code; $this->status = $code;
} else { } else {
throw new \Exception('Invalid status code.'); throw new Exception('Invalid status code.');
} }
return $this; return $this;
@ -145,11 +145,11 @@ class Response
* Adds a header to the response. * Adds a header to the response.
* *
* @param array|string $name Header name or array of names and values * @param array|string $name Header name or array of names and values
* @param string $value Header value * @param string|null $value Header value
* *
* @return object Self reference * @return object Self reference
*/ */
public function header($name, $value = null) public function header($name, ?string $value = null)
{ {
if (\is_array($name)) { if (\is_array($name)) {
foreach ($name as $k => $v) { foreach ($name as $k => $v) {
@ -177,9 +177,9 @@ class Response
* *
* @param string $str Response content * @param string $str Response content
* *
* @return object Self reference * @return Response Self reference
*/ */
public function write($str) public function write(string $str): self
{ {
$this->body .= $str; $this->body .= $str;
@ -189,9 +189,9 @@ class Response
/** /**
* Clears the response. * Clears the response.
* *
* @return object Self reference * @return Response Self reference
*/ */
public function clear() public function clear(): self
{ {
$this->status = 200; $this->status = 200;
$this->headers = []; $this->headers = [];
@ -205,9 +205,9 @@ class Response
* *
* @param int|string $expires Expiration time * @param int|string $expires Expiration time
* *
* @return object Self reference * @return Response Self reference
*/ */
public function cache($expires) public function cache($expires): self
{ {
if (false === $expires) { if (false === $expires) {
$this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; $this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
@ -232,9 +232,9 @@ class Response
/** /**
* Sends HTTP headers. * Sends HTTP headers.
* *
* @return object Self reference * @return Response Self reference
*/ */
public function sendHeaders() public function sendHeaders(): self
{ {
// Send status code header // Send status code header
if (false !== strpos(\PHP_SAPI, 'cgi')) { if (false !== strpos(\PHP_SAPI, 'cgi')) {
@ -250,7 +250,7 @@ class Response
header( header(
sprintf( sprintf(
'%s %d %s', '%s %d %s',
($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'), $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1',
$this->status, $this->status,
self::$codes[$this->status]), self::$codes[$this->status]),
true, true,
@ -284,9 +284,9 @@ class Response
/** /**
* Gets the content length. * Gets the content length.
* *
* @return string Content length * @return int Content length
*/ */
public function getContentLength() public function getContentLength(): int
{ {
return \extension_loaded('mbstring') ? return \extension_loaded('mbstring') ?
mb_strlen($this->body, 'latin1') : mb_strlen($this->body, 'latin1') :
@ -296,7 +296,7 @@ class Response
/** /**
* Gets whether response was sent. * Gets whether response was sent.
*/ */
public function sent() public function sent(): bool
{ {
return $this->sent; return $this->sent;
} }
@ -304,7 +304,7 @@ class Response
/** /**
* Sends a HTTP response. * Sends a HTTP response.
*/ */
public function send() public function send(): void
{ {
if (ob_get_length() > 0) { if (ob_get_length() > 0) {
ob_end_clean(); ob_end_clean();

Loading…
Cancel
Save