Documentation

AcceptHeader
in package

HTTP accept header class

RFC 7231 Section 5.3.2 compliant Accept header parser/negotiator. Built on QualityValue's generic 'value;q=N' parsing, adding media-type wildcard matching (/, type/) and specificity-based precedence (exact match beats type/ beats /). Media-range parameters other than 'q' (e.g. ';level=1') are ignored for matching purposes.

Tags
category

Pop

author

Nick Sagona, III nick@popphp.org

copyright

Copyright (c) 2009-2026 Nick Sagona, III

license

https://www.popphp.org/license New BSD License

version
6.0.0

Table of Contents

Properties

$entries  : array<string|int, QualityValue>
Parsed, quality-sorted entries

Methods

__construct()  : mixed
Constructor
accepts()  : bool
Whether any of the given media type(s) is acceptable
getPreferredType()  : string|null
Given the media types this server can actually respond with, return the client's best match. Ties (equal matches() score) are broken by $available's own order - the server's stated preference wins, since HTTP doesn't mandate an order for equal-quality client preferences. Returns null if every candidate scores 0.
matches()  : float
Get the effective quality for a concrete media type, resolved by specificity: exact type/subtype match > type/* > *\/*. Among entries tied at the same specificity, the highest quality wins. Returns 0.0 if nothing matches, if the highest-specificity match is explicitly excluded via q=0, or if every matching entry falls below the given $specificity threshold.
splitMediaType()  : array<string|int, mixed>
Split a media type into [type, subtype]. A value with no '/' is treated as [$value, '*'] so malformed entries degrade gracefully instead of raising a warning.

Properties

Methods

__construct()

Constructor

public __construct([string|null $header = null ]) : mixed

A null or empty header is treated as '/' - per RFC 7231, a request with no Accept header implies the client will accept any media type in response.

Parameters
$header : string|null = null

accepts()

Whether any of the given media type(s) is acceptable

public accepts(string|array<string|int, mixed> $types[, AcceptSpecificity $specificity = AcceptSpecificity::Any ]) : bool
Parameters
$types : string|array<string|int, mixed>
$specificity : AcceptSpecificity = AcceptSpecificity::Any
Return values
bool

getPreferredType()

Given the media types this server can actually respond with, return the client's best match. Ties (equal matches() score) are broken by $available's own order - the server's stated preference wins, since HTTP doesn't mandate an order for equal-quality client preferences. Returns null if every candidate scores 0.

public getPreferredType(array<string|int, string> $available[, AcceptSpecificity $specificity = AcceptSpecificity::Any ]) : string|null
Parameters
$available : array<string|int, string>
$specificity : AcceptSpecificity = AcceptSpecificity::Any
Return values
string|null

matches()

Get the effective quality for a concrete media type, resolved by specificity: exact type/subtype match > type/* > *\/*. Among entries tied at the same specificity, the highest quality wins. Returns 0.0 if nothing matches, if the highest-specificity match is explicitly excluded via q=0, or if every matching entry falls below the given $specificity threshold.

public matches(string $mediaType[, AcceptSpecificity $specificity = AcceptSpecificity::Any ]) : float
Parameters
$mediaType : string
$specificity : AcceptSpecificity = AcceptSpecificity::Any
Return values
float

splitMediaType()

Split a media type into [type, subtype]. A value with no '/' is treated as [$value, '*'] so malformed entries degrade gracefully instead of raising a warning.

protected static splitMediaType(string $mediaType) : array<string|int, mixed>
Parameters
$mediaType : string
Return values
array<string|int, mixed>

        
On this page

Search results