1
1
<?php
2
2
/**
3
3
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4
- * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
4
+ * @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
5
5
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6
6
*/
7
7
@@ -113,7 +113,7 @@ class Response implements ResponseInterface
113
113
/**
114
114
* @var string
115
115
*/
116
- private $ reasonPhrase = '' ;
116
+ private $ reasonPhrase ;
117
117
118
118
/**
119
119
* @var int
@@ -146,12 +146,6 @@ public function getStatusCode()
146
146
*/
147
147
public function getReasonPhrase ()
148
148
{
149
- if (! $ this ->reasonPhrase
150
- && isset ($ this ->phrases [$ this ->statusCode ])
151
- ) {
152
- $ this ->reasonPhrase = $ this ->phrases [$ this ->statusCode ];
153
- }
154
-
155
149
return $ this ->reasonPhrase ;
156
150
}
157
151
@@ -161,18 +155,18 @@ public function getReasonPhrase()
161
155
public function withStatus ($ code , $ reasonPhrase = '' )
162
156
{
163
157
$ new = clone $ this ;
164
- $ new ->setStatusCode ($ code );
165
- $ new ->reasonPhrase = $ reasonPhrase ;
158
+ $ new ->setStatusCode ($ code , $ reasonPhrase );
166
159
return $ new ;
167
160
}
168
161
169
162
/**
170
163
* Set a valid status code.
171
164
*
172
165
* @param int $code
166
+ * @param string $reasonPhrase
173
167
* @throws InvalidArgumentException on an invalid status code.
174
168
*/
175
- private function setStatusCode ($ code )
169
+ private function setStatusCode ($ code, $ reasonPhrase = '' )
176
170
{
177
171
if (! is_numeric ($ code )
178
172
|| is_float ($ code )
@@ -181,11 +175,24 @@ private function setStatusCode($code)
181
175
) {
182
176
throw new InvalidArgumentException (sprintf (
183
177
'Invalid status code "%s"; must be an integer between %d and %d, inclusive ' ,
184
- ( is_scalar ($ code ) ? $ code : gettype ($ code) ),
178
+ is_scalar ($ code ) ? $ code : gettype ($ code ),
185
179
static ::MIN_STATUS_CODE_VALUE ,
186
180
static ::MAX_STATUS_CODE_VALUE
187
181
));
188
182
}
183
+
184
+ if (! is_string ($ reasonPhrase )) {
185
+ throw new InvalidArgumentException (sprintf (
186
+ 'Unsupported response reason phrase; must be a string, received %s ' ,
187
+ is_object ($ reasonPhrase ) ? get_class ($ reasonPhrase ) : gettype ($ reasonPhrase )
188
+ ));
189
+ }
190
+
191
+ if ($ reasonPhrase === '' && isset ($ this ->phrases [$ code ])) {
192
+ $ reasonPhrase = $ this ->phrases [$ code ];
193
+ }
194
+
195
+ $ this ->reasonPhrase = $ reasonPhrase ;
189
196
$ this ->statusCode = (int ) $ code ;
190
197
}
191
198
}
0 commit comments