Completed
Push — master ( 465cbd...e0d13e )
by Marco
06:44 queued 11s
created

HeadersTrait::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 3
nc 3
nop 1
crap 3
1
<?php namespace Comodojo\Dispatcher\Traits;
2
3
/**
4
 * @package     Comodojo Dispatcher
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @author      Marco Castiello <[email protected]>
7
 * @license     MIT
8
 *
9
 * LICENSE:
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17
 * THE SOFTWARE.
18
 */
19
20
trait HeadersTrait {
21
22
    protected $headers = [];
23
24 3
    public function get($header = null) {
25
26 3
        if (is_null($header)) return $this->headers;
27
28 3
        else if (array_key_exists($header, $this->headers)) return $this->headers[$header];
29
30 2
        else return null;
31
32
    }
33
34 1
    public function getAsString($header = null) {
35
36 1
        if (is_null($header)) {
37
38
            return array_map([$this, 'headerToString'],
39
                array_keys($this->headers),
40
                array_values($this->headers)
41
            );
42
43 1
        } else if (array_key_exists($header, $this->headers)) {
44
45 1
            return self::headerToString($header, $this->headers[$header]);
46
47
        } else {
48
            return null;
49
        }
50
51
    }
52
53 2
    public function set($header, $value = null) {
54
55 2
        if (is_null($value)) {
56
57
            $header = explode(":", $header, 2);
58
59
            $this->headers[$header[0]] = isset($header[1]) ? $header[1] : '';
60
61
        } else {
62
63 2
            $this->headers[$header] = $value;
64
65
        }
66
67 2
        return $this;
68
69
    }
70
71 2
    public function delete($header = null) {
72
73 2
        if (is_null($header)) {
74
75 1
            $this->headers = array();
76
77 1
            return true;
78
79 2
        } else if (array_key_exists($header, $this->headers)) {
80
81 1
            unset($this->headers[$header]);
82
83 1
            return true;
84
85
        } else {
86
87 1
            return false;
88
89
        }
90
91
    }
92
93 1
    public function merge($headers) {
94
95 1
        foreach ($headers as $key => $value) {
96
            $this->set($key, $value);
97 1
        }
98
99 1
        return $this;
100
101
    }
102
103 1
    private static function headerToString($header, $value) {
104
105 1
        return (string)($header.':'.$value);
106
107
    }
108
109
}
110