MediaWiki  1.33.0
Wikimedia\Http\HttpAcceptNegotiator Class Reference

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

Collaboration diagram for Wikimedia\Http\HttpAcceptNegotiator:

Public Member Functions

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

Private Member Functions

 valueMatches ( $accepted, $supported)
 Returns true if the given acceptable value matches the given supported value, according to the HTTP specification. More...
 

Private Attributes

string $defaultValue
 
string[] $supportedValues
 

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 $name, $value, as, and 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.

References $value, as, and Wikimedia\Http\HttpAcceptNegotiator\valueMatches().

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

◆ valueMatches()

Wikimedia\Http\HttpAcceptNegotiator::valueMatches (   $accepted,
  $supported 
)
private

Returns true if the given acceptable value matches the given supported value, according to the HTTP specification.

The following rules are used:

  • comparison is case-insensitive
  • if $accepted and $supported are equal, they match
  • if $accepted is * or * followed by /*, it matches any $supported value.
  • if both $accepted and $supported contain a /, and $accepted ends with /*, they match if the part before the first / is equal.
Parameters
string$acceptedAn accepted value (may contain wildcards)
string$supportedA supported value.
Returns
bool Whether the given supported value matches the given accepted value.

Definition at line 116 of file HttpAcceptNegotiator.php.

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

Member Data Documentation

◆ $defaultValue

string Wikimedia\Http\HttpAcceptNegotiator::$defaultValue
private

Definition at line 27 of file HttpAcceptNegotiator.php.

◆ $supportedValues

string [] Wikimedia\Http\HttpAcceptNegotiator::$supportedValues
private

Definition at line 22 of file HttpAcceptNegotiator.php.


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