Passed
Push — master ( ae009e...1bc377 )
by 世昌
02:06
created

Header::send()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\response\provider;
3
4
/**
5
 * 响应头提供器
6
 */
7
class Header
8
{
9
    /**
10
     * 头部信息
11
     *
12
     * @var array
13
     */
14
    protected $header;
15
16
    /**
17
     * 添加头部值
18
     *
19
     * @param string $name
20
     * @param string $content
21
     * @return void
22
     */
23
    public function addHeader(string $name, string $content)
24
    {
25
        $this->addHeaderRaw(trim($name).': '.trim($content));
26
    }
27
28
    /**
29
     * 添加头部
30
     *
31
     * @param string $content
32
     * @return void
33
     */
34
    public function addHeaderRaw(string $content)
35
    {
36
        $this->header[] = trim($content);
37
    }
38
39
    /**
40
     * 直接发送头部信息
41
     *
42
     * @param string $name
43
     * @param string $content
44
     * @param boolean $replace
45
     * @return boolean
46
     */
47
    public function sendHeader(string $name, string $content, bool $replace = true)
48
    {
49
        return $this->sendHeaderRaw(trim($name).': '.trim($content), $replace);
50
    }
51
52
    /**
53
     * 发送原始头部
54
     *
55
     * @param string $content
56
     * @param boolean $replace
57
     * @return bool
58
     */
59
    public function sendHeaderRaw(string $content, bool $replace = true)
60
    {
61
        if (!headers_sent()) {
62
            header($content, $replace);
63
            return true;
64
        } 
65
        return false;
66
    }
67
68
    /**
69
     * 发送头部信息
70
     *
71
     * @return boolean
72
     */
73
    public function send():bool
74
    {
75
        if (headers_sent()) {
76
            return false;
77
        }
78
        foreach ($this->header as $value) {
79
            $this->sendHeaderRaw($value);
80
        }
81
        return true;
82
    }
83
}
84