Test Failed
Pull Request — master (#171)
by Zaahid
04:32
created

HeaderParser::addRawHeaderToPart()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Parser;
8
9
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
10
11
/**
12
 * Reads headers from an input stream, adding them to a PartHeaderContainer.
13
 *
14
 * @author Zaahid Bateson
15
 */
16
class HeaderParser
17
{
18
    /**
19
     * Ensures the header isn't empty and contains a colon separator character,
20
     * then splits it and adds it to the passed PartHeaderContainer.
21
     *
22
     * @param string $header the header line
23
     * @param PartHeaderContainer $headerContainer the container
24
     */
25
    private function addRawHeaderToPart($header, PartHeaderContainer $headerContainer)
26
    {
27
        if ($header !== '' && strpos($header, ':') !== false) {
28
            $a = explode(':', $header, 2);
29
            $headerContainer->add($a[0], trim($a[1]));
30
        }
31
    }
32
33
    /**
34
     * Reads header lines up to an empty line, adding them to the passed
35
     * PartHeaderContainer.
36
     *
37
     * @param resource $handle The resource handle to read from.
38
     * @param PartHeaderContainer $container the container to add headers to.
39
     */
40
    public function parse($handle, PartHeaderContainer $container)
41
    {
42
        $header = '';
43
        do {
44
            $line = MessageParser::readLine($handle);
45
            if ($line === false || $line === '' || $line[0] !== "\t" && $line[0] !== ' ') {
46
                $this->addRawHeaderToPart($header, $container);
47
                $header = '';
48
            } else {
49
                $line = "\r\n" . $line;
50
            }
51
            $header .= rtrim($line, "\r\n");
52
        } while ($header !== '');
53
    }
54
}
55