AbstractMessage::addHeaders()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport\Message
10
 */
11
12
namespace Bee4\Transport\Message;
13
14
/**
15
 * HTTP Message basic implementation
16
 * @package Bee4\Transport\Message
17
 */
18
abstract class AbstractMessage implements MessageInterface
19
{
20
    /**
21
     * Header collection
22
     * @var array
23
     */
24
    protected $headers = [];
25
26
    /**
27
     * Add a header to the message
28
     * @param string $name
29
     * @param string $value
30
     * @return AbstractMessage
31
     */
32 15
    public function addHeader($name, $value)
33
    {
34 15
        $this->headers[$name] = $value;
35 15
        return $this;
36
    }
37
38
    /**
39
     * Add multiple headers at once
40
     * @param array $headers
41
     * @return AbstractMessage
42
     */
43 17
    public function addHeaders(array $headers)
44
    {
45 17
        foreach ($headers as $name => $value) {
46 3
            if (is_numeric($name)) {
47 1
                $this->parseHeaderLine($value);
48 1
            } else {
49 3
                $this->addHeader($name, $value);
50
            }
51 17
        }
52 17
        return $this;
53
    }
54
55
    /**
56
     * Check if current message has requested header
57
     * @param string $name
58
     * @return boolean
59
     */
60 5
    public function hasHeader($name)
61
    {
62 5
        return isset($this->headers[$name]);
63
    }
64
65
    /**
66
     * Get a header by name
67
     * @param string $name
68
     * @return string|null
69
     */
70 5
    public function getHeader($name)
71
    {
72 5
        if ($this->hasHeader($name)) {
73 5
            return $this->headers[$name];
74
        }
75
76 1
        return null;
77
    }
78
79
    /**
80
     * Get all headers at once in an array
81
     * @return array
82
     */
83 3
    public function getHeaders()
84
    {
85 3
        return $this->headers;
86
    }
87
88
    /**
89
     * Retrieve headers in line format "name: value"
90
     * @return array
91
     */
92 10
    public function getHeaderLines()
93
    {
94 10
        $headers = [];
95 10
        foreach ($this->headers as $name => $value) {
96 2
            $headers[] = $name . ': ' . $value;
97 10
        }
98
99 10
        return $headers;
100
    }
101
102
    /**
103
     * Parse a plain header line if required
104
     * @param  string $line The line to parse: "Name: Value"
105
     * @return boolean
106
     */
107 1
    private function parseHeaderLine($line)
108
    {
109 1
        $matches = [];
110 1
        if (preg_match('/^([^:]+):(.*)$/', $line, $matches) === 1) {
111 1
            $this->addHeader(trim($matches[1]), trim($matches[2]));
112 1
        }
113 1
    }
114
115
    /**
116
     * Remove a header by name
117
     * @param string $name
118
     * @return AbstractMessage
119
     */
120 1
    public function removeHeader($name)
121
    {
122 1
        if ($this->hasHeader($name)) {
123 1
            unset($this->headers[$name]);
124 1
        }
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * Remove all headers at once
131
     * @return AbstractMessage
132
     */
133 1
    public function removeHeaders()
134
    {
135 1
        $this->headers = [];
136 1
        return $this;
137
    }
138
}
139