HeaderConverter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 34
ccs 7
cts 14
cp 0.5
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toBuzzHeaders() 0 15 4
A toPsrHeaders() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Buzz\Message;
6
7
/**
8
 * Convert between Buzz style:
9
 * array(
10
 *   'foo: bar',
11
 *   'baz: biz',
12
 * ).
13
 *
14
 * and PSR style:
15
 * array(
16
 *   'foo' => 'bar'
17
 *   'baz' => ['biz', 'buz'],
18
 * )
19
 *
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
class HeaderConverter
23
{
24
    /**
25
     * Convert from PSR style headers to Buzz style.
26
     */
27 247
    public static function toBuzzHeaders(array $headers): array
28
    {
29 247
        $buzz = [];
30
31 247
        foreach ($headers as $key => $values) {
32 235
            if (!\is_array($values)) {
33
                $buzz[] = sprintf('%s: %s', $key, $values);
34
            } else {
35 235
                foreach ($values as $value) {
36 235
                    $buzz[] = sprintf('%s: %s', $key, $value);
37
                }
38
            }
39
        }
40
41 247
        return $buzz;
42
    }
43
44
    /**
45
     * Convert from Buzz style headers to PSR style.
46
     */
47
    public static function toPsrHeaders(array $headers): array
48
    {
49
        $psr = [];
50
        foreach ($headers as $header) {
51
            list($key, $value) = explode(':', $header, 2);
52
            $psr[trim($key)][] = trim($value);
53
        }
54
55
        return $psr;
56
    }
57
}
58