1 | <?php |
||
18 | final class headers |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Parse response headers string from a HTTP request into an array of headers. e.g. |
||
23 | * [ 'Location' => 'http://www.example.com', ... ] |
||
24 | * When multiple headers with the same name are present, all values will form an array, in the order in which |
||
25 | * they are present in the source. |
||
26 | * @param string|string[] $headers The headers string to parse. |
||
27 | * @return array |
||
28 | */ |
||
29 | 6 | public static function parse( $headers ) { |
|
63 | |||
64 | /** |
||
65 | * Return the last value sent for a specific header, uses the output of parse(). |
||
66 | * @param (mixed) $headers An array with multiple header strings or a single string. |
||
67 | * @return array|mixed |
||
68 | */ |
||
69 | 1 | private static function getLastHeader($headers) { |
|
75 | |||
76 | /** |
||
77 | * Return an array with values from a header like Cache-Control |
||
78 | * e.g. 'max-age=300,public,no-store' |
||
79 | * results in |
||
80 | * [ 'max-age' => '300', 'public' => 'public', 'no-store' => 'no-store' ] |
||
81 | * @param string $header |
||
82 | * @return array |
||
83 | */ |
||
84 | 4 | public static function parseHeader($header) |
|
85 | { |
||
86 | 4 | $header = (strpos($header, ':')!==false) ? explode(':', $header)[1] : $header; |
|
87 | 4 | $info = array_map('trim', explode(',', $header)); |
|
88 | 4 | $header = []; |
|
89 | 4 | foreach ( $info as $entry ) { |
|
90 | 4 | $temp = array_map( 'trim', explode( '=', $entry )); |
|
91 | 4 | $header[ $temp[0] ] = (isset($temp[1]) ? $temp[1] : $temp[0] ); |
|
92 | 4 | } |
|
93 | 4 | return $header; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Merge multiple occurances of a comma seperated header |
||
98 | * @param array $headers |
||
99 | * @return array |
||
100 | */ |
||
101 | 4 | public static function mergeHeaders( $headers ) |
|
114 | |||
115 | 4 | private static function getCacheControlTime( $header, $private ) |
|
153 | |||
154 | /** |
||
155 | * Parse response headers to determine if and how long you may cache the response. Doesn't understand ETags. |
||
156 | * @param string|string[] $headers Headers string or array as returned by parse() |
||
157 | * @param bool $private Whether to store a private cache or public cache image. |
||
158 | * @return int The number of seconds you may cache this result starting from now. |
||
159 | */ |
||
160 | 5 | public static function parseCacheTime( $headers, $private=true ) |
|
175 | |||
176 | } |