1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 zepi |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
* THE SOFTWARE. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The Response holds all information which are outputtet at the end |
29
|
|
|
* of the request. |
30
|
|
|
* |
31
|
|
|
* @package Zepi\Turbo\Response |
32
|
|
|
* @author Matthias Zobrist <[email protected]> |
33
|
|
|
* @copyright Copyright (c) 2015 zepi |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
namespace Zepi\Turbo\Response; |
37
|
|
|
|
38
|
|
|
use Zepi\Turbo\Request\RequestAbstract; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The Response holds all information which are outputtet at the end |
42
|
|
|
* of the request. |
43
|
|
|
* |
44
|
|
|
* @author Matthias Zobrist <[email protected]> |
45
|
|
|
* @copyright Copyright (c) 2015 zepi |
46
|
|
|
*/ |
47
|
|
|
class Response |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @access protected |
51
|
|
|
* @var RequestAbstract |
52
|
|
|
*/ |
53
|
|
|
protected $_request; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @access protected |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $_data = array(); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @access protected |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $_outputParts = array(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @access protected |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $_output; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructs the object |
75
|
|
|
* |
76
|
|
|
* @access public |
77
|
|
|
* @param \Zepi\Turbo\Request\RequestAbstract $request |
78
|
|
|
*/ |
79
|
|
|
public function __construct(RequestAbstract $request) |
80
|
|
|
{ |
81
|
|
|
$this->_request = $request; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the data for the given key. If the key does |
86
|
|
|
* not exists the function will return false. |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @param string $key |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function getData($key) |
93
|
|
|
{ |
94
|
|
|
if (!$this->hasData($key)) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->_data[$key]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns true if the given key is set. |
103
|
|
|
* |
104
|
|
|
* @access public |
105
|
|
|
* @param string $key |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
|
|
public function hasData($key) |
109
|
|
|
{ |
110
|
|
|
return (isset($this->_data[$key])); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Saves the value for the given key in the response object. |
115
|
|
|
* |
116
|
|
|
* @access public |
117
|
|
|
* @param string $key |
118
|
|
|
* @param mixed $value |
119
|
|
|
*/ |
120
|
|
|
public function setData($key, $value) |
121
|
|
|
{ |
122
|
|
|
$this->_data[$key] = $value; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the output for the given key. If the key does |
127
|
|
|
* not exists the function will return false. |
128
|
|
|
* |
129
|
|
|
* @access public |
130
|
|
|
* @param string $key |
131
|
|
|
* @return false|string |
132
|
|
|
*/ |
133
|
|
|
public function getOutputPart($key) |
134
|
|
|
{ |
135
|
|
|
if (!$this->hasOutputPart($key)) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->_outputParts[$key]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns true if the given key exists as output key. |
144
|
|
|
* |
145
|
|
|
* @access public |
146
|
|
|
* @param string $key |
147
|
|
|
* @return boolean |
148
|
|
|
*/ |
149
|
|
|
public function hasOutputPart($key) |
150
|
|
|
{ |
151
|
|
|
return (isset($this->_outputParts[$key])); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Saves the output for the given key in the Response object. |
156
|
|
|
* |
157
|
|
|
* @access public |
158
|
|
|
* @param string $key |
159
|
|
|
* @param string $output |
160
|
|
|
*/ |
161
|
|
|
public function setOutputPart($key, $output) |
162
|
|
|
{ |
163
|
|
|
$this->_outputParts[$key] = $output; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns all output parts of the Response object. |
168
|
|
|
* |
169
|
|
|
* @access public |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public function getOutputParts() |
173
|
|
|
{ |
174
|
|
|
return $this->_outputParts; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the output of the response. |
179
|
|
|
* |
180
|
|
|
* @access public |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function getOutput() |
184
|
|
|
{ |
185
|
|
|
return $this->_output; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns true if the response has an output. |
190
|
|
|
* |
191
|
|
|
* @access public |
192
|
|
|
* @return boolean |
193
|
|
|
*/ |
194
|
|
|
public function hasOutput() |
195
|
|
|
{ |
196
|
|
|
return ($this->_output != ''); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Sets the output of the response. |
201
|
|
|
* |
202
|
|
|
* @access public |
203
|
|
|
* @param string $output |
204
|
|
|
*/ |
205
|
|
|
public function setOutput($output) |
206
|
|
|
{ |
207
|
|
|
$this->_output = $output; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the Location header to redirect a request |
212
|
|
|
* |
213
|
|
|
* @access public |
214
|
|
|
* @param string $location |
215
|
|
|
* @param integer $headerCode |
216
|
|
|
*/ |
217
|
|
|
public function redirectTo($location, $headerCode = 301) |
218
|
|
|
{ |
219
|
|
|
if (strpos($location, 'http://') === false) { |
220
|
|
|
$location = $this->_request->getFullRoute($location); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
header("Location: " . $location, true, $headerCode); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|