Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

Message   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addHeader() 0 8 3
A __construct() 0 4 1
A hasHeader() 0 3 1
A getContent() 0 3 1
A getHeader() 0 6 2
A getHeaders() 0 3 1
A setHeader() 0 10 2
1
<?php
2
3
namespace XoopsModules\Wggithub\Github\Http;
4
5
use XoopsModules\Wggithub\Github;
6
7
8
/**
9
 * HTTP request or response ascendant.
10
 *
11
 * @author  Miloslav Hůla (https://github.com/milo)
12
 */
13
abstract class Message extends Github\Sanity
14
{
15
	/** @var array[name => value] */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[name at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
16
	private $headers = [];
17
18
	/** @var string|NULL */
19
	private $content;
20
21
22
	/**
23
	 * @param  array
24
	 * @param  string|NULL
25
	 */
26
	public function __construct(array $headers = [], $content = NULL)
27
	{
28
		$this->headers = array_change_key_case($headers, CASE_LOWER);
29
		$this->content = $content;
30
	}
31
32
33
	/**
34
	 * @param  string
35
	 * @return bool
36
	 */
37
	public function hasHeader($name)
38
	{
39
		return array_key_exists(strtolower($name), $this->headers);
40
	}
41
42
43
	/**
44
	 * @param  string
45
	 * @param  mixed
46
	 * @return mixed
47
	 */
48
	public function getHeader($name, $default = NULL)
49
	{
50
		$name = strtolower($name);
51
		return array_key_exists($name, $this->headers)
52
			? $this->headers[$name]
53
			: $default;
54
	}
55
56
57
	/**
58
	 * @param  string
59
	 * @param  string
60
	 * @return self
61
	 */
62
	protected function addHeader($name, $value)
63
	{
64
		$name = strtolower($name);
65
		if (!array_key_exists($name, $this->headers) && $value !== NULL) {
66
			$this->headers[$name] = $value;
67
		}
68
69
		return $this;
70
	}
71
72
73
	/**
74
	 * @param  string
75
	 * @param  string|NULL
76
	 * @return self
77
	 */
78
	protected function setHeader($name, $value)
79
	{
80
		$name = strtolower($name);
81
		if ($value === NULL) {
82
			unset($this->headers[$name]);
83
		} else {
84
			$this->headers[$name] = $value;
85
		}
86
87
		return $this;
88
	}
89
90
91
	/**
92
	 * @return array
93
	 */
94
	public function getHeaders()
95
	{
96
		return $this->headers;
97
	}
98
99
100
	/**
101
	 * @return string|NULL
102
	 */
103
	public function getContent()
104
	{
105
		return $this->content;
106
	}
107
108
}
109