Contributte Psr7-http-message
# Contributte PSR-7
PSR-7 is HTTP message interface. It's very useful interface especially for middleware / relay pattern. Read more on official PHP-FIG (opens new window) website.
This package is based on great guzzle/psr7 (opens new window) implementation. Only adds some extra features for convenient usage in Nette Framework (opens new window).
# Content
# Setup
composer require contributte/psr7-http-message
 # Psr7Request
 The easiest way is to create request using Psr7RequestFactory.
use Contributte\Psr7\Psr7RequestFactory;
$psr7 = Psr7RequestFactory::fromGlobal();
 2
3
In Nette application we can use existing Nette\Http\Request.
use Contributte\Psr7\Psr7RequestFactory;
$httpRequest = new Request();
$psr7 = Psr7RequestFactory::fromNette($httpRequest);
 2
3
4
You can fill httpRequest (opens new window) and applicationRequest (opens new window) over methods:
use Contributte\Psr7\Psr7RequestFactory;
$psr7 = Psr7RequestFactory::fromGlobal();
$psr7 = $psr7->withHttpRequest($httpRequest);
$psr7 = $psr7->withApplicationRequest($applicationRequest);
 2
3
4
5
Additional methods (against PSR7 interface):
- of(RequestInterface $request): self
 - getContents(): mixed
 - getContentsCopy(): mixed
 - getJsonBody(bool $associative = true): mixed
 - getJsonBodyCopy(bool $associative = true): mixed
 - withNewUri(string $uri): self - returns clone with given url
 
# Psr7ServerRequest
 Additional methods (against PSR7 interface):
- normalizeNetteFiles(Nette\Http\FileUpload[] $files): Psr7UploadedFile[]
 - of(ServerRequestInterface $request): self
 - fromGlobals(): self
 - withAttributes(array $attributes): self
 - hasQueryParam(string $name): bool
 - getQueryParam(string $name, mixed $default = null): mixed
 
# Psr7Response
 The easiest way is to create request using Psr7ResponseFactory.
use Contributte\Psr7\Psr7ResponseFactory;
$psr7 = Psr7ResponseFactory::fromGlobal();
 2
3
In Nette application we can use existing Nette\Http\Response.
use Contributte\Psr7\Psr7ResponseFactory;
$httpResponse = new Response();
$psr7 = Psr7ResponseFactory::fromNette($httpResponse);
 2
3
4
You can fill httpResponse (opens new window) and applicationResponse (opens new window) over methods:
use Contributte\Psr7\Psr7ResponseFactory;
$psr7 = Psr7ResponseFactory::fromGlobal();
$psr7 = $psr7->withHttpResponse($httpResponse);
$psr7 = $psr7->withApplicationResponse($applicationResponse);
 2
3
4
5
Additional methods (against PSR7 interface):
- of(ResponseInterface $response): self
 - fromGlobals(): self
 - appendBody(mixed $body): self
 - rewindBody(): self
 - writeBody(mixed $body): self
 - writeJsonBody(array $data): self
 - writeJsonObject(JsonSerializable $object): self
 - getJsonBody(bool $associative = true): mixed
 - getContents(bool $rewind = true): mixed
 - withHeaders(array $headers): self
 - getHttpResponse(): ?Nette\Http\IResponse
 - withHttpResponse(Nette\Http\IResponse $response)
 - hasHttpResponse(): bool
 - getApplicationResponse(): ?Nette\Application\IResponse
 - withApplicationResponse(Nette\Application\IResponse $response)
 - hasApplicationResponse(): bool
 - send(): void
 - sendHeaders(): void
 - sendBody(): void
 
# API
MessageInterface
getProtocolVersion: stringwithProtocolVersion(string $version): staticgetHeaders(): arrayhasHeader(string $name): boolgetHeader(string $name): string[]getHeaderLine(string $name): stringwithHeader(string $name, string|string[] $value): staticwithAddedHeader(string $name, string|string[] $value): staticwithoutHeader(string $name): staticgetBody(): StreamInterfacewithBody(StreamInterface $body): static
RequestInterface << MessageInterface
getRequestTarget(): stringwithRequestTarget($requestTarget): staticgetMethod(): stringwithMethod(string $method): staticgetUri(): UriInterfacewithUri(UriInterface $uri, bool $preserveHost = false): static
ServerRequestInterface << RequestInterface
getServerParams(): arraygetCookieParams(): arraywithCookieParams(array $cookies): staticgetQueryParams(): arraywithQueryParams(array $query): staticgetUploadedFiles(): UploadedFileInterface[]withUploadedFiles(array $uploadedFiles): staticgetParsedBody(): mixedwithParsedBody($data): staticgetAttributes(): mixed[]getAttribute(string $name, $default = null): mixedwithAttribute(string $name, $value): staticwithoutAttribute(string $name): static
ResponseInterface << MessageInterface
getStatusCode(): intwithStatus(int $code, string $reasonPhrase = ''): staticgetReasonPhrase(): string
StreamInterface
__toString(): stringclose(): voiddetach(): ?resourcegetSize(): ?inttell(): inteof(): boolisSeekable(): boolseek(int $offset, int $whence = SEEK_SET): voidrewind(): voidisWritable(): boolwrite($string): voidisReadable(): boolread($length): stringgetContents(): stringgetMetadata(?string $key = null): mixed
UriInterface
getScheme(): stringwithScheme(string $scheme): staticgetAuthority(): stringgetUserInfo(): stringwithUserInfo(string $user, ?string $password = null): staticgetHost(): stringwithHost(string $host): staticgetPort(): ?intwithPort(?int $port): staticgetPath(): stringwithPath(string $path): staticgetQuery(): stringwithQuery(string $query): staticgetFragment(): stringwithFragment(string $fragment): static__toString(): string
UploadedFileInterface
getStream(): StreamInterfacemoveTo($targetPath): voidgetSize(): ?intgetError(): intgetClientFilename(): ?stringgetClientMediaType(): ?string