Index

tWithHeaders

MMO\core\classes · 1 symbol · generated 30 Sep 2026

trait

tWithHeaders

tWithHeaders.php:22

Generic, case-insensitive HTTP-header storage, implementing the read/write parts of PSR-7's MessageInterface (getHeaders/hasHeader/getHeader/getHeaderLine) plus a handful of convenience getters/setters on top.

trait MMO\core\classes\tWithHeaders
Namespace
MMO\core\classes
See
setHeader / hasHeader / getHeader call for the same header name (in any casing) resolves back to that same stored entry.
Todo
move into aMessage
Headers are stored under whichever casing they were FIRST set with (e.g. 'Content-Type'); every later
class Msg {use tWithHeaders;} $m = new Msg(); $m->setContentType('text/html'); $m->getHeaderLine('content-type'); // 'text/html;charset=utf-8' -- lookup is case-insensitive

Index

headers
ikeys
addHeader($name, $value)Append a value to a header, keeping any value(s) already set (as opposed to setHeader, which replaces).
getCharset()
getContentExtension()
getContentType($single)
getHeader($name)Retrieves a message header value by the given case-insensitive name.
getHeaderLine($name)Retrieves a comma-separated string of the values for a single header.
getHeaders()Retrieves all message header values.
getOrigin()Get origin of the request by looking at 'Origin', then 'Referer', then 'Sender' in the request's headers.
getUserAgent()
hasHeader($name)Checks if a header exists by the given case-insensitive name.
isHTML()
isIe()
isIphone()
isJSON()
isMac()
isMobile()
isWindows()
setCacheControl($cache, $pragma)Set the Cache-Control header (and, optionally, the legacy Pragma header alongside it).
setContentType($type, $charset)Set the Content-Type header, appending a charset parameter (unless $charset is null/empty).
setExpires($time)Set the Expires header from a unix timestamp, formatted as an RFC 7231 HTTP-date.
setHeader($name, $value)Set header, replacing any previous value(s) for the same (case-insensitively matched) header name.
unsetHeader($name)Remove a header entirely.

Properties2

headers

protected $headers = []

ikeys

protected $ikeys = []

Methods22

addHeader($name, $value)

tWithHeaders.php:166

Append a value to a header, keeping any value(s) already set (as opposed to setHeader, which replaces).

function addHeader(string $name, array|string $value): self
$namestring
$valuearray | stringa plain string is appended as a single new value (NOT split on ',', unlike setHeader)
returnsself
$msg->addHeader('X-Tags', 'a'); $msg->addHeader('X-Tags', 'b'); $msg->getHeader('X-Tags'); // ['a', 'b']

getCharset()

tWithHeaders.php:55
function getCharset(): ?string
returnsnull | stringthe charset parameter of the Content-Type header, or null if not present
$msg->setContentType('text/html'); $msg->getCharset(); // 'utf-8' (the default charset)

getContentExtension()

tWithHeaders.php:63
function getContentExtension(): ?string
returnsnull | stringthe file extension conventionally associated with the Content-Type header (e.g. 'html', 'json')
$msg->setContentType('text/html'); $msg->getContentExtension(); // 'html'

getContentType($single)

tWithHeaders.php:47
function getContentType(bool $single = true): array|string
$single = trueboolif true, return only the media type (e.g. 'text/html'); if false, every parameter too
returnsarray | string
$msg->setContentType('text/html'); $msg->getContentType(); // 'text/html'

getHeader($name)

tWithHeaders.php:254

Retrieves a message header value by the given case-insensitive name.

function getHeader(string $name): arrayThis method returns an array of all the header values of the given case-insensitive header name. If the header does not appear in the message, this method MUST return an empty array.
$namestringCase-insensitive header field name.
returnsarrayAn array of string values as provided for the given header. If the header does not appear in the message, this method MUST return an empty array.
$msg->addHeader('Accept', 'text/html'); $msg->addHeader('Accept', 'application/json'); $msg->getHeader('Accept'); // ['text/html', 'application/json']

getHeaderLine($name)

tWithHeaders.php:281

Retrieves a comma-separated string of the values for a single header.

function getHeaderLine(string $name): stringThis method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma. NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating. If the header does not appear in the message, this method MUST return an empty string.
$namestringCase-insensitive header field name.
returnsstringA string of values as provided for the given header concatenated together using a comma. If the header does not appear in the message, this method MUST return an empty string.
$msg->addHeader('Accept', 'text/html'); $msg->addHeader('Accept', 'application/json'); $msg->getHeaderLine('Accept'); // 'text/html, application/json'

getHeaders()

tWithHeaders.php:218

Retrieves all message header values.

function getHeaders(): arrayThe keys represent the header name as it will be sent over the wire, and each value is an array of strings associated with the header. // Represent the headers as a string foreach ($message->getHeaders() as $name => $values) { echo $name . ": " . implode(", ", $values); } // Emit headers iteratively: foreach ($message->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } While header names are not case-sensitive, getHeaders() will preserve the exact case in which headers were originally specified.
returnsarrayReturns an associative array of the message's headers. Each key MUST be a header name, and each value MUST be an array of strings for that header.
$msg->getHeaders(); // ['Content-Type' => ['text/html;charset=utf-8']]

getOrigin()

tWithHeaders.php:35

Get origin of the request by looking at 'Origin', then 'Referer', then 'Sender' in the request's headers.

function getOrigin(): string
returnsstring'' if none of those headers are set
$msg->setHeader('Origin', 'https://example.com'); $msg->getOrigin(); // 'https://example.com'

hasHeader($name)

tWithHeaders.php:231

Checks if a header exists by the given case-insensitive name.

function hasHeader(string $name): bool
$namestringCase-insensitive header field name.
returnsboolReturns true if any header names match the given header name using a case-insensitive string comparison. Returns false if no matching header name is found in the message.
$msg->setHeader('Content-Type', 'text/html'); $msg->hasHeader('content-type'); // true

isIphone()

tWithHeaders.php:77
function isIphone(): bool
returnsbooltrue if the User-Agent looks like mobile Safari on iPhone

isMobile()

tWithHeaders.php:105
function isMobile(): bool
returnsbooltrue if the User-Agent looks like a mobile device/browser

setCacheControl($cache, $pragma)

tWithHeaders.php:305

Set the Cache-Control header (and, optionally, the legacy Pragma header alongside it).

function setCacheControl($cache, $pragma = null): self
$cachecache method 'public', 'private', 'no-cache', 'no-store'
$pragma = nullif given, also sets the Pragma header (e.g. 'no-cache', for HTTP/1.0 clients)
returnsself
$msg->setCacheControl('no-cache', 'no-cache');

setContentType($type, $charset)

tWithHeaders.php:293

Set the Content-Type header, appending a charset parameter (unless $charset is null/empty).

function setContentType(string $type, ?string $charset = 'utf-8'): self
$typestringthe media type, e.g. 'text/html' or one of the framework's MIME_* constants
$charset = 'utf-8'null | stringappended as ;charset=$charset; pass null/'' to omit it entirely
returnsself
$msg->setContentType('text/html'); $msg->getHeaderLine('Content-Type'); // 'text/html;charset=utf-8'$msg->setContentType('application/octet-stream', null); // no charset appended

setExpires($time)

tWithHeaders.php:317

Set the Expires header from a unix timestamp, formatted as an RFC 7231 HTTP-date.

function setExpires(?int $time): self
$timenull | inta unix timestamp; 0 (or any past time) tells clients/caches the response is already stale
returnsself
$msg->setExpires(0); $msg->getHeaderLine('Expires'); // 'Thu, 01 Jan 1970 00:00:00 GMT'

setHeader($name, $value)

tWithHeaders.php:143

Set header, replacing any previous value(s) for the same (case-insensitively matched) header name.

function setHeader(string $name, array|string $value): self
$namestring
$valuearray | stringif a string, it is split on ',' into the multiple values PSR-7 expects a header to hold (e.g. 'a,b' becomes ['a', 'b']) -- pass an array (even a single-element one, e.g. [$value]) if $value may itself legitimately contain a comma that must NOT be split (an HTTP-date value like 'Thu, 01 Jan ...' is the classic example -- see setExpires)
returnsself
$msg->setHeader('Content-Type', 'text/html');$msg->setHeader('Accept', 'text/html,application/json'); // stored as ['text/html', 'application/json']

unsetHeader($name)

tWithHeaders.php:179

Remove a header entirely.

function unsetHeader(string $name): self
$namestring
returnsself
$msg->unsetHeader('X-Tags'); $msg->hasHeader('X-Tags'); // false