<?php
/**
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
- * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
+ * @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
+use function gettype;
+use function is_float;
+use function is_numeric;
+use function is_scalar;
+use function sprintf;
+
/**
* HTTP response encapsulation.
*
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
- 425 => 'Unordered Collection',
+ 425 => 'Too Early',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
/**
* @var string
*/
- private $reasonPhrase = '';
+ private $reasonPhrase;
/**
* @var int
*/
public function getReasonPhrase()
{
- if (! $this->reasonPhrase
- && isset($this->phrases[$this->statusCode])
- ) {
- $this->reasonPhrase = $this->phrases[$this->statusCode];
- }
-
return $this->reasonPhrase;
}
public function withStatus($code, $reasonPhrase = '')
{
$new = clone $this;
- $new->setStatusCode($code);
- $new->reasonPhrase = $reasonPhrase;
+ $new->setStatusCode($code, $reasonPhrase);
return $new;
}
* Set a valid status code.
*
* @param int $code
+ * @param string $reasonPhrase
* @throws InvalidArgumentException on an invalid status code.
*/
- private function setStatusCode($code)
+ private function setStatusCode($code, $reasonPhrase = '')
{
if (! is_numeric($code)
|| is_float($code)
) {
throw new InvalidArgumentException(sprintf(
'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
- (is_scalar($code) ? $code : gettype($code)),
+ is_scalar($code) ? $code : gettype($code),
static::MIN_STATUS_CODE_VALUE,
static::MAX_STATUS_CODE_VALUE
));
}
- $this->statusCode = $code;
+
+ if (! is_string($reasonPhrase)) {
+ throw new InvalidArgumentException(sprintf(
+ 'Unsupported response reason phrase; must be a string, received %s',
+ is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
+ ));
+ }
+
+ if ($reasonPhrase === '' && isset($this->phrases[$code])) {
+ $reasonPhrase = $this->phrases[$code];
+ }
+
+ $this->reasonPhrase = $reasonPhrase;
+ $this->statusCode = (int) $code;
}
}