HeaderConverter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertRawToAssociative() 0 20 4
A convertAssociativeToRaw() 0 9 2
A convertComplexAssociativeToFlatAssociative() 0 9 2
1
<?php
2
3
/**
4
 * This converter is written based on the http header specification
5
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
6
 */
7
8
namespace Saxulum\HttpClient;
9
10
class HeaderConverter
11
{
12
    /**
13
     * @param  array $rawHeaders
14
     * @return array
15
     */
16
    public static function convertRawToAssociative(array $rawHeaders)
17
    {
18
        $associativeHeaders = array();
19
        foreach ($rawHeaders as $rawHeader) {
20
            if (false === $pos = strpos($rawHeader, ':')) {
21
                continue;
22
            }
23
24
            $headerName = substr($rawHeader, 0, $pos);
25
            $headerValue = trim(substr($rawHeader, $pos + 1));
26
27
            if (!isset($associativeHeaders[$headerName])) {
28
                $associativeHeaders[$headerName] = $headerValue;
29
            } else {
30
                $associativeHeaders[$headerName] .= ', ' . $headerValue;
31
            }
32
        }
33
34
        return $associativeHeaders;
35
    }
36
37
    /**
38
     * @param  array $associativeHeaders
39
     * @return array
40
     */
41
    public static function convertAssociativeToRaw(array $associativeHeaders)
42
    {
43
        $rawHeaders = array();
44
        foreach ($associativeHeaders as $headerName => $headerValue) {
45
            $rawHeaders[] = $headerName . ': ' . $headerValue;
46
        }
47
48
        return $rawHeaders;
49
    }
50
51
    /**
52
     * @param  array $complexAssociativeHeaders
53
     * @return array
54
     */
55
    public static function convertComplexAssociativeToFlatAssociative(array $complexAssociativeHeaders)
56
    {
57
        $associativeHeaders = array();
58
        foreach ($complexAssociativeHeaders as $headerName => $headerValues) {
59
            $associativeHeaders[$headerName] = implode(', ', $headerValues);
60
        }
61
62
        return $associativeHeaders;
63
    }
64
}
65