Passed
Push — master ( e0fa92...e382f1 )
by Oss
02:09
created

Headers::__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
 * @category    Brownie/HttpClient
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\HttpClient;
9
10
/**
11
 * HTTP headers collection.
12
 */
13
class Headers
14
{
15
16
    /**
17
     * All headers at once.
18
     *
19
     * @var null|string
20
     */
21
    private $headerString = null;
22
23
    /**
24
     * Headers container as an array.
25
     *
26
     * @var null|array
27
     */
28
    private $headerList = null;
29
30
    /**
31
     * Sets incoming values.
32
     *
33
     * @param $headerString
34
     */
35 3
    public function __construct($headerString)
36
    {
37 3
        $this->setHeaderString(trim($headerString));
38 3
    }
39
40
    /**
41
     * Sets all headers at string.
42
     * Returns the current object.
43
     *
44
     * @param string    $headerString   All headers at string.
45
     *
46
     * @return self
47
     */
48 3
    private function setHeaderString($headerString)
49
    {
50 3
        $this->headerString = $headerString;
51 3
        return $this;
52
    }
53
54
    /**
55
     * Returns all headers at string.
56
     *
57
     * @return null|string
58
     */
59 3
    public function toString()
60
    {
61 3
        return $this->headerString;
62
    }
63
64
    /**
65
     * Sets headers container.
66
     * Returns the current object.
67
     *
68
     * @param array     $headerList     Headers container as an array.
69
     *
70
     * @return self
71
     */
72 3
    private function setHeaderList(array $headerList)
73
    {
74 3
        $this->headerList = $headerList;
75 3
        return $this;
76
    }
77
78
    /**
79
     * Return the headers container as an array.
80
     *
81
     * @return array|null
82
     */
83 3
    public function getHeaderList()
84
    {
85 3
        if (empty($this->headerList)) {
86 3
            $this->setHeaderList($this->initHeaderList());
87
        }
88 3
        return $this->headerList;
89
    }
90
91
    /**
92
     * Gets header of by name.
93
     *
94
     * @param string    $name               Header name.
95
     * @param mixed     $defaultValue       Default header value.
96
     *
97
     * @return string|mixed
98
     */
99 3
    public function get($name, $defaultValue = null)
100
    {
101 3
        $headerList = $this->getHeaderList();
102 3
        $name = strtolower($name);
103 3
        if (empty($headerList[$name])) {
104 3
            return $defaultValue;
105
        }
106 3
        return $headerList[$name];
107
    }
108
109
    /**
110
     * Initializing the headers container.
111
     * Returns headers container.
112
     *
113
     * @return array
114
     */
115 3
    private function initHeaderList()
116
    {
117 3
        $headerList = array();
118 3
        foreach (explode("\r\n", $this->toString()) as $index => $headerLine) {
119 3
            if (0 == $index) {
120 3
                continue;
121
            }
122 3
            list($key, $value) = explode(':', trim($headerLine), 2);
123 3
            $headerList[strtolower(trim($key))] = trim($value);
124
        }
125 3
        $this->setHeaderList($headerList);
126 3
        return $headerList;
127
    }
128
}
129