Skip to content

Commit 0e5d730

Browse files
committed
Added usability improvements
2 parents 7893207 + 74ea1a9 commit 0e5d730

File tree

7 files changed

+64
-57
lines changed

7 files changed

+64
-57
lines changed

src/Request.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ class Request implements RequestInterface
2121
use RequestMethodsTrait;
2222

2323
/**
24-
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
25-
* @param UriInterface $uri
26-
* @param StreamInterface $body
27-
* @param array $headers Associative array of header strings or arrays of header strings
28-
* @param array $params Associative array with following keys and its default values
29-
* when key is not present or its value is null:
30-
* - version - http protocol version (default: '1.1')
31-
* - target - request target (default: resolved from passed $uri param)
24+
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
25+
* @param UriInterface $uri
26+
* @param ?StreamInterface $body
27+
* @param array $headers Associative array of header strings or arrays of header strings
28+
* @param array $params Associative array with following keys and its default values
29+
* when key is not present or its value is null:
30+
* - version - http protocol version (default: '1.1')
31+
* - target - request target (default: resolved from passed $uri param)
3232
*
3333
* @see https://tools.ietf.org/html/rfc7231#section-4.3
3434
*/
3535
public function __construct(
3636
string $method,
3737
UriInterface $uri,
38-
StreamInterface $body,
38+
?StreamInterface $body = null,
3939
array $headers = [],
4040
array $params = []
4141
) {
4242
$this->method = $this->validMethod($method);
4343
$this->uri = $uri;
44-
$this->body = $body;
44+
$this->body = $body ?? Stream::fromBodyString('');
4545
$this->version = isset($params['version']) ? $this->validProtocolVersion($params['version']) : '1.1';
4646
$this->target = isset($params['target']) ? $this->validRequestTarget($params['target']) : null;
4747
$this->loadHeaders($headers);

src/Response.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ class Response implements ResponseInterface
2525
private string $reason;
2626

2727
/**
28-
* @param int $statusCode Normally one of the status codes defined by RFC 7231 section 6
29-
* @param StreamInterface $body
30-
* @param array $headers Associative array of header strings or arrays of header strings
31-
* @param array $params Associative array with following keys and its default values
32-
* when key is not present or its value is null:
33-
* - version - http protocol version (default: '1.1')
34-
* - reason - reason phrase normally associated with $statusCode, so by
35-
* default it will be resolved from it.
28+
* @param int $statusCode Normally one of the status codes defined by RFC 7231 section 6
29+
* @param ?StreamInterface $body
30+
* @param array $headers Associative array of header strings or arrays of header strings
31+
* @param array $params Associative array with following keys and its default values
32+
* when key is not present or its value is null:
33+
* - version - http protocol version (default: '1.1')
34+
* - reason - reason phrase normally associated with $statusCode, so by
35+
* default it will be resolved from it.
3636
*
3737
* @see https://tools.ietf.org/html/rfc7231#section-6
3838
* @see StatusCodesTrait
3939
*/
4040
public function __construct(
4141
int $statusCode,
42-
StreamInterface $body,
42+
?StreamInterface $body = null,
4343
array $headers = [],
4444
array $params = []
4545
) {
4646
$this->status = $this->validStatusCode($statusCode);
47-
$this->body = $body;
47+
$this->body = $body ?? Stream::fromBodyString('');
4848
$this->reason = $this->validReasonPhrase($params['reason'] ?? '');
4949
$this->version = isset($params['version']) ? $this->validProtocolVersion($params['version']) : '1.1';
5050
$this->loadHeaders($headers);
@@ -66,8 +66,8 @@ public static function xml(string $xml, int $statusCode = 200): self
6666
}
6767

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

8584
return new self($statusCode, Stream::fromBodyString($serialized), ['Content-Type' => 'application/json']);
8685
}
@@ -90,12 +89,22 @@ public static function redirect($uri, int $status = 303): self
9089
if ($status < 300 || $status > 399) {
9190
throw new InvalidArgumentException('Invalid status code for redirect response');
9291
}
93-
return new self($status, new Stream(fopen('php://temp', 'r')), ['Location' => (string) $uri]);
92+
return new self($status, null, ['Location' => (string) $uri]);
93+
}
94+
95+
public static function badRequest(StreamInterface $body = null): self
96+
{
97+
return new self(400, $body);
98+
}
99+
100+
public static function unauthorized(StreamInterface $body = null): self
101+
{
102+
return new self(401, $body);
94103
}
95104

96105
public static function notFound(StreamInterface $body = null): self
97106
{
98-
return new self(404, $body ?: Stream::fromResourceUri('php://temp'));
107+
return new self(404, $body);
99108
}
100109

101110
public function getStatusCode(): int

src/ServerRequest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@ class ServerRequest extends Request implements ServerRequestInterface
2828
private array $attributes = [];
2929

3030
/**
31-
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
32-
* @param UriInterface $uri
33-
* @param StreamInterface $body
34-
* @param array $headers Associative array of header strings or arrays of header strings
35-
* @param array $params Associative array with following keys and its default values
36-
* when key is not present or its value is null:
37-
* - version - http protocol version (default: '1.1')
38-
* - target - request target (default: resolved from passed $uri param)
39-
* - server - $_SERVER superglobal equivalent (default: [])
40-
* - cookie - $_COOKIE superglobal equivalent (default: [])
41-
* - query - $_GET superglobal equivalent (default: [])
42-
* - parsedBody - $_POST superglobal equivalent (default: [])
43-
* - files - associative array (multidimensional) of UploadedFileInterface
44-
* instances (default: [])
31+
* @param string $method Normally one of the common methods defined by RFC 7231 section 4.3
32+
* @param UriInterface $uri
33+
* @param ?StreamInterface $body
34+
* @param array $headers Associative array of header strings or arrays of header strings
35+
* @param array $params Associative array with following keys and its default values
36+
* when key is not present or its value is null:
37+
* - version - http protocol version (default: '1.1')
38+
* - target - request target (default: resolved from passed $uri param)
39+
* - server - $_SERVER superglobal equivalent (default: [])
40+
* - cookie - $_COOKIE superglobal equivalent (default: [])
41+
* - query - $_GET superglobal equivalent (default: [])
42+
* - parsedBody - $_POST superglobal equivalent (default: [])
43+
* - files - associative array (multidimensional) of UploadedFileInterface
44+
* instances (default: [])
4545
*
4646
* @see https://tools.ietf.org/html/rfc7231#section-4.3
4747
*/
4848
public function __construct(
4949
string $method,
5050
UriInterface $uri,
51-
StreamInterface $body,
51+
?StreamInterface $body = null,
5252
array $headers = [],
5353
array $params = []
5454
) {

src/Stream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class Stream implements StreamInterface
2121
{
22-
protected $resource;
22+
private $resource;
2323

2424
private ?array $metaData;
2525
private ?bool $readable;
@@ -116,7 +116,7 @@ public function tell(): int
116116

117117
public function eof(): bool
118118
{
119-
return $this->resource ? feof($this->resource) : true;
119+
return !$this->resource || feof($this->resource);
120120
}
121121

122122
public function isSeekable(): bool

tests/RequestTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Polymorphine\Message\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Polymorphine\Message\Tests\Doubles\FakeStream;
1615
use Polymorphine\Message\Request;
1716
use Polymorphine\Message\Uri;
1817
use Psr\Http\Message\RequestInterface;
@@ -142,9 +141,9 @@ private function request($method = 'GET', array $headers = [], $uri = null, $tar
142141
$uri = Uri::fromString();
143142
}
144143
if (!$target) {
145-
return new Request($method, $uri, new FakeStream(), $headers, []);
144+
return new Request($method, $uri, null, $headers, []);
146145
}
147146

148-
return new Request($method, $uri, new FakeStream(), $headers, ['target' => $target]);
147+
return new Request($method, $uri, null, $headers, ['target' => $target]);
149148
}
150149
}

tests/ResponseTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,16 @@ public function invalidReasonPhrases(): array
131131
public function testNamedConstructors()
132132
{
133133
$this->equivalentConstructs(
134-
new Response(303, new FakeStream(), ['Location' => '/foo/bar/234']),
134+
new Response(303, null, ['Location' => '/foo/bar/234']),
135135
Response::redirect(Uri::fromString('/foo/bar/234'))
136136
);
137137
$this->equivalentConstructs(
138-
new Response(301, new FakeStream(), ['Location' => '/foo/bar/baz']),
138+
new Response(301, null, ['Location' => '/foo/bar/baz']),
139139
Response::redirect('/foo/bar/baz', 301)
140140
);
141-
$this->equivalentConstructs(
142-
new Response(404, new FakeStream()),
143-
Response::notFound()
144-
);
141+
$this->equivalentConstructs(new Response(400), Response::badRequest());
142+
$this->equivalentConstructs(new Response(401), Response::unauthorized());
143+
$this->equivalentConstructs(new Response(404), Response::notFound());
145144
$this->equivalentConstructs(
146145
new Response(404, new FakeStream('Not Found. Sorry.')),
147146
Response::notFound(new FakeStream('Not Found. Sorry.'))
@@ -184,6 +183,6 @@ private function equivalentConstructs(ResponseInterface $responseA, ResponseInte
184183

185184
private function response($status = 200, $reason = null): Response
186185
{
187-
return new Response($status, new FakeStream(), [], ['reason' => $reason]);
186+
return new Response($status, null, [], ['reason' => $reason]);
188187
}
189188
}

tests/ServerRequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public function testUploadedFileNestedStructureIsValid()
147147
$this->assertSame($files, $request->getUploadedFiles());
148148
}
149149

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

0 commit comments

Comments
 (0)