From f3aeec1d59a4e81647cdfaf9fbad44e0ee718dc9 Mon Sep 17 00:00:00 2001 From: Nicholas Ruunu Date: Thu, 4 Jan 2018 14:18:52 +0100 Subject: [PATCH] Refactor to response envelope --- src/Response/Response.php | 30 ++++---- src/Response/TextResponse.php | 132 +--------------------------------- 2 files changed, 17 insertions(+), 145 deletions(-) diff --git a/src/Response/Response.php b/src/Response/Response.php index 7035104..99c1230 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -7,7 +7,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; -final class Response implements ResponseInterface +class Response implements ResponseInterface { private $statusCode; private $reasonPhrase; @@ -26,7 +26,7 @@ public function __construct( /** * @inheritdoc */ - public function getProtocolVersion(): string + final public function getProtocolVersion(): string { return $this->message->getProtocolVersion(); } @@ -34,7 +34,7 @@ public function getProtocolVersion(): string /** * @inheritdoc */ - public function withProtocolVersion($version): self + final public function withProtocolVersion($version): self { return new self( $this->message->withProtocolVersion($version), @@ -46,7 +46,7 @@ public function withProtocolVersion($version): self /** * @inheritdoc */ - public function getHeaders(): array + final public function getHeaders(): array { return $this->message->getHeaders(); } @@ -54,7 +54,7 @@ public function getHeaders(): array /** * @inheritdoc */ - public function hasHeader($name): bool + final public function hasHeader($name): bool { return $this->message->hasHeader($name); } @@ -62,7 +62,7 @@ public function hasHeader($name): bool /** * @inheritdoc */ - public function getHeader($name): array + final public function getHeader($name): array { return $this->message->getHeader($name); } @@ -70,7 +70,7 @@ public function getHeader($name): array /** * @inheritdoc */ - public function getHeaderLine($name): string + final public function getHeaderLine($name): string { return $this->message->getHeaderLine($name); } @@ -78,7 +78,7 @@ public function getHeaderLine($name): string /** * @inheritdoc */ - public function withHeader($name, $value): self + final public function withHeader($name, $value): self { return new self( $this->message->withHeader($name, $value), @@ -90,7 +90,7 @@ public function withHeader($name, $value): self /** * @inheritdoc */ - public function withAddedHeader($name, $value): self + final public function withAddedHeader($name, $value): self { return new self( $this->message->withAddedHeader($name, $value), @@ -102,7 +102,7 @@ public function withAddedHeader($name, $value): self /** * @inheritdoc */ - public function withoutHeader($name): self + final public function withoutHeader($name): self { return new self( $this->message->withoutHeader($name), @@ -114,7 +114,7 @@ public function withoutHeader($name): self /** * @inheritdoc */ - public function getBody(): StreamInterface + final public function getBody(): StreamInterface { return $this->message->getBody(); } @@ -122,7 +122,7 @@ public function getBody(): StreamInterface /** * @inheritdoc */ - public function withBody(StreamInterface $body): self + final public function withBody(StreamInterface $body): self { return $this->message->withBody($body); } @@ -130,7 +130,7 @@ public function withBody(StreamInterface $body): self /** * @inheritdoc */ - public function getStatusCode(): int + final public function getStatusCode(): int { return $this->statusCode; } @@ -138,7 +138,7 @@ public function getStatusCode(): int /** * @inheritdoc */ - public function withStatus($code, $reasonPhrase = ''): self + final public function withStatus($code, $reasonPhrase = ''): self { return new self( $this->message, @@ -150,7 +150,7 @@ public function withStatus($code, $reasonPhrase = ''): self /** * @inheritdoc */ - public function getReasonPhrase(): string + final public function getReasonPhrase(): string { return $this->reasonPhrase; } diff --git a/src/Response/TextResponse.php b/src/Response/TextResponse.php index fffcce4..4535f8d 100644 --- a/src/Response/TextResponse.php +++ b/src/Response/TextResponse.php @@ -3,145 +3,17 @@ namespace Purist\Http\Response; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\StreamInterface; use Purist\Http\Header\Headers; use Purist\Http\Message; use Purist\Http\Stream\LazyReadOnlyTextStream; -final class TextResponse implements ResponseInterface +final class TextResponse extends Response { - private $response; - public function __construct(string $text, int $statusCode = 200, Headers $headers = null) { - $this->response = new Response( + parent::__construct( new Message(new LazyReadOnlyTextStream($text), $headers), $statusCode ); } - - /** - * @inheritdoc - */ - public function getProtocolVersion(): string - { - return $this->response->getProtocolVersion(); - } - - /** - * @inheritdoc - */ - public function withProtocolVersion($version): self - { - $response = clone $this; - $response->response = $this->response->withProtocolVersion($version); - return $response; - } - - /** - * @inheritdoc - */ - public function getHeaders(): array - { - return $this->response->getHeaders(); - } - - /** - * @inheritdoc - */ - public function hasHeader($name): bool - { - return $this->response->hasHeader($name); - } - - /** - * @inheritdoc - */ - public function getHeader($name): array - { - return $this->response->getHeader($name); - } - - /** - * @inheritdoc - */ - public function getHeaderLine($name): string - { - return $this->response->getHeaderLine($name); - } - - /** - * @inheritdoc - */ - public function withHeader($name, $value): self - { - $response = clone $this; - $response->response = $this->response->withHeader($name, $value); - return $response; - } - - /** - * @inheritdoc - */ - public function withAddedHeader($name, $value): self - { - $response = clone $this; - $response->response = $this->response->withAddedHeader($name, $value); - return $response; - } - - /** - * @inheritdoc - */ - public function withoutHeader($name): self - { - $response = clone $this; - $response->response = $this->response->withoutHeader($name); - return $response; - } - - /** - * @inheritdoc - */ - public function getBody(): StreamInterface - { - return $this->response->getBody(); - } - - /** - * @inheritdoc - */ - public function withBody(StreamInterface $body) - { - $response = clone $this; - $response->response = $this->response->withBody($body); - return $response; - } - - /** - * @inheritdoc - */ - public function getStatusCode(): int - { - return $this->response->getStatusCode(); - } - - /** - * @inheritdoc - */ - public function withStatus($code, $reasonPhrase = '') - { - $response = clone $this; - $response->response = $this->response->withStatus($code, $reasonPhrase); - return $response; - } - - /** - * @inheritdoc - */ - public function getReasonPhrase(): string - { - return $this->response->getReasonPhrase(); - } }