Passed
Push — master ( ad3faa...5c4918 )
by Zaahid
03:33
created

HeaderContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Header;
8
9
use ArrayIterator;
10
use IteratorAggregate;
11
use ZBateson\MailMimeParser\Header\HeaderFactory;
12
13
/**
14
 * Maintains a collection of headers for a part.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
class HeaderContainer implements IteratorAggregate
19
{
20
    /**
21
     * @var HeaderFactory the HeaderFactory object used for created headers
22
     */
23
    protected $headerFactory;
24
25
    private $headerObjects = [];
26
    private $headers = [];
27
    private $headerMap = [];
28
29
    private $nextIndex = 0;
30
31 5
    public function __construct(HeaderFactory $headerFactory)
32
    {
33 5
        $this->headerFactory = $headerFactory;
34 5
    }
35
36
    /**
37
     * Returns the string in lower-case, and with non-alphanumeric characters
38
     * stripped out.
39
     *
40
     * @param string $header
41
     * @return string
42
     */
43 5
    private function getNormalizedHeaderName($header)
44
    {
45 5
        return preg_replace('/[^a-z0-9]/', '', strtolower($header));
46
    }
47
48 5
    public function exists($name, $offset = 0)
49
    {
50 5
        $s = $this->getNormalizedHeaderName($name);
51 5
        return isset($this->headerMap[$s][$offset]);
52
    }
53
54
    /**
55
     * Returns the AbstractHeader object for the header with the given $name
56
     *
57
     * Note that mime headers aren't case sensitive.
58
     *
59
     * @param string $name
60
     * @param int $offset
61
     * @return \ZBateson\MailMimeParser\Header\AbstractHeader
62
     */
63 5
    public function get($name, $offset = 0)
64
    {
65 5
        $s = $this->getNormalizedHeaderName($name);
66 5
        if (isset($this->headerMap[$s][$offset])) {
67 5
            return $this->getByIndex($this->headerMap[$s][$offset]);
68
        }
69 4
        return null;
70
    }
71
72 2
    public function getAll($name)
73
    {
74 2
        $s = $this->getNormalizedHeaderName($name);
75 2
        $ret = [];
76 2
        if (!empty($this->headerMap[$s])) {
77 2
            foreach ($this->headerMap[$s] as $index) {
78 2
                $ret[] = $this->getByIndex($index);
79
            }
80
        }
81 2
        return $ret;
82
    }
83
84 5
    private function getByIndex($index)
85
    {
86 5
        if (!isset($this->headers[$index])) {
87
            return null;
88
        }
89 5
        if ($this->headerObjects[$index] === null) {
90 5
            $this->headerObjects[$index] = $this->headerFactory->newInstance(
91 5
                $this->headers[$index][0],
92 5
                $this->headers[$index][1]
93
            );
94
        }
95 5
        return $this->headerObjects[$index];
96
    }
97
98 2
    public function remove($name, $offset = 0)
99
    {
100 2
        $s = $this->getNormalizedHeaderName($name);
101 2
        if (isset($this->headerMap[$s][$offset])) {
102 2
            $index = $this->headerMap[$s][$offset];
103 2
            array_splice($this->headerMap[$s], $offset, 1);
104 2
            unset($this->headers[$index]);
105 2
            unset($this->headerObjects[$index]);
106 2
            return true;
107
        }
108
        return false;
109
    }
110
111 1
    public function removeAll($name)
112
    {
113 1
        $s = $this->getNormalizedHeaderName($name);
114 1
        if (!empty($this->headerMap[$s])) {
115 1
            foreach ($this->headerMap[$s] as $i) {
116 1
                unset($this->headers[$i]);
117 1
                unset($this->headerObjects[$i]);
118
            }
119 1
            $this->headerMap[$s] = [];
120 1
            return true;
121
        }
122
        return false;
123
    }
124
    
125 5
    public function add($name, $value)
126
    {
127 5
        $s = $this->getNormalizedHeaderName($name);
128 5
        $this->headers[$this->nextIndex] = [ $name, $value ];
129 5
        $this->headerObjects[$this->nextIndex] = null;
130 5
        if (!isset($this->headerMap[$s])) {
131 5
            $this->headerMap[$s] = [];
132
        }
133 5
        array_push($this->headerMap[$s], $this->nextIndex);
134 5
        $this->nextIndex++;
135 5
    }
136
137 3
    public function set($name, $value, $offset = 0)
138
    {
139 3
        $s = $this->getNormalizedHeaderName($name);
140 3
        if (!isset($this->headerMap[$s][$offset])) {
141 3
            $this->add($name, $value);
142 3
            return;
143
        }
144 1
        $i = $this->headerMap[$s][$offset];
145 1
        $this->headers[$i] = [ $name, $value ];
146 1
        $this->headerObjects[$i] = null;
147 1
    }
148
149 5
    public function getHeaders()
150
    {
151 5
        return array_values(array_filter($this->headers));
152
    }
153
154
    public function getIterator()
155
    {
156
        return new ArrayIterator($this->getHeaders());
157
    }
158
}
159