MediaWiki REL1_39
Wikimedia\Http\HttpAcceptNegotiator Class Reference

Utility for negotiating a value from a set of supported values using a preference list. More...

Public Member Functions

 __construct (array $supported)
 
 getBestSupportedKey (array $weights, $default=null)
 Returns the best supported key from the given weight map.
 
 getFirstSupportedValue (array $preferences, $default=null)
 Returns the first supported value from the given preference list.
 

Detailed Description

Utility for negotiating a value from a set of supported values using a preference list.

This is intended for use with HTTP headers like Accept, Accept-Language, Accept-Encoding, etc. See RFC 2616 section 14 for details.

To use this with a request header, first parse the header value into an array of weights using HttpAcceptParser, then call getBestSupportedKey.

Note
GPL-2.0-or-later
Author
Daniel Kinzler
Thiemo Kreuz

Definition at line 17 of file HttpAcceptNegotiator.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Http\HttpAcceptNegotiator::__construct ( array $supported)
Parameters
string[]$supportedA list of supported values.

Definition at line 32 of file HttpAcceptNegotiator.php.

Member Function Documentation

◆ getBestSupportedKey()

Wikimedia\Http\HttpAcceptNegotiator::getBestSupportedKey ( array $weights,
$default = null )

Returns the best supported key from the given weight map.

Of the keys from the $weights parameter that are also in the list of supported values supplied to the constructor, this returns the key that has the highest weight associated with it. If two keys have the same weight, the more specific key is preferred, as required by RFC2616 section 14. Keys that map to 0 or false are ignored. If no matching key is found, $default is returned.

Parameters
float[]$weightsAn associative array mapping accepted values to their respective weights.
null | string$defaultThe value to return if non of the keys in $weights is supported (null per default).
Returns
null|string The best supported key from the $weights parameter.

Definition at line 53 of file HttpAcceptNegotiator.php.

References Wikimedia\Http\HttpAcceptNegotiator\getFirstSupportedValue().

◆ getFirstSupportedValue()

Wikimedia\Http\HttpAcceptNegotiator::getFirstSupportedValue ( array $preferences,
$default = null )

Returns the first supported value from the given preference list.

Of the values from the $preferences parameter that are also in the list of supported values supplied to the constructor, this returns the value that has the lowest index in the list. If no such value is found, $default is returned.

Parameters
string[]$preferencesA list of acceptable values, in order of preference.
null | string$defaultThe value to return if non of the keys in $weights is supported (null per default).
Returns
null|string The best supported key from the $weights parameter.

Definition at line 89 of file HttpAcceptNegotiator.php.

Referenced by Wikimedia\Http\HttpAcceptNegotiator\getBestSupportedKey().


The documentation for this class was generated from the following file: