PockResponseBuilder
in package
uses
JsonSerializerAwareTrait, XmlSerializerAwareTrait
Builds the PSR-7 response returned by a pock definition.
Instances are normally obtained from PockBuilder::reply(), but can also be used directly.
$response = (new PockResponseBuilder(202))
->withHeader('Content-Type', 'text/plain')
->withBody('queued')
->getResponse();
Tags
Table of Contents
Methods
- __construct() : mixed
- Create a response builder with the supplied initial status code.
- getResponse() : ResponseInterface
- Return the immutable PSR-7 response built so far.
- withAddedHeader() : self
- Respond with specified header pattern appended to existing header.
- withAddedHeaders() : self
- Respond with specified headers. Works exactly like calling PockResponseBuilder::withAddedHeader() would work.
- withBody() : self
- Reply with specified body. It can be: - PSR-7 StreamInterface - it will be used without any changes.
- withFile() : self
- Reply with data from specified file.
- withHeader() : self
- Respond with specified header pattern.
- withHeaders() : self
- Respond with specified headers. Works exactly like calling PockResponseBuilder::withHeader() would work.
- withJson() : self
- Serialize data as JSON and use it as the response body.
- withStatusCode() : self
- Reply with specified status code.
- withXml() : self
- Serialize data as XML and use it as the response body.
Methods
__construct()
Create a response builder with the supplied initial status code.
public
__construct([int $statusCode = 200 ]) : mixed
$responseBuilder = new PockResponseBuilder(201);
Parameters
- $statusCode : int = 200
getResponse()
Return the immutable PSR-7 response built so far.
public
getResponse() : ResponseInterface
$response = $responseBuilder->getResponse();
Return values
ResponseInterfacewithAddedHeader()
Respond with specified header pattern appended to existing header.
public
withAddedHeader(string $name, string|array<string|int, string> $value) : self
Parameters
- $name : string
- $value : string|array<string|int, string>
Tags
Return values
selfwithAddedHeaders()
Respond with specified headers. Works exactly like calling PockResponseBuilder::withAddedHeader() would work.
public
withAddedHeaders(array<string, string|array<string|int, string>> $headers) : self
$responseBuilder->withAddedHeaders(['Vary' => ['Accept', 'Authorization']]);
Parameters
- $headers : array<string, string|array<string|int, string>>
Return values
selfwithBody()
Reply with specified body. It can be: - PSR-7 StreamInterface - it will be used without any changes.
public
withBody(StreamInterface|resource|string $stream) : self
- string - it will be used as contents contents.
- resource - it's data will be used as contents contents.
Seekable streams and resources are rewound before being attached.
$responseBuilder->withBody('plain response body');
Parameters
- $stream : StreamInterface|resource|string
Return values
selfwithFile()
Reply with data from specified file.
public
withFile(string $path[, string $mode = 'r' ]) : self
For available modes @see \fopen()
$responseBuilder->withFile(__DIR__ . '/fixtures/report.pdf', 'rb');
Parameters
- $path : string
- $mode : string = 'r'
Tags
Return values
selfwithHeader()
Respond with specified header pattern.
public
withHeader(string $name, string|array<string|int, string> $value) : self
Parameters
- $name : string
- $value : string|array<string|int, string>
Tags
Return values
selfwithHeaders()
Respond with specified headers. Works exactly like calling PockResponseBuilder::withHeader() would work.
public
withHeaders(array<string, string|array<string|int, string>> $headers) : self
$responseBuilder->withHeaders(['Content-Type' => 'text/plain', 'X-Job' => '42']);
Parameters
- $headers : array<string, string|array<string|int, string>>
Return values
selfwithJson()
Serialize data as JSON and use it as the response body.
public
withJson(mixed $data) : self
This method does not add a Content-Type header.
$responseBuilder->withHeader('Content-Type', 'application/json')
->withJson(['id' => 42]);
Parameters
- $data : mixed
Tags
Return values
selfwithStatusCode()
Reply with specified status code.
public
withStatusCode([int $statusCode = 200 ]) : self
$responseBuilder->withStatusCode(204);
Parameters
- $statusCode : int = 200
Return values
selfwithXml()
Serialize data as XML and use it as the response body.
public
withXml(mixed $data) : self
This method does not add a Content-Type header.
$responseBuilder->withHeader('Content-Type', 'application/xml')
->withXml('<user><id>42</id></user>');
Parameters
- $data : mixed