File
File
Properties
__defaultCacheControlDirectives
Default cache control.
public array $__defaultCacheControlDirectives = array (
'public' => false,
'max-age' => 300,
)Public with max age is 5 minutes
file
public FileObject $filemanager
public Manager $managerfmtime
public DateTime $fmtimebom
public string $bomcharsetMap
Charset map.
private array $charsetMap = array (
'utf-8' => 'EFBBBF',
'utf-16' => 'FEFF',
'utf-16be' => 'FEFF',
'utf-16le' => 'FFFE',
'utf-32' => '0000FEFF',
'utf-32be' => '0000FEFF',
'utf-32le' => 'FFFE0000',
)This is a map of charsets to their respective byte-order-marks.
Methods
__construct
\Hazaar\File Constructor.
public __construct(null $file, ?Manager $manager): voidParameters
| Parameter | Type | Description |
|---|---|---|
$file | null | either a string filename to use or a \Hazaar\File object |
$manager | Manager |
initialiseCacheControl
Initialise Cache-Control header based on configuration and request headers.
public initialiseCacheControl(): boolThis method sets the Cache-Control header for the response. It first loads the cache control configuration from the application config or uses the default directives. If a Cache-Control header is present in the incoming request, it parses and applies relevant directives (such as max-age, no-cache, no-store, no-transform). The method then builds the Cache-Control header value and sets it for the response.
load
Load a file into the response.
public load(string $file, ?Manager $manager): boolThis method attempts to load a file, either from a FileObject instance or a file path string. If a Manager is provided, it is used to manage the file. The method checks if the file exists or if there is content available. If successful, it sets the content type and last modified headers based on the file's properties.
Parameters
| Parameter | Type | Description |
|---|---|---|
$file | string | the file to load, either as a FileObject or a file path string |
$manager | Manager | optional file manager to use for loading the file |
content
Set the content of the response.
public content(mixed $data, ?string $contentType): selfIf a FileObject is provided, it is set as the file for the response. Otherwise, a new FileObject is created if one does not exist. Optionally sets the content type.
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | mixed | the content to set, or a FileObject instance |
$contentType | string | optional content type to set |
getContent
Get the content of the response, including BOM if set.
public getContent(): stringThis method retrieves the file contents if a file is set, otherwise falls back to the parent content. It checks for the presence of a byte-order-mark (BOM) at the start of the content and manages the BOM accordingly.
getContentLength
Get the length of the response content in bytes.
public getContentLength(): intReturns the size of the file if set, otherwise returns 0.
hasContent
Determine if the response has content.
public hasContent(): boolChecks if the associated file exists and has a size greater than zero.
setUnmodified
Set the response as unmodified if the file has not changed since the given date.
public setUnmodified(DateTime $ifModifiedSince): boolCompares the file's modification time to the provided DateTime. If the file has not been modified since the given time, sets the response status to 304 (Not Modified).
Parameters
| Parameter | Type | Description |
|---|---|---|
$ifModifiedSince | DateTime | the date to compare the file's modification time against |
lastModified
Set the Last-Modified header for the response.
public lastModified(int $fmtime): selfAccepts a DateTime object or a Unix timestamp (int). If an integer is provided, it will be converted to a DateTime object in UTC. The method sets the internal $fmtime property and updates the 'Last-Modified' HTTP header using RFC 2822 format.
Parameters
| Parameter | Type | Description |
|---|---|---|
$fmtime | int | the last modified time as a DateTime object or Unix timestamp |
getLastModified
Get the value of the Last-Modified header.
public getLastModified(): stringPerform a regular expression match on the response content.
public (string $pattern, ?array $matches, int $flags, int $offset): intParameters
| Parameter | Type | Description |
|---|---|---|
$pattern | string | the regular expression pattern to search for |
$matches | array | if matches are found, this will be filled with the results |
$flags | int | Optional flags for preg_match() |
$offset | int | the alternate place from which to start the search (in bytes) |
replace
Perform a regular expression search and replace on the response content.
public replace(string $pattern, string $replacement, int $limit = -1, ?int $count): voidParameters
| Parameter | Type | Description |
|---|---|---|
$pattern | string | the regular expression pattern to search for |
$replacement | string | the string to replace matches with |
$limit | int | The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit). |
$count | int |
downloadable
Set the Content-Disposition header to make the file downloadable or inline.
public downloadable(bool $toggle = true, ?string $filename): selfParameters
| Parameter | Type | Description |
|---|---|---|
$toggle | bool | if true, sets as attachment (download); if false, sets as inline display |
$filename | string | Optional filename for the Content-Disposition header. Defaults to file's basename. |
contentType
Set or get the content type for the response and handle BOM if charset is specified.
public contentType(?string $type): selfThis method sets the content type for the response. If a charset is specified in the content type, it checks if a corresponding byte-order-mark (BOM) exists in the charset map and sets it accordingly.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | string | The MIME type to set for the response, optionally with charset (e.g., 'text/plain; charset=utf-8'). |
getContentType
Get the content type of the response.
public getContentType(): stringReturns the content type if set, otherwise returns the MIME content type of the file.
getFile
Get the FileObject associated with this response.
public getFile(): ?FileObjectfileExists
Check if the associated file exists.
public fileExists(): boolGenerated by Hazaar API Doc Generator on Sun, 01 Mar 2026 09:22:26 +0000