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.
Finds the most specific applicable range before considering its quality.
Determines whether a client-preferred type matches a server-supported type.
Parses media ranges in field order. These enum cases represent bare media types, so ranges requiring media parameters do not match them.
Splits only list commas outside quoted strings, honoring quoted-pair escapes.
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:
- The most specific matching range determines each representation's quality.
- Zero-quality representations are excluded before ranking by quality.
- 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
Finds the most specific applicable range before considering its quality.
private
static
findEffectivePreference(
array<int, array{type: string, q: float, specificity: int}>
$preferences,
string
$supported) : array{type: string, q: float, specificity: int}|null
Parameters
$preferences
:
array<int, array{type: string, q: float, specificity: int}>
$supported
:
string
Return values
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 media ranges in field order. These enum cases represent bare media types, so ranges requiring media parameters do not match them.
private
static
parseHeader(
string
$header) : array<int, array{type: string, q: float, specificity: int}>
Parameters
$header
:
string
Return values
Splits only list commas outside quoted strings, honoring quoted-pair escapes.
private
static
splitRanges(
string
$header) : array<int, string>
Parameters
$header
:
string