Completed
Push — v5-dev ( a42eb3 )
by Oscar
01:56
created

HeadersLoaderTrait::parseHeaders()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
namespace Gettext\Loader;
4
5
use Gettext\Translations;
6
7
/**
8
 * Trait to provide the functionality of parsing headers.
9
 */
10
trait HeadersLoaderTrait
11
{
12
    private static function parseHeaders(string $string): array
13
    {
14
        $headers = [];
15
        $lines = explode("\n", $string);
16
        $name = null;
17
18
        foreach ($lines as $line) {
19
            $line = self::convertString($line);
20
21
            if ($line === '') {
22
                continue;
23
            }
24
25
            if (self::isHeaderDefinition($line)) {
26
                $pieces = array_map('trim', explode(':', $line, 2));
27
                list($name, $value) = $pieces;
28
29
                $headers[$name] = $value;
30
                continue;
31
            }
32
33
            $value = $headers[$name] ?? '';
34
            $headers[$name] = $value.$line;
35
        }
36
37
        return $headers;
38
    }
39
40
    /**
41
     * Checks if it is a header definition line. Useful for distguishing between header definitions
42
     * and possible continuations of a header entry.
43
     */
44
    private static function isHeaderDefinition(string $line): bool
45
    {
46
        return (bool) preg_match('/^[\w-]+:/', $line);
47
    }
48
49
    /**
50
     * Normalize a string.
51
     */
52
    private static function convertString(string $value): string
53
    {
54
        return $value;
55
    }
56
}
57