Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace Rodas\Diactoros;

use InvalidArgumentException;
use Override;
use Rodas\Psr\Http\Message\MessageInterface;
use Rodas\Psr\Http\Message\StreamInterface;

Expand Down Expand Up @@ -71,7 +72,6 @@ trait MessageTrait {
set => $this->body = $value;
}


/**
* Return an instance with the specified HTTP protocol version.
*
Expand All @@ -85,8 +85,7 @@ trait MessageTrait {
* @param string $version HTTP protocol version
* @return static
*/
public function withProtocolVersion(string $version): MessageInterface
{
public function withProtocolVersion(string $version): MessageInterface {
$this->validateProtocolVersion($version);
$new = clone $this;
$new->protocolVersion = $version;
Expand Down Expand Up @@ -152,17 +151,16 @@ public function getHeaderLine(string $name): string {
public function withHeader(string $name, $value): MessageInterface {
$this->assertHeader($name);

$normalized = strtolower($name);

$new = clone $this;
$normalized = strtolower($name);
$new = clone $this;
$newHeaders = $new->headers;
if ($new->hasHeader($name)) {
unset($new->headers[$new->headerNames[$normalized]]);
unset($newHeaders[$new->headerNames[$normalized]]);
}

$value = $this->filterHeaderValue($value);

$new->headerNames[$normalized] = $name;
$new->headers[$name] = $value;
$newHeaders[$name] = $this->filterHeaderValue($value);
$new->headers = $newHeaders;

return $new;
}
Expand Down Expand Up @@ -193,9 +191,11 @@ public function withAddedHeader(string $name, $value): MessageInterface {

$header = $this->headerNames[strtolower($name)];

$new = clone $this;
$value = $this->filterHeaderValue($value);
$new->headers[$header] = array_merge($this->headers[$header], $value);
$new = clone $this;
$value = $this->filterHeaderValue($value);
$newHeaders = $new->headers;
$newHeaders[$header] = array_merge($this->headers[$header], $value);
$new->headers = $newHeaders;
return $new;
}

Expand All @@ -220,7 +220,9 @@ public function withoutHeader(string $name): MessageInterface {
$original = $this->headerNames[$normalized];

$new = clone $this;
unset($new->headers[$original], $new->headerNames[$normalized]);
$newHeaders = $new->headers;
unset($newHeaders[$original], $new->headerNames[$normalized]);
$new->headers = $newHeaders;
return $new;
}

Expand Down
4 changes: 2 additions & 2 deletions src/RelativeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(private readonly StreamInterface $decoratedStream, ?
*/
#[Override]
public function __toString(): string {
if ($this->isSeekable()) {
if ($this->isSeekable) {
$this->seek(0);
}
return $this->getContents();
Expand All @@ -69,7 +69,7 @@ public function detach() {
*/
public ?int $size {
get {
$size = $this->decoratedStream->getSize();
$size = $this->decoratedStream->size;
if ($size === null) {
return null;
}
Expand Down
12 changes: 7 additions & 5 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
namespace Rodas\Diactoros;

use InvalidArgumentException;
use Override;
use Rodas\Psr\Http\Message\RequestInterface;
use Rodas\Psr\Http\Message\StatusCode;
use Rodas\Psr\Http\Message\RequestMethod;
use Rodas\Psr\Http\Message\StreamInterface;
use Rodas\Psr\Http\Message\UriInterface;

Expand All @@ -44,7 +43,7 @@ class Request implements RequestInterface {
* @param array<non-empty-string, string|string[]> $headers Headers for the message, if any.
* @throws InvalidArgumentException For any invalid value.
*/
public function __construct($uri = null, ?string $method = null, $body = 'php://temp', array $headers = []) {
public function __construct(UriInterface|string|null $uri = null, RequestMethod|string|null $method = null, $body = 'php://temp', array $headers = []) {
$this->initialize($uri, $method, $body, $headers);
}

Expand All @@ -55,13 +54,16 @@ public function __construct($uri = null, ?string $method = null, $body = 'php://
*/
public protected(set) array $headers = [] {
get {
$headers = $this->headers;
if (! $this->hasHeader('host') &&
$this->uri->host) {

$headers = $this->headers;
$headers['Host'] = [$this->getHostFromUri()];
$this->headerNames['host'] = 'Host';
$this->headers = $headers;
}

return $headers;
return $this->headers;
}
set => $this->headers = $value;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Request/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ final class ArraySerializer {
*/
public static function toArray(RequestInterface $request): array {
return [
'method' => $request->getMethod(),
'request_target' => $request->getRequestTarget(),
'uri' => (string) $request->getUri(),
'protocol_version' => $request->getProtocolVersion(),
'headers' => $request->getHeaders(),
'body' => (string) $request->getBody(),
'method' => $request->method,
'request_target' => $request->requestTarget,
'uri' => (string) $request->uri,
'protocol_version' => $request->protocolVersion,
'headers' => $request->headers,
'body' => (string) $request->body,
];
}

Expand Down
10 changes: 5 additions & 5 deletions src/Request/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public static function fromStream(StreamInterface $stream): Request {
* Serialize a request message to a string.
*/
public static function toString(RequestInterface $request): string {
$httpMethod = $request->getMethod();
$headers = self::serializeHeaders($request->getHeaders());
$body = (string) $request->getBody();
$httpMethod = $request->method;
$headers = self::serializeHeaders($request->headers);
$body = (string) $request->body;
$format = '%s %s HTTP/%s%s%s';

if (! empty($headers)) {
Expand All @@ -94,8 +94,8 @@ public static function toString(RequestInterface $request): string {
return sprintf(
$format,
$httpMethod,
$request->getRequestTarget(),
$request->getProtocolVersion(),
$request->requestTarget,
$request->protocolVersion,
$headers,
$body
);
Expand Down
4 changes: 2 additions & 2 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

use Override;
use Rodas\Psr\Http\Message\RequestFactoryInterface;
use Rodas\Psr\Http\Message\RequestInterface;
use Rodas\Psr\Http\Message\UriInterface;

class RequestFactory implements RequestFactoryInterface {
/**
* {@inheritDoc}
*/
#[Override]
public function createRequest(string $method, $uri): RequestInterface {
public function createRequest(string $method, string|UriInterface $uri): Request {
return new Request($uri, $method);
}
}
36 changes: 19 additions & 17 deletions src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ trait RequestTrait {
set => $this->requestTarget = $value;
}

public private(set) StreamInterface $stream {
get => $this->stream;
set => $this->stream = $value;
}

/**
* Gets the URI instance.
*
Expand All @@ -131,7 +126,7 @@ trait RequestTrait {
* @throws InvalidArgumentException For any invalid value.
*/
private function initialize(
$uri = null,
UriInterface|string|null $uri = null,
RequestMethod|string|null $method = null,
$body = 'php://memory',
array $headers = []
Expand All @@ -140,16 +135,20 @@ private function initialize(
$this->setMethod($method);
}

$this->uri = $this->createUri($uri);
$this->stream = $this->getStream($body, 'wb+');
$this->uri = $this->createUri($uri);
$this->body = $this->getStream($body, 'wb+');

$this->setHeaders($headers);

// per PSR-7: attempt to set the Host header from a provided URI if no
// Host header is provided
if (! $this->hasHeader('Host') && $this->uri->host) {
$this->headerNames['host'] = 'Host';
$this->headers['Host'] = [$this->getHostFromUri()];
if (! $this->hasHeader('Host') &&
$this->uri->host) {

$this->headerNames['host'] = 'Host';
$headers = $this->headers;
$headers['Host'] = [$this->getHostFromUri()];
$this->headers = $headers;
}
}

Expand All @@ -167,7 +166,7 @@ private function initialize(
*
* @throws InvalidArgumentException
*/
private function createUri(null|string|UriInterface $uri): UriInterface {
private function createUri(UriInterface|string|null $uri): UriInterface {
if ($uri instanceof UriInterface) {
return $uri;
}
Expand Down Expand Up @@ -279,14 +278,15 @@ public function withUri(UriInterface $uri, bool $preserveHost = false): RequestI
// Remove an existing host header if present, regardless of current
// de-normalization of the header name.
// @see https://github.com/zendframework/zend-diactoros/issues/91
foreach (array_keys($new->headers) as $header) {
$newHeaders = $new->headers;
foreach (array_keys($newHeaders) as $header) {
if (strtolower($header) === 'host') {
unset($new->headers[$header]);
unset($newHeaders[$header]);
}
}

$new->headers['Host'] = [$host];

$newHeaders['Host'] = [$host];
$new->headers = $newHeaders;
return $new;
}

Expand Down Expand Up @@ -314,7 +314,9 @@ private function setMethod(RequestMethod|string $method): void {
*/
private function getHostFromUri(): string {
$host = $this->uri->host;
$host .= $this->uri->port !== null ? ':' . $this->uri->port : '';
$host .= $this->uri->port !== null
? ':' . $this->uri->port
: '';
return $host;
}
}
20 changes: 10 additions & 10 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class Response implements ResponseInterface {
set => $this->reasonPhrase = $value;
}

public private(set) int $status {
public private(set) int $status = 200 {
get => $this->status;
set(int $value) {
if ($value < static::MIN_STATUS_CODE_VALUE ||
Expand All @@ -170,6 +170,7 @@ class Response implements ResponseInterface {
} else {
$statusCode = StatusCode::tryFrom($value);
if ($this->statusCode !== $statusCode) {

$this->statusCode = $statusCode;
}
}
Expand All @@ -180,16 +181,15 @@ class Response implements ResponseInterface {
/**
* {@inheritdoc}
*/
public private(set) ?StatusCode $statusCode {
public private(set) ?StatusCode $statusCode = StatusCode::OK {
get => $this->statusCode;
set(?StatusCode $value) {
if ($value === null) {
throw new InvalidArgumentException('Status code cannot be null');
}
$this->statusCode = $value;
$code = $statusCode->value;
if ($this->status !== $code) {
$this->status = $code;
$this->statusCode = $value;
if ($value !== null) {
$code = $value->value;
if ($this->status !== $code) {
$this->status = $code;
}
}
}
}
Expand All @@ -202,7 +202,7 @@ class Response implements ResponseInterface {
*/
public function __construct($body = 'php://memory', int $status = 200, array $headers = []) {
$this->setStatusCode($status);
$this->stream = $this->getStream($body, 'wb+');
$this->body = $this->getStream($body, 'wb+');
$this->setHeaders($headers);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Response/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ final class ArraySerializer {
*/
public static function toArray(ResponseInterface $response): array {
return [
'status_code' => $response->getStatusCode(),
'reason_phrase' => $response->getReasonPhrase(),
'protocol_version' => $response->getProtocolVersion(),
'headers' => $response->getHeaders(),
'body' => (string) $response->getBody(),
'status_code' => $response->status,
'reason_phrase' => $response->reasonPhrase,
'protocol_version' => $response->protocolVersion,
'headers' => $response->headers,
'body' => (string) $response->body,
];
}

Expand Down
10 changes: 5 additions & 5 deletions src/Response/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public static function fromStream(StreamInterface $stream): Response {
* Create a string representation of a response.
*/
public static function toString(ResponseInterface $response): string {
$reasonPhrase = $response->getReasonPhrase();
$headers = self::serializeHeaders($response->getHeaders());
$body = (string) $response->getBody();
$reasonPhrase = $response->reasonPhrase;
$headers = self::serializeHeaders($response->headers);
$body = (string) $response->body;
$format = 'HTTP/%s %d%s%s%s';

if (! empty($headers)) {
Expand All @@ -80,8 +80,8 @@ public static function toString(ResponseInterface $response): string {

return sprintf(
$format,
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->protocolVersion,
$response->status,
$reasonPhrase ? ' ' . $reasonPhrase : '',
$headers,
$body
Expand Down
Loading