Headers   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 112
ccs 19
cts 19
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setCharset() 0 6 1
A set() 0 6 2
A get() 0 4 2
A encode() 0 4 1
A foramatName() 0 4 2
A toString() 0 9 2
A __toString() 0 4 1
1
<?php
2
/**
3
 * Sendmail package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2010, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT MIT
8
 */
9
10
namespace Sendmail\Message;
11
12
/**
13
 * Message headers.
14
 *
15
 * @author  Peter Gribanov <[email protected]>
16
 */
17
class Headers
18
{
19
    /**
20
     * Default charset.
21
     *
22
     * @var string
23
     */
24
    const DEFAULT_CHARSET = 'utf-8';
25
26
    /**
27
     * Headers end of line.
28
     *
29
     * @var string
30
     */
31
    const EOL = "\r\n";
32
33
    /**
34
     * Message charset.
35
     *
36
     * @var string
37
     */
38
    protected $charset = self::DEFAULT_CHARSET;
39
40
    /**
41
     * List headers.
42
     *
43
     * @var array
44
     */
45
    protected $list = array();
46
47
    /**
48
     * @param string $charset
49
     *
50
     * @return Headers
51
     */
52 21
    public function setCharset($charset)
53
    {
54 21
        $this->charset = $charset;
55
56 21
        return $this;
57
    }
58
59
    /**
60
     * @param string $key
61
     * @param string $value
62
     * @param bool $encode
63
     *
64
     * @return self
65
     */
66 25
    public function set($key, $value, $encode = false)
67
    {
68 25
        $this->list[$key] = $encode ? $this->encode($value) : $value;
69
70 25
        return $this;
71
    }
72
73
    /**
74
     * @param string $key
75
     *
76
     * @return string|null
77
     */
78 2
    public function get($key)
79
    {
80 2
        return isset($this->list[$key]) ? $this->list[$key] : null;
81
    }
82
83
    /**
84
     * Encode string.
85
     *
86
     * @param string $string
87
     *
88
     * @return string
89
     */
90 24
    public function encode($string)
91
    {
92 24
        return '=?'.$this->charset.'?B?'.base64_encode($string).'?=';
93
    }
94
95
    /**
96
     * Foramat name.
97
     *
98
     * @param string $email
99
     * @param string $name
100
     *
101
     * @return string
102
     */
103 7
    public function foramatName($email, $name = '')
104
    {
105 7
        return $name ? $this->encode($name).' <'.$email.'>' : $email;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 21
    public function toString()
112
    {
113 21
        $headers = '';
114 21
        foreach ($this->list as $key => $value) {
115 20
            $headers .= $key.': '.$value.self::EOL;
116
        }
117
118 21
        return $headers;
119
    }
120
121
    /**
122
     * @return string
123
     */
124 3
    public function __toString()
125
    {
126 3
        return $this->toString();
127
    }
128
}
129