HeadersParser::parse()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 16
cts 16
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 1
crap 5
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Parser;
13
14
use Ivory\HttpAdapter\Asset\AbstractUninstantiableAsset;
15
use Ivory\HttpAdapter\Normalizer\HeadersNormalizer;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class HeadersParser extends AbstractUninstantiableAsset
21
{
22
    /**
23
     * @param array|string $headers
24
     *
25
     * @return array
26
     */
27 15786
    public static function parse($headers)
28
    {
29 15786
        if (is_string($headers)) {
30 2115
            $headers = explode("\r\n\r\n", trim($headers));
31
32 2115
            return explode("\r\n", end($headers));
33
        }
34
35 15705
        $parsedHeaders = [];
36
37 15705
        foreach ($headers as $name => $value) {
38 15642
            $value = HeadersNormalizer::normalizeHeaderValue($value);
39
40 15642
            if (is_int($name)) {
41 10774
                if (strpos($value, 'HTTP/') === 0) {
42 3078
                    $parsedHeaders = [$value];
43 2376
                } else {
44 10704
                    $parsedHeaders[] = $value;
45
                }
46 8304
            } else {
47 15586
                $parsedHeaders[] = $name.': '.$value;
48
            }
49 12103
        }
50
51 15705
        return $parsedHeaders;
52
    }
53
}
54