Accept
Enum Accept.
Description
Represents common HTTP Accept header values and provides robust helpers for content negotiation in accordance with RFC 2119 requirement levels. This enum MUST be used to ensure that Accept-type comparisons are performed consistently and predictably. Implementations interacting with this enum SHOULD rely on its parsing and negotiation logic to determine the most appropriate response format based on client preferences.
Cases
Methods
Determines the best matching content type from the client's Accept header.
Calculates the specificity of a MIME type.
Determines whether a client-preferred type matches a server-supported type.
Parses the Accept header string into a sorted list of client preferences.
Determines the best matching content type from the client's Accept header.
public
static
getBestMatch(
string
$acceptHeader,
array<string|int, self>
$supportedTypes) : self|null
Description
This method SHALL apply proper HTTP content negotiation rules, including:
- Quality factors (q-values), which MUST be sorted in descending order.
- Specificity preference, where more explicit types MUST be preferred over wildcard types when q-values are equal. If a match cannot be found in the list of supported server types, the method MUST return null.
Parameters
$acceptHeader
:
string
Description
the raw HTTP Accept header value
$supportedTypes
:
array<string|int, self>
Description
an array of enum cases the server supports
Return values
Description
the best match, or null if no acceptable type exists
Calculates the specificity of a MIME type.
private
static
calculateSpecificity(
string
$type) : int
Description
Specificity MUST be determined using the following criteria:
- "* /*" → specificity 0 (least specific)
- "type/*" → specificity 1 (partially specific)
- "type/subtype" → specificity 2 (fully specific) This value SHALL be used to sort MIME types when q-values are equal.
Parameters
$type
:
string
Description
the MIME type to evaluate
Return values
Description
the calculated specificity score
Determines whether a client-preferred type matches a server-supported type.
private
static
matches(
string
$preference,
string
$supported) : bool
Description
This method MUST apply wildcard matching rules as defined in HTTP content negotiation:
- "* /*" MUST match any type.
- A full exact match MUST be treated as the highest priority.
- A "type/*" wildcard MUST match any subtype within the given type.
Parameters
$preference
:
string
Description
the MIME type from the client preference list
$supported
:
string
Description
the server-supported MIME type
Return values
Description
true if the supported type matches the preference
Parses the Accept header string into a sorted list of client preferences.
private
static
parseHeader(
string
$header) : array<int, array{type: string, q: float, specificity: int}>
Description
This method MUST:
- Extract MIME types from the header string.
- Parse quality factors (q-values), defaulting to 1.0 when omitted.
- Calculate specificity, which SHALL be used as a secondary sort criterion.
- Sort results by descending q-value and descending specificity. The resulting array MUST represent the client’s explicit and wildcard preferences in the correct HTTP negotiation order.
Parameters
$header
:
string
Description
the raw Accept header string