1: | <?php |
2: | namespace Worldline\Acquiring\Sdk\Communication; |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | class HttpHeaderHelper |
10: | { |
11: | private function __construct() |
12: | { |
13: | } |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | public static function parseRawHeaders(array $rawHeaders) |
22: | { |
23: | $headers = array(); |
24: | $key = ''; |
25: | foreach ($rawHeaders as $rawHeader) { |
26: | $rawHeaderLineParts = explode(':', $rawHeader, 2); |
27: | if (isset($rawHeaderLineParts[1])) { |
28: | $key = $rawHeaderLineParts[0]; |
29: | $value = trim($rawHeaderLineParts[1]); |
30: | if (!isset($headers[$key])) { |
31: | $headers[$key] = $value; |
32: | } elseif (is_array($headers[$key])) { |
33: | $headers[$key][] = $value; |
34: | } else { |
35: | $headers[$key] = array($headers[$key], $value); |
36: | } |
37: | } elseif (strlen($rawHeaderLineParts[0]) > 0) { |
38: | if (!$key) { |
39: | $headers[0] = trim($rawHeaderLineParts[0]); |
40: | } elseif (in_array(substr($rawHeaderLineParts[0], 0, 1), array(' ', "\t"))) { |
41: | if (is_array($headers[$key])) { |
42: | $lastValue = array_pop($headers[$key]); |
43: | $headers[$key][] = $lastValue . "\r\n" . rtrim($rawHeaderLineParts[0]); |
44: | } else { |
45: | $headers[$key] .= "\r\n" . rtrim($rawHeaderLineParts[0]); |
46: | } |
47: | } |
48: | } |
49: | } |
50: | return $headers; |
51: | } |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | public static function generateRawHeaders(array $headers) |
60: | { |
61: | $rawHeaders = array(); |
62: | foreach ($headers as $key => $values) { |
63: | if (!is_array($values)) { |
64: | $values = array($values); |
65: | } |
66: | foreach ($values as $value) { |
67: | if ($key !== 0) { |
68: | $rawHeader = $key . ': ' . $value; |
69: | } else { |
70: | $rawHeader = $value; |
71: | } |
72: | foreach (explode("\r\n", $rawHeader) as $singleLineRawHeader) { |
73: | $rawHeaders[] = $singleLineRawHeader; |
74: | } |
75: | } |
76: | } |
77: | return $rawHeaders; |
78: | } |
79: | } |
80: | |