Completed
Push — master ( 55caa0...3f2e9b )
by Oscar
02:05
created

HeadersExtractorTrait::extractHeaders()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 22
rs 8.9197
1
<?php
2
3
namespace Gettext\Utils;
4
5
use Gettext\Translations;
6
7
/**
8
 * Trait to provide the functionality of extracting headers
9
 */
10
trait HeadersExtractorTrait
11
{
12
    /**
13
     * Add the headers found to the translations instance
14
     * 
15
     * @param string       $headers
16
     * @param Translations $translations
17
     *
18
     * @return array
19
     */
20
    private static function extractHeaders($headers, Translations $translations)
21
    {
22
        $headers = explode("\n", $headers);
23
        $currentHeader = null;
24
25
        foreach ($headers as $line) {
26
            $line = self::convertString($line);
27
28
            if ($line === '') {
29
                continue;
30
            }
31
32
            if (self::isHeaderDefinition($line)) {
33
                $header = array_map('trim', explode(':', $line, 2));
34
                $currentHeader = $header[0];
35
                $translations->setHeader($currentHeader, $header[1]);
36
            } else {
37
                $entry = $translations->getHeader($currentHeader);
38
                $translations->setHeader($currentHeader, $entry.$line);
39
            }
40
        }
41
    }
42
43
    /**
44
     * Checks if it is a header definition line. Useful for distguishing between header definitions
45
     * and possible continuations of a header entry.
46
     *
47
     * @param string $line Line to parse
48
     *
49
     * @return bool
50
     */
51
    private static function isHeaderDefinition($line)
52
    {
53
        return (bool) preg_match('/^[\w-]+:/', $line);
54
    }
55
56
    /**
57
     * Normalize a string.
58
     *
59
     * @param string $value
60
     *
61
     * @return string
62
     */
63
    public static function convertString($value)
64
    {
65
        return $value;
66
    }
67
}
68