Passed
Push — master ( 185757...a51c77 )
by 世昌
02:03
created

Header   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sendHeader() 0 3 1
A send() 0 9 3
A addHeaderRaw() 0 3 1
A sendHeaderRaw() 0 7 2
A addHeader() 0 3 1
A sended() 0 2 1
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 (!$this->sended()) {
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 ($this->sended()) {
76
            return false;
77
        }
78
        foreach ($this->header as $value) {
79
            $this->sendHeaderRaw($value);
80
        }
81
        return true;
82
    }
83
84
85
    public function sended():bool {
86
        return headers_sent();
87
    }
88
}
89