MediaWiki master
Response.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
5use HttpStatus;
6use Psr\Http\Message\StreamInterface;
7
8class Response implements ResponseInterface {
9
10 private int $statusCode = 200;
11 private string $reasonPhrase = 'OK';
12 private string $protocolVersion = '1.1';
13 private StreamInterface $body;
14 private HeaderContainer $headerContainer;
15 private array $cookies = [];
16
22 public function __construct( $body = '' ) {
23 if ( is_string( $body ) ) {
24 $body = new StringStream( $body );
25 }
26
27 $this->body = $body;
28 $this->headerContainer = new HeaderContainer;
29 }
30
34 public static function cast( ResponseInterface $iResponse ): Response {
35 if ( $iResponse instanceof Response ) {
36 return $iResponse;
37 }
38
39 $resp = new Response(
40 $iResponse->getBody()
41 );
42
43 foreach ( $iResponse->getHeaders() as $name => $values ) {
44 $resp->setHeader( $name, $values );
45 }
46
47 return $resp;
48 }
49
50 public function getStatusCode() {
51 return $this->statusCode;
52 }
53
54 public function getReasonPhrase() {
55 return $this->reasonPhrase;
56 }
57
58 public function setStatus( $code, $reasonPhrase = '' ) {
59 $this->statusCode = $code;
60 if ( $reasonPhrase === '' ) {
61 $reasonPhrase = HttpStatus::getMessage( $code ) ?? '';
62 }
63 $this->reasonPhrase = $reasonPhrase;
64 }
65
66 public function getProtocolVersion() {
67 return $this->protocolVersion;
68 }
69
70 public function getHeaders() {
71 return $this->headerContainer->getHeaders();
72 }
73
74 public function hasHeader( $name ) {
75 return $this->headerContainer->hasHeader( $name );
76 }
77
78 public function getHeader( $name ) {
79 return $this->headerContainer->getHeader( $name );
80 }
81
82 public function getHeaderLine( $name ) {
83 return $this->headerContainer->getHeaderLine( $name );
84 }
85
86 public function getBody() {
87 return $this->body;
88 }
89
90 public function setProtocolVersion( $version ) {
91 $this->protocolVersion = $version;
92 }
93
94 public function setHeader( $name, $value ) {
95 $this->headerContainer->setHeader( $name, $value );
96 }
97
98 public function addHeader( $name, $value ) {
99 $this->headerContainer->addHeader( $name, $value );
100 }
101
102 public function removeHeader( $name ) {
103 $this->headerContainer->removeHeader( $name );
104 }
105
106 public function setBody( StreamInterface $body ) {
107 $this->body = $body;
108 }
109
110 public function getRawHeaderLines() {
111 return $this->headerContainer->getRawHeaderLines();
112 }
113
114 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
115 $this->cookies[] = [
116 'name' => $name,
117 'value' => $value,
118 'expire' => $expire,
119 'options' => $options
120 ];
121 }
122
123 public function getCookies() {
124 return $this->cookies;
125 }
126}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
This is a container for storing headers.
setCookie( $name, $value, $expire=0, $options=[])
Set a cookie.
Definition Response.php:114
setProtocolVersion( $version)
Set the HTTP protocol version.
Definition Response.php:90
hasHeader( $name)
Checks if a header exists by the given case-insensitive name.
Definition Response.php:74
addHeader( $name, $value)
Append the given value to the specified header.
Definition Response.php:98
getCookies()
Get all previously set cookies as a list of associative arrays with the following keys:
Definition Response.php:123
getStatusCode()
Gets the response status code.
Definition Response.php:50
removeHeader( $name)
Remove the specified header.
Definition Response.php:102
getBody()
Gets the body of the message.
Definition Response.php:86
getHeaders()
Retrieves all message header values.
Definition Response.php:70
getHeader( $name)
Retrieves a message header value by the given case-insensitive name.
Definition Response.php:78
getProtocolVersion()
Retrieves the HTTP protocol version as a string.
Definition Response.php:66
getRawHeaderLines()
Get the full header lines including colon-separated name and value, for passing directly to header().
Definition Response.php:110
getHeaderLine( $name)
Retrieves a comma-separated string of the values for a single header.
Definition Response.php:82
setBody(StreamInterface $body)
Set the message body.
Definition Response.php:106
setHeader( $name, $value)
Set or replace the specified header.
Definition Response.php:94
getReasonPhrase()
Gets the response reason phrase associated with the status code.
Definition Response.php:54
setStatus( $code, $reasonPhrase='')
Set the status code and, optionally, reason phrase.
Definition Response.php:58
static cast(ResponseInterface $iResponse)
Definition Response.php:34
A stream class which uses a string as the underlying storage.
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.