MediaWiki master
Response.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
5use 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
51 public function getStatusCode() {
52 return $this->statusCode;
53 }
54
56 public function getReasonPhrase() {
57 return $this->reasonPhrase;
58 }
59
61 public function setStatus( $code, $reasonPhrase = '' ) {
62 $this->statusCode = $code;
63 if ( $reasonPhrase === '' ) {
64 $reasonPhrase = HttpStatus::getMessage( $code ) ?? '';
65 }
66 $this->reasonPhrase = $reasonPhrase;
67 }
68
70 public function getProtocolVersion() {
71 return $this->protocolVersion;
72 }
73
75 public function getHeaders() {
76 return $this->headerContainer->getHeaders();
77 }
78
80 public function hasHeader( $name ) {
81 return $this->headerContainer->hasHeader( $name );
82 }
83
85 public function getHeader( $name ) {
86 return $this->headerContainer->getHeader( $name );
87 }
88
90 public function getHeaderLine( $name ) {
91 return $this->headerContainer->getHeaderLine( $name );
92 }
93
95 public function getBody() {
96 return $this->body;
97 }
98
100 public function setProtocolVersion( $version ) {
101 $this->protocolVersion = $version;
102 }
103
105 public function setHeader( $name, $value ) {
106 $this->headerContainer->setHeader( $name, $value );
107 }
108
110 public function addHeader( $name, $value ) {
111 $this->headerContainer->addHeader( $name, $value );
112 }
113
115 public function removeHeader( $name ) {
116 $this->headerContainer->removeHeader( $name );
117 }
118
120 public function setBody( StreamInterface $body ) {
121 $this->body = $body;
122 }
123
125 public function getRawHeaderLines() {
126 return $this->headerContainer->getRawHeaderLines();
127 }
128
130 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
131 $this->cookies[] = [
132 'name' => $name,
133 'value' => $value,
134 'expire' => $expire,
135 'options' => $options
136 ];
137 }
138
140 public function getCookies() {
141 return $this->cookies;
142 }
143}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
This is a container for storing headers.
setCookie( $name, $value, $expire=0, $options=[])
Set a cookie.The name will have the cookie prefix added to it before it is sent over the network.
Definition Response.php:130
setProtocolVersion( $version)
Set the HTTP protocol version.The version string MUST contain only the HTTP version number (e....
Definition Response.php:100
hasHeader( $name)
Checks if a header exists by the given case-insensitive name.bool Returns true if any header names ma...
Definition Response.php:80
addHeader( $name, $value)
Append the given value to the specified header.Existing values for the specified header will be maint...
Definition Response.php:110
getCookies()
Get all previously set cookies as a list of associative arrays with the following keys:name: The cook...
Definition Response.php:140
getStatusCode()
Gets the response status code.The status code is a 3-digit integer result code of the server's attemp...
Definition Response.php:51
removeHeader( $name)
Remove the specified header.Header resolution MUST be done without case-sensitivity.
Definition Response.php:115
getBody()
Gets the body of the message.StreamInterface Returns the body as a stream.
Definition Response.php:95
getHeaders()
Retrieves all message header values.The keys represent the header name as it will be sent over the wi...
Definition Response.php:75
getHeader( $name)
Retrieves a message header value by the given case-insensitive name.This method returns an array of a...
Definition Response.php:85
getProtocolVersion()
Retrieves the HTTP protocol version as a string.The string MUST contain only the HTTP version number ...
Definition Response.php:70
getRawHeaderLines()
Get the full header lines including colon-separated name and value, for passing directly to header()....
Definition Response.php:125
getHeaderLine( $name)
Retrieves a comma-separated string of the values for a single header.This method returns all of the h...
Definition Response.php:90
setBody(StreamInterface $body)
Set the message body.The body MUST be a StreamInterface object.\InvalidArgumentException When the bod...
Definition Response.php:120
setHeader( $name, $value)
Set or replace the specified header.While header names are case-insensitive, the casing of the header...
Definition Response.php:105
getReasonPhrase()
Gets the response reason phrase associated with the status code.Because a reason phrase is not a requ...
Definition Response.php:56
setStatus( $code, $reasonPhrase='')
Set the status code and, optionally, reason phrase.If no reason phrase is specified,...
Definition Response.php:61
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.