HttpResponse::addHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @name      OpenImporter
4
 * @copyright OpenImporter contributors
5
 * @license   BSD https://opensource.org/licenses/BSD-3-Clause
6
 *
7
 * @version 1.0
8
 */
9
10
namespace OpenImporter;
11
12
/**
13
 * Class HttpResponse
14
 * Contains the data used by the template.
15
 *
16
 * @class HttpResponse
17
 */
18
class HttpResponse
19
{
20
	/** @var int (via magic) the current step  */
21
	public $step = 0;
22
23
	/** @var bool (via magic) the result of a load or check */
24
	public $valid;
25
26
	/** @var string (via magic) title based on script being run */
27
	public $page_title = 'OpenImporter';
28
29
	/** @var string (via magic) the script running */
30
	public $script;
31
32
	/** @var string (via magic see template) name of the template to show, like select_script */
33
	public $use_template;
34
35
	/** @var array (via magic see template) parameters for the call */
36
	public $params_template;
37
38
	/** @var string (via magic see template) */
39
	public $no_template;
40
41
	/** @var bool (via magic see template) */
42
	public $is_xml;
43
44
	/** @var bool (via magic, see template) */
45
	public $is_page;
46
47
	/** @var mixed|null (via magic, see template) */
48
	public $template_error;
49
50
	/** @var array Any kind of data the templates may need. */
51
	protected $data = array();
52
53
	/** @var \OpenImporter\ResponseHeader */
54
	protected $headers;
55
56
	/** @var \OpenImporter\Lang */
57
	public $lng;
58
59
	/** @var array */
60
	protected $error_params = array();
61
62
	/**
63
	 * HttpResponse constructor.
64
	 *
65
	 * @param ResponseHeader $headers
66
	 */
67
	public function __construct($headers)
68
	{
69
		$this->headers = $headers;
70
	}
71
72
	/**
73
	 * Set a key / value pair with magic
74
	 *
75
	 * @param string $key
76
	 * @param mixed $val
77
	 */
78
	public function __set($key, $val)
79
	{
80
		$this->data[$key] = $val;
81
	}
82
83
	/**
84
	 * Fetch a value for a key via magic
85
	 *
86
	 * @param string $key
87
	 *
88
	 * @return mixed
89
	 */
90
	public function __get($key)
91
	{
92
		return $this->data[$key] ?? null;
93
	}
94
95
	/**
96
	 * Output all defined headers
97
	 */
98
	public function sendHeaders()
99
	{
100
		foreach ($this->headers->get() as $val)
101
		{
102
			header($val);
103
		}
104
	}
105
106
	/**
107
	 * Add a new header for output later
108
	 *
109
	 * @param string $key
110
	 * @param mixed $val
111
	 */
112
	public function addHeader($key, $val)
113
	{
114
		$this->headers->set($key, $val);
115
	}
116
117
	/**
118
	 * Add an error message to the stack
119
	 *
120
	 * @param $error_message
121
	 */
122
	public function addErrorParam($error_message)
123
	{
124
		$this->error_params[] = $error_message;
125
	}
126
127
	/**
128
	 * Returns the collected errors, if any
129
	 *
130
	 * @return string[]
131
	 */
132
	public function getErrors()
133
	{
134
		$return = array();
135
136
		foreach ($this->error_params as $msg)
137
		{
138
			if (is_array($msg))
139
			{
140
				$return[] = sprintf($msg[0], $msg[1]);
141
			}
142
			else
143
			{
144
				$return[] = $msg;
145
			}
146
		}
147
148
		return $return;
149
	}
150
}
151