Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of late static binding to allow decorator extension #78

Merged
merged 8 commits into from
Dec 14, 2018
36 changes: 18 additions & 18 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ public function hasHeader($name)
*
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s).
* @return Response
* @return static
* @throws InvalidArgumentException for invalid header names or values.
*/
public function withAddedHeader($name, $value)
{
$response = $this->response->withAddedHeader($name, $value);
return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -228,13 +228,13 @@ public function withAddedHeader($name, $value)
* new body stream.
*
* @param StreamInterface $body Body.
* @return Response
* @return static
* @throws InvalidArgumentException When the body is not valid.
*/
public function withBody(StreamInterface $body)
{
$response = $this->response->withBody($body);
return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -249,13 +249,13 @@ public function withBody(StreamInterface $body)
*
* @param string $name Case-insensitive header field name.
* @param string|string[] $value Header value(s).
* @return Response
* @return static
* @throws InvalidArgumentException for invalid header names or values.
*/
public function withHeader($name, $value)
{
$response = $this->response->withHeader($name, $value);
return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -268,12 +268,12 @@ public function withHeader($name, $value)
* the named header.
*
* @param string $name Case-insensitive header field name to remove.
* @return Response
* @return static
*/
public function withoutHeader($name)
{
$response = $this->response->withoutHeader($name);
return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -287,12 +287,12 @@ public function withoutHeader($name)
* new protocol version.
*
* @param string $version HTTP protocol version
* @return Response
* @return static
*/
public function withProtocolVersion($version)
{
$response = $this->response->withProtocolVersion($version);
return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -312,13 +312,13 @@ public function withProtocolVersion($version)
* @param string $reasonPhrase The reason phrase to use with the
* provided status code; if none is provided, implementations MAY
* use the defaults as suggested in the HTTP specification.
* @return Response
* @return static
* @throws InvalidArgumentException For invalid status code arguments.
*/
public function withStatus($code, $reasonPhrase = '')
{
$response = $this->response->withStatus($code, $reasonPhrase);
return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -333,7 +333,7 @@ public function withStatus($code, $reasonPhrase = '')
* @param int $status The HTTP status code.
* @param int $options Json encoding options
* @param int $depth Json encoding max depth
* @return Response
* @return static
*/
public function withJson($data, int $status = null, int $options = 0, int $depth = 512): ResponseInterface
{
Expand All @@ -351,7 +351,7 @@ public function withJson($data, int $status = null, int $options = 0, int $depth
$response = $response->withStatus($status);
}

return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -364,7 +364,7 @@ public function withJson($data, int $status = null, int $options = 0, int $depth
*
* @param string $url The redirect destination.
* @param int|null $status The redirect HTTP status code.
* @return Response
* @return static
*/
public function withRedirect(string $url, $status = null): ResponseInterface
{
Expand All @@ -375,7 +375,7 @@ public function withRedirect(string $url, $status = null): ResponseInterface
}
$response = $response->withStatus($status);

return new Response($response, $this->streamFactory);
return new static($response, $this->streamFactory);
}

/**
Expand All @@ -386,9 +386,9 @@ public function withRedirect(string $url, $status = null): ResponseInterface
* Proxies to the underlying stream and writes the provided data to it.
*
* @param string $data
* @return self
* @return static
*/
public function write($data)
public function write($data): ResponseInterface
{
$this->response->getBody()->write($data);
return $this;
Expand Down
64 changes: 32 additions & 32 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,13 @@ public function hasHeader($name)
*
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s).
* @return ServerRequest
* @return static
* @throws InvalidArgumentException for invalid header names or values.
*/
public function withAddedHeader($name, $value)
{
$serverRequest = $this->serverRequest->withAddedHeader($name, $value);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -445,12 +445,12 @@ public function withAddedHeader($name, $value)
* @see getAttributes()
* @param string $name The attribute name.
* @param mixed $value The value of the attribute.
* @return ServerRequest
* @return static
*/
public function withAttribute($name, $value)
{
$serverRequest = $this->serverRequest->withAttribute($name, $value);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -466,7 +466,7 @@ public function withAttribute($name, $value)
* updated attributes.
*
* @param array $attributes New attributes
* @return ServerRequest
* @return static
*/
public function withAttributes(array $attributes)
{
Expand All @@ -476,7 +476,7 @@ public function withAttributes(array $attributes)
$serverRequest = $serverRequest->withAttribute($attribute, $value);
}

return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -491,12 +491,12 @@ public function withAttributes(array $attributes)
*
* @see getAttributes()
* @param string $name The attribute name.
* @return ServerRequest
* @return static
*/
public function withoutAttribute($name)
{
$serverRequest = $this->serverRequest->withoutAttribute($name);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -509,13 +509,13 @@ public function withoutAttribute($name)
* new body stream.
*
* @param StreamInterface $body Body.
* @return ServerRequest
* @return static
* @throws InvalidArgumentException When the body is not valid.
*/
public function withBody(StreamInterface $body)
{
$serverRequest = $this->serverRequest->withBody($body);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -533,12 +533,12 @@ public function withBody(StreamInterface $body)
* updated cookie values.
*
* @param array $cookies Array of key/value pairs representing cookies.
* @return ServerRequest
* @return static
*/
public function withCookieParams(array $cookies)
{
$serverRequest = $this->serverRequest->withCookieParams($cookies);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -553,13 +553,13 @@ public function withCookieParams(array $cookies)
*
* @param string $name Case-insensitive header field name.
* @param string|string[] $value Header value(s).
* @return ServerRequest
* @return static
* @throws InvalidArgumentException for invalid header names or values.
*/
public function withHeader($name, $value)
{
$serverRequest = $this->serverRequest->withHeader($name, $value);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -572,12 +572,12 @@ public function withHeader($name, $value)
* the named header.
*
* @param string $name Case-insensitive header field name to remove.
* @return ServerRequest
* @return static
*/
public function withoutHeader($name)
{
$serverRequest = $this->serverRequest->withoutHeader($name);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -592,13 +592,13 @@ public function withoutHeader($name)
* changed request method.
*
* @param string $method Case-sensitive method.
* @return ServerRequest
* @return static
* @throws InvalidArgumentException for invalid HTTP methods.
*/
public function withMethod($method)
{
$serverRequest = $this->serverRequest->withMethod($method);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -625,14 +625,14 @@ public function withMethod($method)
*
* @param null|array|object $data The deserialized body data. This will
* typically be in an array or object.
* @return ServerRequest
* @return static
* @throws InvalidArgumentException if an unsupported argument type is
* provided.
*/
public function withParsedBody($data)
{
$serverRequest = $this->serverRequest->withParsedBody($data);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -646,12 +646,12 @@ public function withParsedBody($data)
* new protocol version.
*
* @param string $version HTTP protocol version
* @return ServerRequest
* @return static
*/
public function withProtocolVersion($version)
{
$serverRequest = $this->serverRequest->withProtocolVersion($version);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -674,12 +674,12 @@ public function withProtocolVersion($version)
*
* @param array $query Array of query string arguments, typically from
* $_GET.
* @return ServerRequest
* @return static
*/
public function withQueryParams(array $query)
{
$serverRequest = $this->serverRequest->withQueryParams($query);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -697,12 +697,12 @@ public function withQueryParams(array $query)
* @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various
* request-target forms allowed in request messages)
* @param mixed $requestTarget
* @return ServerRequest
* @return static
*/
public function withRequestTarget($requestTarget)
{
$serverRequest = $this->serverRequest->withRequestTarget($requestTarget);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand All @@ -713,13 +713,13 @@ public function withRequestTarget($requestTarget)
* updated body parameters.
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
* @return ServerRequest
* @return static
* @throws InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles)
{
$serverRequest = $this->serverRequest->withUploadedFiles($uploadedFiles);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand Down Expand Up @@ -750,12 +750,12 @@ public function withUploadedFiles(array $uploadedFiles)
* @link http://tools.ietf.org/html/rfc3986#section-4.3
* @param UriInterface $uri New request URI to use.
* @param bool $preserveHost Preserve the original state of the Host header.
* @return ServerRequest
* @return static
*/
public function withUri(UriInterface $uri, $preserveHost = false)
{
$serverRequest = $this->serverRequest->withUri($uri, $preserveHost);
return new ServerRequest($serverRequest);
return new static($serverRequest);
}

/**
Expand Down Expand Up @@ -986,9 +986,9 @@ public function getServerParam($key, $default = null)
*
* @param string $mediaType A HTTP media type (excluding content-type params).
* @param callable $callable A callable that returns parsed contents for media type.
* @return self
* @return static
*/
public function registerMediaTypeParser($mediaType, callable $callable)
public function registerMediaTypeParser($mediaType, callable $callable): ServerRequestInterface
{
if ($callable instanceof Closure) {
$callable = $callable->bindTo($this);
Expand Down
Loading