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