Completed
Push — 4.0 ( 38d142...8ab097 )
by Marco
03:25
created

Headers::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
/**
4
 * @package     Comodojo Dispatcher
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @author      Marco Castiello <[email protected]>
7
 * @license     GPL-3.0+
8
 *
9
 * LICENSE:
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
26
trait Headers {
27
28
    protected $headers = array();
29
30 3
    public function get($header = null) {
31
32 3
        if ( is_null($header) ) return $this->headers;
33
34 3
        else if ( array_key_exists($header, $this->headers) ) return $this->headers[$header];
35
36 2
        else return null;
37
38
    }
39
40 1
    public function getAsString($header = null) {
41
42 1
        if ( is_null($header) ) {
43
44
            return array_map( function($header, $value) {
45
                return (string)($header.':'.$value);
46
            },
47
            $this->headers);
48
49 1
        } else if ( array_key_exists($header, $this->headers) ) {
50
51 1
            return (string)($header.':'.$this->headers[$header]);
52
53
        } else return null;
54
55
    }
56
57 2
    public function set($header, $value=null) {
58
59 2
        if ( is_null($value) ) {
60
61 1
            $header = explode(":", $header);
62
63 1
            $this->headers[$header[0]] = isset($header[1]) ? $header[1] : '';
64
65 1
        } else {
66
67 2
            $this->headers[$header] = $value;
68
69
        }
70
71 2
        return $this;
72
73
    }
74
75 2 View Code Duplication
    public function delete($header = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
77 2
        if ( is_null($header) ) {
78
79 1
            $this->headers = array();
80
81 1
            return true;
82
83 2
        } else if ( array_key_exists($header, $this->headers) ) {
84
85 1
            unset($this->headers[$header]);
86
87 1
            return true;
88
89
        } else {
90
91 1
            return false;
92
93
        }
94
95
    }
96
97 1
    public function merge($headers) {
98
99 1
        foreach ($headers as $key => $value) {
100
            $this->set($key, $value);
101 1
        }
102
103 1
        return $this;
104
105
    }
106
107
}
108