| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
final class Header |
| 7 |
{ |
| 8 |
private function __construct() |
| 9 |
{ |
| 10 |
} |
| 11 |
/** |
| 12 |
* Parses semicolon-separated header parameters into associative arrays, one |
| 13 |
* per comma-separated header value. Parameters without a value are appended |
| 14 |
* as values under integer keys. |
| 15 |
* |
| 16 |
* @param string|array $header Header to parse into components. |
| 17 |
*/ |
| 18 |
public static function parse($header) : array |
| 19 |
{ |
| 20 |
static $trimmed = "\"' \n\t\r"; |
| 21 |
$params = $matches = []; |
| 22 |
foreach ((array) $header as $value) { |
| 23 |
foreach (self::splitList($value) as $val) { |
| 24 |
$part = []; |
| 25 |
foreach (self::splitParameters($val) as $kvp) { |
| 26 |
$count = \preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches); |
| 27 |
if ($count === \false) { |
| 28 |
throw new \RuntimeException('Unable to parse header parameters: ' . \preg_last_error_msg()); |
| 29 |
} |
| 30 |
if ($count !== 0) { |
| 31 |
$m = $matches[0]; |
| 32 |
if (isset($m[1])) { |
| 33 |
$part[\trim($m[0], $trimmed)] = \trim($m[1], $trimmed); |
| 34 |
} else { |
| 35 |
$part[] = \trim($m[0], $trimmed); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
if ($part) { |
| 40 |
$params[] = $part; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
return $params; |
| 45 |
} |
| 46 |
/** |
| 47 |
* Split a header value into semicolon-separated parameters. |
| 48 |
* |
| 49 |
* @return string[] |
| 50 |
*/ |
| 51 |
private static function splitParameters(string $value) : array |
| 52 |
{ |
| 53 |
$values = []; |
| 54 |
$start = 0; |
| 55 |
$isQuoted = \false; |
| 56 |
$isEscaped = \false; |
| 57 |
for ($i = 0, $max = \strlen($value); $i < $max; ++$i) { |
| 58 |
$char = $value[$i]; |
| 59 |
if ($isEscaped) { |
| 60 |
$isEscaped = \false; |
| 61 |
continue; |
| 62 |
} |
| 63 |
if ($isQuoted && $char === '\\') { |
| 64 |
$isEscaped = \true; |
| 65 |
continue; |
| 66 |
} |
| 67 |
if ($char === '"') { |
| 68 |
$isQuoted = !$isQuoted; |
| 69 |
continue; |
| 70 |
} |
| 71 |
if (!$isQuoted && $char === ';') { |
| 72 |
$values[] = \substr($value, $start, $i - $start); |
| 73 |
$start = $i + 1; |
| 74 |
} |
| 75 |
} |
| 76 |
$values[] = \substr($value, $start); |
| 77 |
return $values; |
| 78 |
} |
| 79 |
/** |
| 80 |
* Splits an HTTP header defined to contain a comma-separated list into each |
| 81 |
* individual value. Empty values are removed. |
| 82 |
* |
| 83 |
* Example headers include `accept`, `cache-control`, and `if-none-match`. |
| 84 |
* |
| 85 |
* This method must not be used to parse headers that are not defined as a |
| 86 |
* list, such as `user-agent` or `set-cookie`. |
| 87 |
* |
| 88 |
* @param string|string[] $values Header value as returned by |
| 89 |
* MessageInterface::getHeader() |
| 90 |
* |
| 91 |
* @return string[] |
| 92 |
*/ |
| 93 |
public static function splitList($values) : array |
| 94 |
{ |
| 95 |
if (!\is_array($values)) { |
| 96 |
$values = [$values]; |
| 97 |
} |
| 98 |
$result = []; |
| 99 |
foreach ($values as $value) { |
| 100 |
if (!\is_string($value)) { |
| 101 |
throw new \TypeError('$header must either be a string or an array containing strings.'); |
| 102 |
} |
| 103 |
$v = ''; |
| 104 |
$isQuoted = \false; |
| 105 |
$isEscaped = \false; |
| 106 |
for ($i = 0, $max = \strlen($value); $i < $max; ++$i) { |
| 107 |
if ($isEscaped) { |
| 108 |
$v .= $value[$i]; |
| 109 |
$isEscaped = \false; |
| 110 |
continue; |
| 111 |
} |
| 112 |
if (!$isQuoted && $value[$i] === ',') { |
| 113 |
$v = \trim($v, " \t\n\r"); |
| 114 |
if ($v !== '') { |
| 115 |
$result[] = $v; |
| 116 |
} |
| 117 |
$v = ''; |
| 118 |
continue; |
| 119 |
} |
| 120 |
if ($isQuoted && $value[$i] === '\\') { |
| 121 |
$isEscaped = \true; |
| 122 |
$v .= $value[$i]; |
| 123 |
continue; |
| 124 |
} |
| 125 |
if ($value[$i] === '"') { |
| 126 |
$isQuoted = !$isQuoted; |
| 127 |
$v .= $value[$i]; |
| 128 |
continue; |
| 129 |
} |
| 130 |
$v .= $value[$i]; |
| 131 |
} |
| 132 |
$v = \trim($v, " \t\n\r"); |
| 133 |
if ($v !== '') { |
| 134 |
$result[] = $v; |
| 135 |
} |
| 136 |
} |
| 137 |
return $result; |
| 138 |
} |
| 139 |
} |
| 140 |
|