MediaWiki  master
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 
22  private $headerContainer;
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() {
53  return $this->protocolVersion;
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 }
static getMessage( $code)
Get the message associated with an HTTP response status code.
Definition: HttpStatus.php:34
This is a container for storing headers.
setCookie( $name, $value, $expire=0, $options=[])
Set a cookie.
Definition: Response.php:100
setProtocolVersion( $version)
Set the HTTP protocol version.
Definition: Response.php:76
hasHeader( $name)
Checks if a header exists by the given case-insensitive name.
Definition: Response.php:60
addHeader( $name, $value)
Append the given value to the specified header.
Definition: Response.php:84
getCookies()
Get all previously set cookies as a list of associative arrays with the following keys:
Definition: Response.php:109
getStatusCode()
Gets the response status code.
Definition: Response.php:36
removeHeader( $name)
Remove the specified header.
Definition: Response.php:88
getBody()
Gets the body of the message.
Definition: Response.php:72
getHeaders()
Retrieves all message header values.
Definition: Response.php:56
getHeader( $name)
Retrieves a message header value by the given case-insensitive name.
Definition: Response.php:64
getProtocolVersion()
Retrieves the HTTP protocol version as a string.
Definition: Response.php:52
getRawHeaderLines()
Get the full header lines including colon-separated name and value, for passing directly to header().
Definition: Response.php:96
getHeaderLine( $name)
Retrieves a comma-separated string of the values for a single header.
Definition: Response.php:68
setBody(StreamInterface $body)
Set the message body.
Definition: Response.php:92
__construct( $bodyContents='')
Definition: Response.php:31
setHeader( $name, $value)
Set or replace the specified header.
Definition: Response.php:80
getReasonPhrase()
Gets the response reason phrase associated with the status code.
Definition: Response.php:40
setStatus( $code, $reasonPhrase='')
Set the status code and, optionally, reason phrase.
Definition: Response.php:44
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.