HTML
HTML
- Full name:
\Hazaar\Controller\Response\HTML
- Parent class:
\Hazaar\Controller\Response
Properties
content
private string $content
Methods
__construct
public __construct(?string $content = null, int $status = 200): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$content | ?string | |
$status | int |
setContent
Sets the content for the response.
public setContent(mixed $content): void
Parameters:
Parameter | Type | Description |
---|---|---|
$content | mixed | the content to be set for the response |
getContent
Retrieves the content of the response.
public getContent(): string
Return Value:
an empty string as the content
Inherited methods
__construct
public __construct(string $type = 'text/html', int $status = 501): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string | |
$status | int |
__sleep
public __sleep(): mixed
writeOutput
Writes the output content to the response.
public writeOutput(): void
This method performs the following steps:
- Initializes the content to an empty string.
- Checks if the content has been modified and retrieves it if so.
- If an encryption key is set, encrypts the content using the default encryption cipher and sets the appropriate header.
- If the script is not running in CLI mode and headers have not been set, writes the headers.
- Outputs the content and flushes the output buffer.
Throws:
if headers have already been sent
setHeader
Sets a header for the response.
public setHeader(string $key, string $value, bool $overwrite = true): bool
This method allows setting a header key-value pair for the response. If the header key is 'content-length', the method will return false and will not set the header. Otherwise, it will set the header based on the provided parameters.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the header key |
$value | string | the header value |
$overwrite | bool | Optional. Whether to overwrite the existing header value if the key already exists. Default is true. |
Return Value:
returns true if the header was set successfully, false if the header key is 'content-length'
setHeaders
Add multiple headers.
public setHeaders((string[]|string)[] $headers, bool $overwrite = true): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$headers | (string[]|string)[] | |
$overwrite | bool |
removeHeader
Removes a header from the response.
public removeHeader(string $key): void
This method checks if a header with the specified key exists in the headers array. If it does, the header is removed.
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the header to be removed |
getHeaders
Retrieves the headers associated with the response.
public getHeaders(): array<string,string[]|string>
Return Value:
an array of headers
getHeader
Retrieves the header value associated with the specified key.
public getHeader(string $key): null|string[]|string
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the header to retrieve |
Return Value:
the value of the header if found, or null if not found
clearHeaders
Clears all the headers from the response.
public clearHeaders(): void
This method resets the headers array, effectively removing any headers that have been set previously. It is useful when you need to start with a clean slate for headers before setting new ones.
modified
Checks if the response has been modified.
public modified(): bool
This method determines if the response status code indicates that the content has been modified. A status code of 304 means "Not Modified", so this method returns true if the status code is anything other than 304.
Return Value:
true if the response has been modified, false otherwise
setContentType
Sets the content type for the response.
public setContentType(null|string $type = null): void
If no content type is provided, it attempts to detect the MIME type of the current content using the finfo
extension.
Parameters:
Parameter | Type | Description |
---|---|---|
$type | null|string | The MIME type to set. If null, the MIME type will be auto-detected. |
getContentType
Retrieves the content type of the response.
public getContentType(): string
Return Value:
the content type of the response
getContent
Retrieves the content of the response.
public getContent(): string
Return Value:
an empty string as the content
setContent
Sets the content for the response.
public setContent(mixed $content): void
Parameters:
Parameter | Type | Description |
---|---|---|
$content | mixed | the content to be set for the response |
getContentLength
Retrieves the content length.
public getContentLength(): int
Return Value:
the length of the content, which is currently always 0
hasContent
Checks if the response has content.
public hasContent(): bool
Return Value:
returns false indicating that the response does not have content
addContent
Adds content to the response.
public addContent(mixed $content): void
Parameters:
Parameter | Type | Description |
---|---|---|
$content | mixed | the content to be added to the response |
setStatus
Sets the HTTP status code for the response.
public setStatus(int $status): void
Parameters:
Parameter | Type | Description |
---|---|---|
$status | int | the HTTP status code to set |
getStatus
Retrieves the current status code of the response.
public getStatus(): int
Return Value:
the status code of the response
getStatusMessage
Retrieves the status message corresponding to the current HTTP status code.
public getStatusMessage(): string
This method uses the http_response_text
function to convert the status code stored in the $statusCode
property to its associated textual representation.
Return Value:
the status message corresponding to the current HTTP status code
ignoreHeaders
Marks the headers as set, effectively ignoring any further header modifications.
public ignoreHeaders(): void
This method sets the internal flag $headersSet
to true
, which indicates that headers have already been sent or should be ignored. This can be useful in scenarios where you want to prevent any additional headers from being added to the response.
writeHeaders
Sets the HTTP response headers.
private writeHeaders(null|int $content_length = null): void
This method sets the HTTP status code, content type, content length, and any additional headers specified in the $this->headers
array. If the content length is not provided, it will be determined by the getContentLength
method.
Parameters:
Parameter | Type | Description |
---|---|---|
$content_length | null|int | The length of the content. If null, it will be determined automatically. |
Automatically generated on 2024-11-14