Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Response
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 getStatusCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReasonPhrase
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withStatus
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setStatus
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Tal;
4
5use GuzzleHttp\Psr7\Utils;
6use function GuzzleHttp\Psr7\stream_for;
7use Psr\Http\Message\ResponseInterface;
8use Psr\Http\Message\StreamInterface;
9
10/**
11 * PSR-7 response implementation.
12 */
13abstract class Response implements ResponseInterface
14{
15    use MessageTrait;
16
17    /** @var array Map of standard HTTP status code/reason phrases */
18    protected static $phrases = [
19        100 => 'Continue',
20        101 => 'Switching Protocols',
21        102 => 'Processing',
22        200 => 'OK',
23        201 => 'Created',
24        202 => 'Accepted',
25        203 => 'Non-Authoritative Information',
26        204 => 'No Content',
27        205 => 'Reset Content',
28        206 => 'Partial Content',
29        207 => 'Multi-status',
30        208 => 'Already Reported',
31        300 => 'Multiple Choices',
32        301 => 'Moved Permanently',
33        302 => 'Found',
34        303 => 'See Other',
35        304 => 'Not Modified',
36        305 => 'Use Proxy',
37        306 => 'Switch Proxy',
38        307 => 'Temporary Redirect',
39        400 => 'Bad Request',
40        401 => 'Unauthorized',
41        402 => 'Payment Required',
42        403 => 'Forbidden',
43        404 => 'Not Found',
44        405 => 'Method Not Allowed',
45        406 => 'Not Acceptable',
46        407 => 'Proxy Authentication Required',
47        408 => 'Request Time-out',
48        409 => 'Conflict',
49        410 => 'Gone',
50        411 => 'Length Required',
51        412 => 'Precondition Failed',
52        413 => 'Request Entity Too Large',
53        414 => 'Request-URI Too Large',
54        415 => 'Unsupported Media Type',
55        416 => 'Requested range not satisfiable',
56        417 => 'Expectation Failed',
57        418 => 'I\'m a teapot',
58        422 => 'Unprocessable Entity',
59        423 => 'Locked',
60        424 => 'Failed Dependency',
61        425 => 'Unordered Collection',
62        426 => 'Upgrade Required',
63        428 => 'Precondition Required',
64        429 => 'Too Many Requests',
65        431 => 'Request Header Fields Too Large',
66        451 => 'Unavailable For Legal Reasons',
67        500 => 'Internal Server Error',
68        501 => 'Not Implemented',
69        502 => 'Bad Gateway',
70        503 => 'Service Unavailable',
71        504 => 'Gateway Time-out',
72        505 => 'HTTP Version not supported',
73        506 => 'Variant Also Negotiates',
74        507 => 'Insufficient Storage',
75        508 => 'Loop Detected',
76        511 => 'Network Authentication Required',
77    ];
78
79    /** @var string */
80    protected $reasonPhrase = '';
81
82    /** @var int */
83    protected $statusCode = 200;
84
85    /**
86     * @param int $status Status code
87     * @param array $headers Response headers
88     * @param string|null|resource|StreamInterface $body Response body
89     * @param string $version Protocol version
90     * @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
91     */
92    public function __construct(
93        $status = 200,
94        array $headers = [],
95        $body = null,
96        $version = '1.1',
97        $reason = null
98    ) {
99        $this->statusCode = (int) $status;
100
101        if ($body !== '' && $body !== null) {
102            $this->stream = Utils::streamFor($body);
103        }
104
105        $this->setHeaders($headers);
106        if ($reason == '' && isset(self::$phrases[$this->statusCode])) {
107            $this->reasonPhrase = self::$phrases[$this->statusCode];
108        } else {
109            $this->reasonPhrase = (string) $reason;
110        }
111
112        $this->protocol = $version;
113    }
114
115    public function getStatusCode(): int
116    {
117        return $this->statusCode;
118    }
119
120    public function getReasonPhrase(): string
121    {
122        return $this->reasonPhrase;
123    }
124
125    public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface
126    {
127        $new = clone $this;
128        return $new->setStatus($code, $reasonPhrase);
129    }
130
131    /**
132     * Sets the specified status code and, optionally, reason phrase.
133     *
134     * If no reason phrase is specified, implementations MAY choose to default
135     * to the RFC 7231 or IANA recommended reason phrase for the response's
136     * status code.
137     *
138     * @link http://tools.ietf.org/html/rfc7231#section-6
139     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
140     * @param int $code The 3-digit integer result code to set.
141     * @param string $reasonPhrase The reason phrase to use with the
142     *     provided status code; if none is provided, implementations MAY
143     *     use the defaults as suggested in the HTTP specification.
144     * @return static
145     * @throws \InvalidArgumentException For invalid status code arguments.
146     */
147    protected function setStatus($code, $reasonPhrase = '')
148    {
149        $this->statusCode = (int) $code;
150        if ($reasonPhrase == '' && isset(static::$phrases[$this->statusCode])) {
151            $reasonPhrase = static::$phrases[$this->statusCode];
152        }
153        $this->reasonPhrase = $reasonPhrase;
154        return $this;
155    }
156}