HeaderSelector
in package
HeaderSelector Class Doc Comment
Tags
Table of Contents
- getNextWeight() : int
- Calculate the next weight, based on the current one.
- selectHeaders() : array<string|int, string>
- adjustWeight() : array<string|int, string>
- buildAcceptHeader() : string
- getAcceptHeaderWithAdjustedWeight() : string
- Create an Accept header string from the given "Accept" headers array, recalculating all weights
- getHeaderAndWeight() : array<string|int, mixed>
- Given an Accept header, returns an associative array splitting the header and its weight
- selectAcceptHeader() : null|string
- Return the header 'Accept' based on an array of Accept provided.
Methods
getNextWeight()
Calculate the next weight, based on the current one.
public
getNextWeight(int $currentWeight, bool $hasMoreThan28Headers) : int
If there are less than 28 "Accept" headers, the weights will be decreased by 1 on its highest significant digit, using the following formula:
next weight = current weight - 10 ^ (floor(log(current weight - 1)))
( current weight minus ( 10 raised to the power of ( floor of (log to the base 10 of ( current weight minus 1 ) ) ) ) )
Starting from 1000, this generates the following series:
1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
The resulting quality codes are closer to the average "normal" usage of them (like "q=0.9", "q=0.8" and so on), but it only works if there is a maximum of 28 "Accept" headers. If we have more than that (which is extremely unlikely), then we fall back to a 1-by-1 decrement rule, which will result in quality codes like "q=0.999", "q=0.998" etc.
Parameters
- $currentWeight : int
-
varying from 1 to 1000 (will be divided by 1000 to build the quality value)
- $hasMoreThan28Headers : bool
Return values
int —selectHeaders()
public
selectHeaders(array<string|int, string> $accept, string $contentType, bool $isMultipart) : array<string|int, string>
Parameters
- $accept : array<string|int, string>
- $contentType : string
- $isMultipart : bool
Return values
array<string|int, string> —adjustWeight()
private
adjustWeight(array<string|int, array<string|int, mixed>> $headers, float &$currentWeight, bool $hasMoreThan28Headers) : array<string|int, string>
Parameters
- $headers : array<string|int, array<string|int, mixed>>
- $currentWeight : float
- $hasMoreThan28Headers : bool
Return values
array<string|int, string> —array of adjusted "Accept" headers
buildAcceptHeader()
private
buildAcceptHeader(string $header, int $weight) : string
Parameters
- $header : string
- $weight : int
Return values
string —getAcceptHeaderWithAdjustedWeight()
Create an Accept header string from the given "Accept" headers array, recalculating all weights
private
getAcceptHeaderWithAdjustedWeight(array<string|int, string> $accept, array<string|int, string> $headersWithJson) : string
Parameters
- $accept : array<string|int, string>
-
Array of Accept Headers
- $headersWithJson : array<string|int, string>
-
Array of Accept Headers of type "json"
Return values
string —"Accept" Header (e.g. "application/json, text/html; q=0.9")
getHeaderAndWeight()
Given an Accept header, returns an associative array splitting the header and its weight
private
getHeaderAndWeight(string $header) : array<string|int, mixed>
Parameters
- $header : string
-
"Accept" Header
Return values
array<string|int, mixed> —with the header and its weight
selectAcceptHeader()
Return the header 'Accept' based on an array of Accept provided.
private
selectAcceptHeader(array<string|int, string> $accept) : null|string
Parameters
- $accept : array<string|int, string>
-
Array of header
Return values
null|string —Accept (e.g. application/json)