started to cleanup requests/responses

This commit is contained in:
Dennis Eichhorn 2019-11-03 23:17:46 +01:00
parent 159decf760
commit 8995020117
6 changed files with 121 additions and 180 deletions

View File

@ -39,15 +39,7 @@ final class Header extends HeaderAbstract
private array $header = [];
/**
* Set header.
*
* @param string $key Header key (case insensitive)
* @param string $header Header value
* @param bool $overwrite Overwrite if already existing
*
* @return bool
*
* @since 1.0.0
* {@inheritdoc}
*/
public function set(string $key, string $header, bool $overwrite = false) : bool
{
@ -209,13 +201,7 @@ final class Header extends HeaderAbstract
}
/**
* Get header by name.
*
* @param string $key Header key
*
* @return array
*
* @since 1.0.0
* {@inheritdoc}
*/
public function get(string $key = null) : array
{
@ -223,13 +209,7 @@ final class Header extends HeaderAbstract
}
/**
* Check if header is defined.
*
* @param string $key Header key
*
* @return bool
*
* @since 1.0.0
* {@inheritdoc}
*/
public function has(string $key) : bool
{

View File

@ -33,6 +33,29 @@ use phpOMS\Uri\UriInterface;
*/
final class Request extends RequestAbstract
{
/**
* Uri.
*
* @var UriInterface
* @since 1.0.0
*/
protected UriInterface $uri;
/**
* Request method.
*
* @var null|string
* @since 1.0.0
*/
protected ?string $method = null;
/**
* Request type.
*
* @var null|string
* @since 1.0.0
*/
protected ?string $type = null;
/**
* Browser type.
@ -58,6 +81,22 @@ final class Request extends RequestAbstract
*/
private ?array $info = null;
/**
* Request hash.
*
* @var array
* @since 1.0.0
*/
protected array $hash = [];
/**
* Uploaded files.
*
* @var array
* @since 1.0.0
*/
protected array $files = [];
/**
* Constructor.
*
@ -154,6 +193,45 @@ final class Request extends RequestAbstract
}
}
/**
* Get request uri.
*
* @return UriInterface
*
* @since 1.0.0
*/
public function getUri() : UriInterface
{
return $this->uri;
}
/**
* Get request hash.
*
* @return array
*
* @since 1.0.0
*/
public function getHash() : array
{
return $this->hash;
}
/**
* Set request method.
*
* @param string $method Request method
*
* @return void
*
* @since 1.0.0
*/
public function setMethod(string $method) : void
{
$this->method = $method;
}
/**
* Get request language
*
@ -198,6 +276,8 @@ final class Request extends RequestAbstract
*
* @return void
*
* @todo: consider making this function static.
*
* @since 1.0.0
*/
private function cleanupGlobals() : void
@ -242,11 +322,17 @@ final class Request extends RequestAbstract
}
/**
* {@inheritdoc}
* Set request uri.
*
* @param UriInterface $uri Uri
*
* @return void
*
* @since 1.0.0
*/
public function setUri(UriInterface $uri) : void
{
parent::setUri($uri);
$this->uri = $uri;
$this->data += $uri->getQueryArray();
}
@ -406,6 +492,8 @@ final class Request extends RequestAbstract
*
* @throws \OutOfRangeException This exception is thrown if the port is out of range
*
* @todo: consider making this static
*
* @since 1.0.0
*/
public function isHttps(int $port = 443) : bool
@ -430,7 +518,11 @@ final class Request extends RequestAbstract
}
/**
* {@inheritdoc}
* Get route verb.
*
* @return int
*
* @since 1.0.0
*/
public function getRouteVerb() : int
{
@ -449,7 +541,11 @@ final class Request extends RequestAbstract
}
/**
* {@inheritdoc}
* Get request method.
*
* @return string
*
* @since 1.0.0
*/
public function getMethod() : string
{

View File

@ -32,14 +32,6 @@ use phpOMS\Views\View;
*/
final class Response extends ResponseAbstract implements RenderableInterface
{
/**
* Response status.
*
* @var int
* @since 1.0.0
*/
protected int $status = RequestStatusCode::R_200;
/**
* Constructor.
*

View File

@ -26,30 +26,6 @@ use phpOMS\Uri\UriInterface;
*/
abstract class RequestAbstract implements MessageInterface
{
/**
* Uri.
*
* @var UriInterface
* @since 1.0.0
*/
protected UriInterface $uri;
/**
* Request method.
*
* @var null|string
* @since 1.0.0
*/
protected ?string $method = null;
/**
* Request type.
*
* @var null|string
* @since 1.0.0
*/
protected ?string $type = null;
/**
* Request data.
*
@ -58,22 +34,6 @@ abstract class RequestAbstract implements MessageInterface
*/
protected array $data = [];
/**
* Request hash.
*
* @var array
* @since 1.0.0
*/
protected array $hash = [];
/**
* Uploaded files.
*
* @var array
* @since 1.0.0
*/
protected array $files = [];
/**
* Request lock.
*
@ -90,67 +50,6 @@ abstract class RequestAbstract implements MessageInterface
*/
protected HeaderAbstract $header;
/**
* Get request uri.
*
* @return UriInterface
*
* @since 1.0.0
*/
public function getUri() : UriInterface
{
return $this->uri;
}
/**
* Set request uri.
*
* @param UriInterface $uri Uri
*
* @return void
*
* @since 1.0.0
*/
public function setUri(UriInterface $uri) : void
{
$this->uri = $uri;
}
/**
* Get request hash.
*
* @return array
*
* @since 1.0.0
*/
public function getHash() : array
{
return $this->hash;
}
/**
* Get request method.
*
* @return string
*
* @since 1.0.0
*/
abstract public function getMethod() : string;
/**
* Set request method.
*
* @param string $method Request method
*
* @return void
*
* @since 1.0.0
*/
public function setMethod(string $method) : void
{
$this->method = $method;
}
/**
* Get data.
*
@ -309,7 +208,9 @@ abstract class RequestAbstract implements MessageInterface
}
/**
* {@inheritdoc}
* Get the origin request source (IPv4/IPv6)
*
* @return string
*/
abstract public function getOrigin() : string;
@ -321,15 +222,6 @@ abstract class RequestAbstract implements MessageInterface
return $this->uri->__toString();
}
/**
* Get route verb.
*
* @return int
*
* @since 1.0.0
*/
abstract public function getRouteVerb() : int;
/**
* Get files.
*

View File

@ -80,6 +80,21 @@ class ResponseTest extends \PHPUnit\Framework\TestCase
self::assertEquals(\json_encode($data), $response->render());
}
public function testMinimizedRender() : void
{
$response = new Response();
$response->set('view', new class() extends \phpOMS\Views\View {
public function render(...$data) : string
{
return " view_string with <div> text</div> that has \n whitespaces and \n\nnew lines\n ";
}
});
$response->getHeader()->set('Content-Type', MimeType::M_HTML . '; charset=utf-8', true);
self::assertEquals('view_string with <div> text</div> that has whitespaces and new lines', $response->render(true));
}
public function testInvalidResponseData() : void
{
$response = new Response();

View File

@ -1,34 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\tests\Message;
require_once __DIR__ . '/../Autoloader.php';
use phpOMS\Message\RequestSource;
/**
* @internal
*/
class RequestSourceTest extends \PHPUnit\Framework\TestCase
{
public function testEnums() : void
{
self::assertCount(4, RequestSource::getConstants());
self::assertEquals(0, RequestSource::WEB);
self::assertEquals(1, RequestSource::CONSOLE);
self::assertEquals(2, RequestSource::SOCKET);
self::assertEquals(3, RequestSource::UNDEFINED);
}
}