Passed
Pull Request — master (#171)
by Zaahid
07:22 queued 03:34
created

HeaderParser::parse()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.2222
cc 6
nc 2
nop 2
crap 6
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 115
    private function addRawHeaderToPart($header, PartHeaderContainer $headerContainer)
26
    {
27 115
        if ($header !== '' && strpos($header, ':') !== false) {
28 113
            $a = explode(':', $header, 2);
29 113
            $headerContainer->add($a[0], trim($a[1]));
30
        }
31 115
    }
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 115
    public function parse($handle, PartHeaderContainer $container)
41
    {
42 115
        $header = '';
43
        do {
44 115
            $line = MessageParser::readLine($handle);
45 115
            if ($line === false || $line === '' || $line[0] !== "\t" && $line[0] !== ' ') {
46 115
                $this->addRawHeaderToPart($header, $container);
47 115
                $header = '';
48
            } else {
49 96
                $line = "\r\n" . $line;
50
            }
51 115
            $header .= rtrim($line, "\r\n");
52 115
        } while ($header !== '');
53 115
    }
54
}
55