Skip to content

Commit

Permalink
Added usability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shudd3r committed Aug 31, 2022
2 parents 7893207 + 74ea1a9 commit 0e5d730
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 57 deletions.
20 changes: 10 additions & 10 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ class Request implements RequestInterface
use RequestMethodsTrait;

/**
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
* @param UriInterface $uri
* @param StreamInterface $body
* @param array $headers Associative array of header strings or arrays of header strings
* @param array $params Associative array with following keys and its default values
* when key is not present or its value is null:
* - version - http protocol version (default: '1.1')
* - target - request target (default: resolved from passed $uri param)
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
* @param UriInterface $uri
* @param ?StreamInterface $body
* @param array $headers Associative array of header strings or arrays of header strings
* @param array $params Associative array with following keys and its default values
* when key is not present or its value is null:
* - version - http protocol version (default: '1.1')
* - target - request target (default: resolved from passed $uri param)
*
* @see https://tools.ietf.org/html/rfc7231#section-4.3
*/
public function __construct(
string $method,
UriInterface $uri,
StreamInterface $body,
?StreamInterface $body = null,
array $headers = [],
array $params = []
) {
$this->method = $this->validMethod($method);
$this->uri = $uri;
$this->body = $body;
$this->body = $body ?? Stream::fromBodyString('');
$this->version = isset($params['version']) ? $this->validProtocolVersion($params['version']) : '1.1';
$this->target = isset($params['target']) ? $this->validRequestTarget($params['target']) : null;
$this->loadHeaders($headers);
Expand Down
45 changes: 27 additions & 18 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ class Response implements ResponseInterface
private string $reason;

/**
* @param int $statusCode Normally one of the status codes defined by RFC 7231 section 6
* @param StreamInterface $body
* @param array $headers Associative array of header strings or arrays of header strings
* @param array $params Associative array with following keys and its default values
* when key is not present or its value is null:
* - version - http protocol version (default: '1.1')
* - reason - reason phrase normally associated with $statusCode, so by
* default it will be resolved from it.
* @param int $statusCode Normally one of the status codes defined by RFC 7231 section 6
* @param ?StreamInterface $body
* @param array $headers Associative array of header strings or arrays of header strings
* @param array $params Associative array with following keys and its default values
* when key is not present or its value is null:
* - version - http protocol version (default: '1.1')
* - reason - reason phrase normally associated with $statusCode, so by
* default it will be resolved from it.
*
* @see https://tools.ietf.org/html/rfc7231#section-6
* @see StatusCodesTrait
*/
public function __construct(
int $statusCode,
StreamInterface $body,
?StreamInterface $body = null,
array $headers = [],
array $params = []
) {
$this->status = $this->validStatusCode($statusCode);
$this->body = $body;
$this->body = $body ?? Stream::fromBodyString('');
$this->reason = $this->validReasonPhrase($params['reason'] ?? '');
$this->version = isset($params['version']) ? $this->validProtocolVersion($params['version']) : '1.1';
$this->loadHeaders($headers);
Expand All @@ -66,8 +66,8 @@ public static function xml(string $xml, int $statusCode = 200): self
}

/**
* There's a XOR operator between $defaultEncode and $encodeOptions,
* which means that if option is set in both provided and default it
* There's a XOR operator between $defaultOptions and $encodeOptions,
* which means that if an option is set in both provided and default it
* will be switched off.
*
* @param array $data
Expand All @@ -76,11 +76,10 @@ public static function xml(string $xml, int $statusCode = 200): self
*
* @return self
*/
public static function json(array $data, int $statusCode = 200, $encodeOptions = 0): self
public static function json(array $data, int $statusCode = 200, int $encodeOptions = 0): self
{
$defaultEncode = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT |
JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT;
$serialized = json_encode($data, $defaultEncode ^ $encodeOptions);
$defaultOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES;
$serialized = $data ? json_encode($data, $defaultOptions ^ $encodeOptions) : '{}';

return new self($statusCode, Stream::fromBodyString($serialized), ['Content-Type' => 'application/json']);
}
Expand All @@ -90,12 +89,22 @@ public static function redirect($uri, int $status = 303): self
if ($status < 300 || $status > 399) {
throw new InvalidArgumentException('Invalid status code for redirect response');
}
return new self($status, new Stream(fopen('php://temp', 'r')), ['Location' => (string) $uri]);
return new self($status, null, ['Location' => (string) $uri]);
}

public static function badRequest(StreamInterface $body = null): self
{
return new self(400, $body);
}

public static function unauthorized(StreamInterface $body = null): self
{
return new self(401, $body);
}

public static function notFound(StreamInterface $body = null): self
{
return new self(404, $body ?: Stream::fromResourceUri('php://temp'));
return new self(404, $body);
}

public function getStatusCode(): int
Expand Down
30 changes: 15 additions & 15 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ class ServerRequest extends Request implements ServerRequestInterface
private array $attributes = [];

/**
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
* @param UriInterface $uri
* @param StreamInterface $body
* @param array $headers Associative array of header strings or arrays of header strings
* @param array $params Associative array with following keys and its default values
* when key is not present or its value is null:
* - version - http protocol version (default: '1.1')
* - target - request target (default: resolved from passed $uri param)
* - server - $_SERVER superglobal equivalent (default: [])
* - cookie - $_COOKIE superglobal equivalent (default: [])
* - query - $_GET superglobal equivalent (default: [])
* - parsedBody - $_POST superglobal equivalent (default: [])
* - files - associative array (multidimensional) of UploadedFileInterface
* instances (default: [])
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
* @param UriInterface $uri
* @param ?StreamInterface $body
* @param array $headers Associative array of header strings or arrays of header strings
* @param array $params Associative array with following keys and its default values
* when key is not present or its value is null:
* - version - http protocol version (default: '1.1')
* - target - request target (default: resolved from passed $uri param)
* - server - $_SERVER superglobal equivalent (default: [])
* - cookie - $_COOKIE superglobal equivalent (default: [])
* - query - $_GET superglobal equivalent (default: [])
* - parsedBody - $_POST superglobal equivalent (default: [])
* - files - associative array (multidimensional) of UploadedFileInterface
* instances (default: [])
*
* @see https://tools.ietf.org/html/rfc7231#section-4.3
*/
public function __construct(
string $method,
UriInterface $uri,
StreamInterface $body,
?StreamInterface $body = null,
array $headers = [],
array $params = []
) {
Expand Down
4 changes: 2 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class Stream implements StreamInterface
{
protected $resource;
private $resource;

private ?array $metaData;
private ?bool $readable;
Expand Down Expand Up @@ -116,7 +116,7 @@ public function tell(): int

public function eof(): bool
{
return $this->resource ? feof($this->resource) : true;
return !$this->resource || feof($this->resource);
}

public function isSeekable(): bool
Expand Down
5 changes: 2 additions & 3 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Polymorphine\Message\Tests;

use PHPUnit\Framework\TestCase;
use Polymorphine\Message\Tests\Doubles\FakeStream;
use Polymorphine\Message\Request;
use Polymorphine\Message\Uri;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -142,9 +141,9 @@ private function request($method = 'GET', array $headers = [], $uri = null, $tar
$uri = Uri::fromString();
}
if (!$target) {
return new Request($method, $uri, new FakeStream(), $headers, []);
return new Request($method, $uri, null, $headers, []);
}

return new Request($method, $uri, new FakeStream(), $headers, ['target' => $target]);
return new Request($method, $uri, null, $headers, ['target' => $target]);
}
}
13 changes: 6 additions & 7 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,16 @@ public function invalidReasonPhrases(): array
public function testNamedConstructors()
{
$this->equivalentConstructs(
new Response(303, new FakeStream(), ['Location' => '/foo/bar/234']),
new Response(303, null, ['Location' => '/foo/bar/234']),
Response::redirect(Uri::fromString('/foo/bar/234'))
);
$this->equivalentConstructs(
new Response(301, new FakeStream(), ['Location' => '/foo/bar/baz']),
new Response(301, null, ['Location' => '/foo/bar/baz']),
Response::redirect('/foo/bar/baz', 301)
);
$this->equivalentConstructs(
new Response(404, new FakeStream()),
Response::notFound()
);
$this->equivalentConstructs(new Response(400), Response::badRequest());
$this->equivalentConstructs(new Response(401), Response::unauthorized());
$this->equivalentConstructs(new Response(404), Response::notFound());
$this->equivalentConstructs(
new Response(404, new FakeStream('Not Found. Sorry.')),
Response::notFound(new FakeStream('Not Found. Sorry.'))
Expand Down Expand Up @@ -184,6 +183,6 @@ private function equivalentConstructs(ResponseInterface $responseA, ResponseInte

private function response($status = 200, $reason = null): Response
{
return new Response($status, new FakeStream(), [], ['reason' => $reason]);
return new Response($status, null, [], ['reason' => $reason]);
}
}
4 changes: 2 additions & 2 deletions tests/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public function testUploadedFileNestedStructureIsValid()
$this->assertSame($files, $request->getUploadedFiles());
}

private function request(array $params = [], $method = 'GET', $headers = []): ServerRequest
private function request(array $params = []): ServerRequest
{
return new ServerRequest($method, Uri::fromString(), new Doubles\FakeStream(), $headers, $params);
return new ServerRequest('GET', Uri::fromString(), null, [], $params);
}
}

0 comments on commit 0e5d730

Please sign in to comment.