Response
extends Message
in package
implements
ResponseInterface
uses
ResponseDownload
Class Response.
Tags
Table of Contents
Interfaces
- ResponseInterface
- Interface ResponseInterface.
Properties
- $autoEtag : bool
- $autoEtagHashAlgo : string
- $body : string
- HTTP Message Body.
- $cacheSeconds : int
- $cookies : array<string, Cookie>
- HTTP Message Cookies.
- $csp : CSP
- $cspReportOnly : CSP
- $debugCollector : HTTPCollector
- $headers : array<string, string>
- HTTP Message Headers.
- $headersSent : bool
- $inToString : bool
- $isSent : bool
- $jsonFlags : int
- $method : string
- HTTP Request Method.
- $protocol : string
- HTTP Message Protocol.
- $replaceHeaders : bool
- $request : Request
- $sendedBody : string|null
- $statusCode : int
- HTTP Response Status Code.
- $statusReason : string
- HTTP Response Status Reason.
- $url : URL
- HTTP Request URL.
Methods
- __construct() : mixed
- Response constructor.
- __toString() : string
- appendBody() : static
- Append a string to the body.
- appendHeader() : static
- Append a Message header.
- getBody() : string
- Get the Response body.
- getCacheSeconds() : int
- Get the number of seconds the cache is active.
- getCookie() : Cookie|null
- Get a Cookie by name.
- getCookies() : array<string, Cookie>
- Get all Cookies.
- getCsp() : CSP|null
- Get the Content-Security-Policy instance or null.
- getCspReportOnly() : CSP|null
- Get the Content-Security-Policy-Report-Only instance or null.
- getHeader() : string|null
- getHeaderLine() : string|null
- getHeaderLines() : array<int, string>
- getHeaders() : array<string, string>
- getJsonFlags() : int
- Get JSON flags.
- getProtocol() : string
- Get the HTTP protocol.
- getRequest() : Request
- getStartLine() : string
- Get the Message Start-Line.
- getStatus() : string
- Get the status line without the protocol part.
- getStatusCode() : int
- Get the status code.
- getStatusReason() : string
- Get the status reason.
- hasCookie() : bool
- Say if the Message has a Cookie.
- hasCsp() : bool
- Tells if the Content-Security-Policy instance has been set.
- hasCspReportOnly() : bool
- Tells if the Content-Security-Policy-Report-Only instance has been set.
- hasDownload() : bool
- Tell if Response has a downloadable file.
- hasHeader() : bool
- isAutoEtag() : bool
- isReplacingHeaders() : bool
- Tells if headers are being replaced.
- isSent() : bool
- Say if the response was sent.
- isStatusCode() : bool
- parseQualityValues() : array<string, float>
- prependBody() : static
- Prepend a string to the body.
- redirect() : static
- Sets the HTTP Redirect Response with data accessible in the next HTTP Request.
- removeCookie() : static
- Remove a Cookie by name.
- removeCookies() : static
- Remove many Cookies by names.
- removeCsp() : static
- Remove the Content-Security-Policy instance.
- removeCspReportOnly() : static
- Remove the Content-Security-Policy-Report-Only instance.
- removeHeader() : static
- Remove a header by name.
- removeHeaders() : static
- Remove all headers.
- removeHeadersByNames() : static
- Remove headers by names.
- send() : void
- Send the Response headers, cookies and body to the output.
- setAutoEtag() : static
- Enable or disable the capability of auto-add the ETag header and negotiate the response with it.
- setBody() : static
- Set the Response body.
- setCache() : static
- Set the Cache-Control header.
- setContentEncoding() : static
- Set the Content-Encoding header.
- setContentLanguage() : static
- Set the Content-Language header.
- setContentLength() : static
- Set the Content-Length header.
- setContentType() : static
- Set the Content-Type header.
- setCookie() : static
- Set a new Cookie.
- setCookies() : static
- Set a list of Cookies.
- setCsp() : static
- Set the Content-Security-Policy instance.
- setCspReportOnly() : static
- Set the Content-Security-Policy-Report-Only instance.
- setDate() : static
- Set the Date header.
- setDebugCollector() : static
- setDownload() : static
- Sets a file to download/stream.
- setEtag() : static
- Set the ETag header.
- setExpires() : static
- Set the Expires header.
- setHeader() : static
- Set a Message header.
- setHeaders() : static
- Set a list of headers.
- setJson() : static
- Set Response body and Content-Type as JSON.
- setJsonFlags() : static
- Set JSON flags.
- setLastModified() : static
- Set the Last-Modified header.
- setNoCache() : static
- Clear the browser cache.
- setNotModified() : static
- Set the status line as "Not Modified".
- setReplaceHeaders() : static
- The replace parameter indicates whether the header should replace a previous similar header, or add a next header of the same type.
- setStatus() : static
- Set the status part in the start-line.
- setStatusCode() : static
- Set the status code.
- setStatusReason() : static
- Set a custom status reason.
- getMethod() : string
- Gets the HTTP Request Method.
- getUrl() : URL
- Gets the requested URL.
- isMethod() : bool
- negotiateContentType() : void
- Negotiates the Content-Type header, setting the MIME type "text/html" if the response body is not empty.
- negotiateCsp() : void
- Set the Content-Security-Policy and Content-Security-Policy-Report-Only headers if the CSP classes are set and the response has not downloads.
- negotiateEtag() : void
- Set the ETag header, based on the Response body, and start the negotiation.
- parseContentType() : string|null
- sendAll() : void
- sendBody() : void
- sendCookies() : void
- sendDownload() : void
- sendHeaders() : void
- Send the HTTP headers to the output.
- setMethod() : static
- Set the request method.
- setProtocol() : static
- Set the HTTP protocol.
- setUrl() : static
- Set the Message URL.
Properties
$autoEtag
protected
bool
$autoEtag
= false
$autoEtagHashAlgo
protected
string
$autoEtagHashAlgo
= 'xxh3'
$body
HTTP Message Body.
protected
string
$body
$cacheSeconds
protected
int
$cacheSeconds
= 0
$cookies
HTTP Message Cookies.
protected
array<string, Cookie>
$cookies
= []
$csp
protected
CSP
$csp
$cspReportOnly
protected
CSP
$cspReportOnly
$debugCollector
protected
HTTPCollector
$debugCollector
$headers
HTTP Message Headers.
protected
array<string, string>
$headers
= []
$headersSent
protected
bool
$headersSent
= false
$inToString
protected
bool
$inToString
= false
$isSent
protected
bool
$isSent
= false
$jsonFlags
protected
int
$jsonFlags
= \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE
$method
HTTP Request Method.
protected
string
$method
$protocol
HTTP Message Protocol.
protected
string
$protocol
= \Framework\HTTP\Protocol::HTTP_1_1
$replaceHeaders
protected
bool
$replaceHeaders
= false
$request
protected
Request
$request
$sendedBody
protected
string|null
$sendedBody
= null
$statusCode
HTTP Response Status Code.
protected
int
$statusCode
= \Framework\HTTP\Status::OK
$statusReason
HTTP Response Status Reason.
protected
string
$statusReason
= 'OK'
$url
HTTP Request URL.
protected
URL
$url
Methods
__construct()
Response constructor.
public
__construct(Request $request) : mixed
Parameters
- $request : Request
__toString()
public
__toString() : string
Attributes
- #[Override]
Return values
stringappendBody()
Append a string to the body.
public
appendBody(string $content) : static
Parameters
- $content : string
Return values
staticappendHeader()
Append a Message header.
public
appendHeader(string $name, string $value) : static
Used to set repeated header field names.
Parameters
- $name : string
- $value : string
Attributes
- #[Override]
Return values
staticgetBody()
Get the Response body.
public
getBody() : string
Attributes
- #[Override]
Return values
stringgetCacheSeconds()
Get the number of seconds the cache is active.
public
getCacheSeconds() : int
Attributes
- #[Pure]
Return values
intgetCookie()
Get a Cookie by name.
public
getCookie(string $name) : Cookie|null
Parameters
- $name : string
Attributes
- #[Pure]
Return values
Cookie|nullgetCookies()
Get all Cookies.
public
getCookies() : array<string, Cookie>
Attributes
- #[Pure]
Return values
array<string, Cookie>getCsp()
Get the Content-Security-Policy instance or null.
public
getCsp() : CSP|null
Return values
CSP|nullgetCspReportOnly()
Get the Content-Security-Policy-Report-Only instance or null.
public
getCspReportOnly() : CSP|null
Return values
CSP|nullgetHeader()
public
getHeader(string $name) : string|null
Parameters
- $name : string
Attributes
- #[Pure]
Return values
string|nullgetHeaderLine()
public
getHeaderLine(string $name) : string|null
Parameters
- $name : string
Attributes
- #[Pure]
Return values
string|nullgetHeaderLines()
public
getHeaderLines() : array<int, string>
Attributes
- #[Pure]
Return values
array<int, string>getHeaders()
public
getHeaders() : array<string, string>
Attributes
- #[Pure]
Return values
array<string, string>getJsonFlags()
Get JSON flags.
public
getJsonFlags() : int
Tags
Return values
intgetProtocol()
Get the HTTP protocol.
public
getProtocol() : string
Attributes
- #[Pure]
Return values
stringgetRequest()
public
getRequest() : Request
Attributes
- #[Pure]
Return values
RequestgetStartLine()
Get the Message Start-Line.
public
getStartLine() : string
Tags
Return values
stringgetStatus()
Get the status line without the protocol part.
public
getStatus() : string
Tags
Attributes
- #[Pure]
Return values
stringgetStatusCode()
Get the status code.
public
getStatusCode() : int
Attributes
- #[Override]
- #[Pure]
Return values
intgetStatusReason()
Get the status reason.
public
getStatusReason() : string
Attributes
- #[Pure]
Return values
stringhasCookie()
Say if the Message has a Cookie.
public
hasCookie(string $name) : bool
Parameters
- $name : string
-
Cookie name
Attributes
- #[Pure]
Return values
boolhasCsp()
Tells if the Content-Security-Policy instance has been set.
public
hasCsp() : bool
Return values
boolhasCspReportOnly()
Tells if the Content-Security-Policy-Report-Only instance has been set.
public
hasCspReportOnly() : bool
Return values
boolhasDownload()
Tell if Response has a downloadable file.
public
hasDownload() : bool
Attributes
- #[Pure]
Return values
boolhasHeader()
public
hasHeader(string $name[, string|null $value = null ]) : bool
Parameters
- $name : string
- $value : string|null = null
Attributes
- #[Pure]
Return values
boolisAutoEtag()
public
isAutoEtag() : bool
Return values
boolisReplacingHeaders()
Tells if headers are being replaced.
public
isReplacingHeaders() : bool
Tags
Return values
boolisSent()
Say if the response was sent.
public
isSent() : bool
Attributes
- #[Pure]
Return values
boolisStatusCode()
public
isStatusCode(int $code) : bool
Parameters
- $code : int
Tags
Attributes
- #[Override]
Return values
boolparseQualityValues()
public
static parseQualityValues(string|null $string) : array<string, float>
Parameters
- $string : string|null
Tags
Return values
array<string, float>prependBody()
Prepend a string to the body.
public
prependBody(string $content) : static
Parameters
- $content : string
Return values
staticredirect()
Sets the HTTP Redirect Response with data accessible in the next HTTP Request.
public
redirect(string $location[, array<string|int, mixed> $data = [] ][, int|null $code = null ]) : static
Parameters
- $location : string
-
Location Header value
- $data : array<string|int, mixed> = []
-
Session data available on next Request
- $code : int|null = null
-
HTTP Redirect status code. Leave null to determine based on the current HTTP method.
Tags
Return values
staticremoveCookie()
Remove a Cookie by name.
public
removeCookie(string $name) : static
Parameters
- $name : string
Attributes
- #[Override]
Return values
staticremoveCookies()
Remove many Cookies by names.
public
removeCookies(array<string|int, mixed> $names) : static
Parameters
- $names : array<string|int, mixed>
Attributes
- #[Override]
Return values
staticremoveCsp()
Remove the Content-Security-Policy instance.
public
removeCsp() : static
Return values
staticremoveCspReportOnly()
Remove the Content-Security-Policy-Report-Only instance.
public
removeCspReportOnly() : static
Return values
staticremoveHeader()
Remove a header by name.
public
removeHeader(string $name) : static
NOTE: This method does not remove headers set by PHP, such as with
sessions or the header() function. To do this type of removal use the
header_remove() function.
Parameters
- $name : string
Tags
Attributes
- #[Override]
Return values
staticremoveHeaders()
Remove all headers.
public
removeHeaders() : static
Attributes
- #[Override]
Return values
staticremoveHeadersByNames()
Remove headers by names.
public
removeHeadersByNames(array<string|int, string> $names) : static
Parameters
- $names : array<string|int, string>
Tags
Attributes
- #[Override]
Return values
staticsend()
Send the Response headers, cookies and body to the output.
public
send() : void
Tags
setAutoEtag()
Enable or disable the capability of auto-add the ETag header and negotiate the response with it.
public
setAutoEtag([bool $active = true ][, string|null $hashAlgo = null ]) : static
Parameters
- $active : bool = true
- $hashAlgo : string|null = null
Tags
Return values
staticsetBody()
Set the Response body.
public
setBody(string $body) : static
Parameters
- $body : string
Attributes
- #[Override]
Return values
staticsetCache()
Set the Cache-Control header.
public
setCache(int $seconds[, bool $public = false ]) : static
Parameters
- $seconds : int
- $public : bool = false
Tags
Return values
staticsetContentEncoding()
Set the Content-Encoding header.
public
setContentEncoding(string $encoding) : static
Parameters
- $encoding : string
Tags
Return values
staticsetContentLanguage()
Set the Content-Language header.
public
setContentLanguage(string $language) : static
Parameters
- $language : string
Tags
Return values
staticsetContentLength()
Set the Content-Length header.
public
setContentLength([int|null $length = null ]) : static
Parameters
- $length : int|null = null
-
Set a value or leave null to set the body length automatically
Tags
Return values
staticsetContentType()
Set the Content-Type header.
public
setContentType(string $mime[, string|null $charset = 'UTF-8' ]) : static
Parameters
- $mime : string
- $charset : string|null = 'UTF-8'
Tags
Return values
staticsetCookie()
Set a new Cookie.
public
setCookie(Cookie $cookie) : static
Parameters
- $cookie : Cookie
Attributes
- #[Override]
Return values
staticsetCookies()
Set a list of Cookies.
public
setCookies(array<string|int, mixed> $cookies) : static
Parameters
- $cookies : array<string|int, mixed>
Attributes
- #[Override]
Return values
staticsetCsp()
Set the Content-Security-Policy instance.
public
setCsp(CSP $csp) : static
Parameters
- $csp : CSP
Return values
staticsetCspReportOnly()
Set the Content-Security-Policy-Report-Only instance.
public
setCspReportOnly(CSP $csp) : static
Parameters
- $csp : CSP
Return values
staticsetDate()
Set the Date header.
public
setDate([DateTime $datetime = new DateTime() ]) : static
Parameters
- $datetime : DateTime = new DateTime()
-
A DateTime value or default to the current DateTime
Tags
Return values
staticsetDebugCollector()
public
setDebugCollector(HTTPCollector $debugCollector) : static
Parameters
- $debugCollector : HTTPCollector
Return values
staticsetDownload()
Sets a file to download/stream.
public
setDownload(string $filepath[, bool $inline = false ][, bool $acceptRanges = true ][, int $delay = 0 ][, int $readLength = 1024 ][, string|null $filename = null ]) : static
Parameters
- $filepath : string
- $inline : bool = false
-
Set Content-Disposition header as "inline". Browsers load the file in the window. Set true to allow video or audio streams
- $acceptRanges : bool = true
-
Set Accept-Ranges header to "bytes". Allow partial downloads, media players to move the time position forward and back and download managers to continue/download multi-parts
- $delay : int = 0
-
Delay between flushs in microseconds
- $readLength : int = 1024
-
Bytes read by flush
- $filename : string|null = null
-
A custom filename
Tags
Return values
staticsetEtag()
Set the ETag header.
public
setEtag(string $etag[, bool $strong = true ]) : static
Parameters
- $etag : string
-
The ETag value without the quotes around it
- $strong : bool = true
-
False for a weak validator, otherwise true
Tags
Return values
staticsetExpires()
Set the Expires header.
public
setExpires([DateTime $datetime = new DateTime() ]) : static
Parameters
- $datetime : DateTime = new DateTime()
-
A DateTime value or default to the current DateTime
Tags
Return values
staticsetHeader()
Set a Message header.
public
setHeader(string $name, string $value) : static
Parameters
- $name : string
- $value : string
Attributes
- #[Override]
Return values
staticsetHeaders()
Set a list of headers.
public
setHeaders(array<string|int, mixed> $headers) : static
Parameters
- $headers : array<string|int, mixed>
Attributes
- #[Override]
Return values
staticsetJson()
Set Response body and Content-Type as JSON.
public
setJson(mixed $data[, int|null $flags = null ][, int<1, max> $depth = 512 ]) : static
Parameters
- $data : mixed
-
The data being encoded. Can be any type except a resource.
- $flags : int|null = null
-
[optional]
Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_AMP, JSON_HEX_APOS, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_THROW_ON_ERROR. JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE,
Default is JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE when null. JSON_THROW_ON_ERROR is enforced by default.
- $depth : int<1, max> = 512
-
Set the maximum depth. Must be greater than zero.
Tags
Return values
staticsetJsonFlags()
Set JSON flags.
public
setJsonFlags(int $flags) : static
Parameters
- $flags : int
Tags
Return values
staticsetLastModified()
Set the Last-Modified header.
public
setLastModified([DateTime $datetime = new DateTime() ]) : static
Parameters
- $datetime : DateTime = new DateTime()
-
A DateTime value or default to the current DateTime
Tags
Return values
staticsetNoCache()
Clear the browser cache.
public
setNoCache() : static
Return values
staticsetNotModified()
Set the status line as "Not Modified".
public
setNotModified() : static
Return values
staticsetReplaceHeaders()
The replace parameter indicates whether the header should replace a previous similar header, or add a next header of the same type.
public
setReplaceHeaders([bool $replace = true ]) : static
By default, it will replace, but if you pass in false as the first argument you can force multiple headers of the same type.
Parameters
- $replace : bool = true
Tags
Return values
staticsetStatus()
Set the status part in the start-line.
public
setStatus(int $code[, string|null $reason = null ]) : static
Parameters
- $code : int
- $reason : string|null = null
Tags
Return values
staticsetStatusCode()
Set the status code.
public
setStatusCode(int $code) : static
Parameters
- $code : int
Tags
Attributes
- #[Override]
Return values
staticsetStatusReason()
Set a custom status reason.
public
setStatusReason(string $reason) : static
Parameters
- $reason : string
Return values
staticgetMethod()
Gets the HTTP Request Method.
protected
getMethod() : string
Attributes
- #[Pure]
Return values
string —$method One of: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, or TRACE
getUrl()
Gets the requested URL.
protected
getUrl() : URL
Attributes
- #[Pure]
Return values
URLisMethod()
protected
isMethod(string $method) : bool
Parameters
- $method : string
Tags
Return values
boolnegotiateContentType()
Negotiates the Content-Type header, setting the MIME type "text/html" if the response body is not empty.
protected
negotiateContentType() : void
If the response body is empty, it removes the Content-Type header, and it will not appear to the client from the request.
This prevents the Content-Type from appearing without it being needed in, for example, REST API responses.
Tags
negotiateCsp()
Set the Content-Security-Policy and Content-Security-Policy-Report-Only headers if the CSP classes are set and the response has not downloads.
protected
negotiateCsp() : void
negotiateEtag()
Set the ETag header, based on the Response body, and start the negotiation.
protected
negotiateEtag() : void
-
Empty the body and set a status 304 (Not Modified) if the Request If-None-Match header has the same value of the generated ETag, on GET and HEAD requests.
-
Empty the body and set a status 412 (Precondition Failed) if the Request If-Match header is set and has not the same value of the generated ETag, on non-GET or non-HEAD requests.
Tags
parseContentType()
protected
parseContentType() : string|null
Attributes
- #[Pure]
Return values
string|nullsendAll()
protected
sendAll() : void
sendBody()
protected
sendBody() : void
sendCookies()
protected
sendCookies() : void
sendDownload()
protected
sendDownload() : void
sendHeaders()
Send the HTTP headers to the output.
protected
sendHeaders() : void
Tags
setMethod()
Set the request method.
protected
setMethod(string $method) : static
Parameters
- $method : string
-
One of: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, or TRACE
Tags
Return values
staticsetProtocol()
Set the HTTP protocol.
protected
setProtocol(string $protocol) : static
Parameters
- $protocol : string
-
HTTP/1.1, HTTP/2, etc
Return values
staticsetUrl()
Set the Message URL.
protected
setUrl(URL|string $url) : static
Parameters
- $url : URL|string